You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.71.0新架构Codegen构建失败求助

React Native 0.71.0新架构Codegen构建失败解决方案

问题描述

  • 任务执行异常:任务:react-native:ReactAndroid:buildCodegenCLI执行失败,启动命令.../node_modules/@react-native/codegen/scripts/oss/build.sh时出现问题。

已完成操作

  • 在gradle.properties中设置newArchEnabled=true
  • 在package.json的devDependencies中添加了"react-native-codegen": "^0.71.5"

配置文件内容

settings.gradle

rootProject.name = 'XXX'
include ':app'

includeBuild('../node_modules/react-native-gradle-plugin')
if (settings.hasProperty("newArchEnabled") && settings.newArchEnabled == "true") {
    include(":ReactAndroid")
    project(":ReactAndroid").projectDir = file('../node_modules/react-native/ReactAndroid')
    include(":ReactAndroid:hermes-engine")
    project(":ReactAndroid:hermes-engine").projectDir = file('../node_modules/react-native/ReactAndroid/hermes-engine')
}
includeBuild('../node_modules/react-native') {
dependencySubstitution {
         substitute(module("com.facebook.react:react-android")).using(project(":ReactAndroid"))
         substitute(module("com.facebook.react:react-native")).using(project(":ReactAndroid"))
         substitute(module("com.facebook.react:hermes-android")).using(project(":ReactAndroid:hermes-engine"))
         substitute(module("com.facebook.react:hermes-engine")).using(project(":ReactAndroid:hermes-engine"))
        //  substitute(module("com.facebook.react:react-native-gradle-plugin")).using(project(":"))
     }
 }
include ':@react-native-community_datetimepicker'
project(':@react-native-community_datetimepicker').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/datetimepicker/android')
include ':@react-native-community_datetimepicker'
project(':@react-native-community_datetimepicker').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/datetimepicker/android')
include ':react-native-firebase'
project(':react-native-firebase').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-firebase/android')
include ':react-native-device-info'
project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
include ':react-native-device-info'
project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
include ':rn-fetch-blob'
project(':rn-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/rn-fetch-blob/android')
include ':react-native-is-device-locked'
project(':react-native-is-device-locked').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-is-device-locked/android')
include ':react-native-otp-verify'
project(':react-native-otp-verify').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-otp-verify/android')
include ':react-native-otp-verify'
project(':react-native-otp-verify').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-otp-verify/android')

// apply from: '../node_modules/react-native-unimodules/gradle.groovy'
// includeUnimodulesProjects()
apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/autolinking.gradle");
useExpoModules()

// apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle");
apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json')"].execute(null, rootDir).text.trim(), "../native_modules.gradle");
applyNativeModulesSettingsGradle(settings)

include ':app'

apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/autolinking.gradle")
useExpoModules()

app/build.gradle

apply plugin: "com.android.application"
apply plugin: "com.facebook.react"
import com.android.build.OutputFile
react {
}


project.ext.react = [
    enableHermes: true,
    entryFile: "index.js",
    bundleInDebug: true,
    bundleInRelease: false,
    cliPath: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()).getParentFile().getAbsolutePath() + "/cli.js",
    hermesCommand: '../../node_modules/react-native/sdks/hermesc/%OS-BIN%/hermesc',
    composeSourceMapsPath: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()).getParentFile().getAbsolutePath() + "/scripts/compose-source-maps.js",
]

apply from: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim(), "../react.gradle")

project.ext.vectoricons = [
    iconFontNames: [ 'MaterialIcons.ttf', 'Ionicons.ttf' ] 
]

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

def enableSeparateBuildPerCPUArchitecture = false


def enableProguardInReleaseBuilds = false


def jscFlavor = 'org.webkit:android-jsc:+'


def enableHermes = project.ext.react.get("enableHermes", true);
def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim());
                configurations.all {
        resolutionStrategy {
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
    }
}

