添加Product Flavors后React Native安卓构建卡在指定任务
React Native安卓添加Product Flavors后构建卡在
:app:createBundleDevelopmentDebugJsAndAssets的排查 问题描述
为React Native安卓项目添加Product Flavors后,运行启动脚本react-native run-android --mode=developmentdebug --appId .dev时,构建无限期卡在:app:createBundleDevelopmentDebugJsAndAssets任务。未添加productFlavors前,react-native run-android可正常启动应用,对比其他配置了Product Flavors的项目,未发现build.gradle配置差异。
卡住时的日志
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details > Configure project :app Reading env from: .env.debug WARNING: DSL element 'dexOptions' is obsolete and should be removed. It will be removed in version 8.0 of the Android Gradle plugin. Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically. > Configure project :react-native-reanimated Android gradle plugin: 8.1.1 Gradle: 8.3 > Task :app:createBundleDevelopmentDebugJsAndAssets debug Reading Metro config from /Users/brianvarley/Projects/ChillMode/metro.config.js warning: the transform cache was reset. Welcome to Metro v0.80.6 Fast - Scalable - Integrated <=------------> 11% EXECUTING [9m 12s] > IDLE > :app:createBundleDevelopmentDebugJsAndAssets
参考的app/build.gradle精简版本
apply plugin: "com.android.application" apply plugin: "org.jetbrains.kotlin.android" apply plugin: "com.facebook.react" project.ext.envConfigFiles = [ productionrelease: ".env.prod", developmentdebug: ".env.debug", ] project.ext.react = [ inputExcludes: ["ios/**", "__tests__/**", "bundle_out/**"] ]; apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle" ... android { dexOptions { preDexLibraries false javaMaxHeapSize "8g" } ndkVersion rootProject.ext.ndkVersion buildToolsVersion rootProject.ext.buildToolsVersion compileSdk rootProject.ext.compileSdkVersion namespace "com.bvapps.chillmode" flavorDimensions "default" defaultConfig { applicationId "com.bvapps.chillmode" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" } signingConfigs { debug { storeFile file('debug.keystore') storePassword 'android' keyAlias 'androiddebugkey' keyPassword 'android' } release { if (project.env.get('RELEASE_STORE_FILE')) { storeFile file(project.env.get('RELEASE_STORE_FILE')) storePassword project.env.get('RELEASE_STORE_PASSWORD') keyAlias project.env.get('RELEASE_KEY_ALIAS') keyPassword project.env.get('RELEASE_KEY_PASSWORD') } } } productFlavors { production { dimension "default" applicationIdSuffix ".prod" } development { dimension "default" applicationIdSuffix ".dev" } } buildTypes { debug { signingConfig signingConfigs.debug } release { // Caution! In production, you need to generate your own keystore file. signingConfig signingConfigs.debug minifyEnabled enableProguardInReleaseBuilds proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" } } } dependencies { // The version of react-native is set by the React Native Gradle Plugin implementation("com.facebook.react:react-android") implementation("com.facebook.react:flipper-integration") if (hermesEnabled.toBoolean()) { implementation("com.facebook.react:hermes-android") } else { implementation jscFlavor } } apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
可能的原因及解决方案
1. Metro缓存或端口占用
Metro服务缓存异常或端口被占用会导致bundle任务停滞:
- 执行
npx react-native start --reset-cache手动重启Metro并清除缓存,再重新运行构建命令。 - 检查端口占用:
lsof -i :8081(macOS/Linux)或netstat -ano | findstr :8081(Windows),杀掉占用进程后重试。
2. React Native Config环境文件匹配问题
虽然日志显示读取了.env.debug,但仍需确认flavor+buildType的组合配置:
- 尝试在productFlavors中直接指定环境文件,避免拼接错误:
development { dimension "default" applicationIdSuffix ".dev" project.ext.envConfigFile = file(".env.debug") }
3. Gradle Daemon兼容性问题
日志提示有不兼容的Gradle Daemon无法复用,可能导致构建异常:
- 执行
./gradlew --stop停止所有Gradle Daemon,再重新运行构建命令。 - 验证Gradle与AGP版本兼容性:AGP 8.1.x推荐Gradle 8.0-8.2,可尝试降级Gradle版本测试。
4. Hermes引擎相关问题
启用Hermes时可能存在字节码编译或缓存问题:
- 临时禁用Hermes,将
hermesEnabled设为false,重新构建看是否能正常完成;若恢复正常,再排查Hermes配置或升级对应版本。
5. 文件权限或路径问题
Metro读取文件时可能遇到权限不足或路径错误:
- 检查项目目录的读写权限,确保当前用户有权访问所有文件。
- 确认
metro.config.js中的inputExcludes等配置是否合理,避免误排除必要文件。
6. 第三方插件兼容性问题
react-native-reanimated等插件可能与当前RN版本不兼容:
- 更新
react-native-reanimated到与RN版本匹配的稳定版本,或临时移除该插件后重新构建测试。
内容的提问来源于stack exchange,提问作者Brian Var
相关产品推荐
相关产品推荐