android {
    compileSdkVersion 33

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    defaultConfig {
        applicationId 'xxx.xxx.xxx'
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 9
        versionName "1.0.2"
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
    }

    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        release {
            if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                storeFile file(MYAPP_UPLOAD_STORE_FILE)
                storePassword MYAPP_UPLOAD_STORE_PASSWORD
                keyAlias MYAPP_UPLOAD_KEY_ALIAS
                keyPassword MYAPP_UPLOAD_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

      applicationVariants.all { variant ->
        variant.outputs.each { output ->
      
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }

        }
    }
    packagingOptions {
        pickFirst '**/*.so'
        pickFirst '**/libc++_shared.so'
        pickFirst '**/libfbjni.so'
        pickFirst 'lib/armeabi-v7a/your_name.so'
        pickFirst 'lib/arm64-v8a/your_name.so'
        pickFirst 'lib/x86/your_name.so'
        pickFirst 'lib/x86_64/your_name.so'
    }
     ndkVersion '24.0.8215888'

    dexOptions {
        incremental true
    }
    }

dependencies {
     // The version of react-native is set by the React Native Gradle Plugin
    implementation("com.facebook.react:react-android")

    implementation fileTree(dir: "libs", include: ["*.jar"])
       
    def isGifEnabled = (findProperty('expo.gif.enabled') ?: "") == "true";
    def isWebpEnabled = (findProperty('expo.webp.enabled') ?: "") == "true";
    def isWebpAnimatedEnabled = (findProperty('expo.webp.animated') ?: "") == "true";

    if (isGifEnabled || isWebpEnabled) {
        implementation 'com.facebook.fresco:fresco:2.0.0'
        implementation 'com.facebook.fresco:imagepipeline-okhttp3:2.0.0'
    }

    if (isGifEnabled) {
        // For animated gif support
        implementation 'com.facebook.fresco:animated-gif:2.0.0'
    }

    if (isWebpEnabled) {
        // For webp support
        implementation 'com.facebook.fresco:webpsupport:2.0.0'
        if (isWebpAnimatedEnabled) {
            // Animated webp support
            implementation 'com.facebook.fresco:animated-webp:2.0.0'
        }
    }

    implementation platform('com.google.firebase:firebase-bom:31.2.3')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.facebook.soloader:soloader:0.10.5'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
    implementation 'com.google.firebase:firebase-appcheck-safetynet:16.1.2'
    debugImplementation("com.facebook.flipper:flipper:0.185.0") {
      exclude group:'com.facebook.fbjni'
    }
    debugImplementation("com.facebook.flipper:flipper-network-plugin:0.185.0") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }
    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:0.182.0") {
        exclude group:'com.facebook.flipper'
    }
    
    if (enableHermes) {
        debugImplementation files(new File(["node", "--print", "require.resolve('hermes-engine/package.json')"].execute(null, rootDir).text.trim(), "../android/hermes-debug.aar"))
        releaseImplementation files(new File(["node", "--print", "require.resolve('hermes-engine/package.json')"].execute(null, rootDir).text.trim(), "../android/hermes-release.aar"))
    } else {
        implementation jscFlavor
    }
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.api
    into 'libs'
}

apply from: new File(["node", "--print", "require.resolve('@react-native-community/cli-platform-android/package.json')"].execute(null, rootDir).text.trim(), "../native_modules.gradle");
applyNativeModulesAppBuildGradle(project)
apply plugin: 'com.google.gms.google-services'

解决方案

1. 修复脚本执行权限

进入node_modules/@react-native/codegen/scripts/oss/目录,执行:

chmod +x build.sh

确保脚本拥有可执行权限,避免Gradle调用时因权限不足失败。

2. 匹配Codegen与RN版本

React Native 0.71.0要求react-native-codegen版本严格对应0.71.x分支,修改package.json中的版本为:

"react-native-codegen": "^0.71.0"

然后重新安装依赖:

rm -rf node_modules package-lock.json
npm install

3. 清理缓存并重新构建

  • 清理Gradle缓存:
cd android && ./gradlew clean
  • 清理Metro缓存:
npx react-native start --reset-cache
  • 重新执行构建:
cd android && ./gradlew assembleDebug

4. 修复settings.gradle重复配置

删除settings.gradle中重复的模块include代码(如:@react-native-community_datetimepicker、:react-native-device-info等重复项),避免Gradle解析冲突。

5. 检查环境依赖

确保系统满足以下要求:

  • 安装Python 3.8及以上版本
  • 使用JDK 11(与项目compileOptions配置一致)
  • 确认Android NDK 24.0.8215888已在Android Studio中正确下载

内容的提问来源于stack exchange,提问作者Preetika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 23:34:56