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

执行npm run android时出现:amazon-cognito-identity-js:javaPreCompileDebug错误求助

React Native构建失败::amazon-cognito-identity-js:javaPreCompileDebug因Java堆空间不足报错

问题详情

执行npm run android构建React Native应用时,:amazon-cognito-identity-js:javaPreCompileDebug任务执行失败,错误根源为Java堆空间不足,导致JetifyTransform转换react-native-0.71.0-rc.0-debug.aar时失败。此前构建一切正常,当前package.json中RN版本为0.63.4,但构建过程中出现了高版本RN依赖,推测是第三方库间接引入导致冲突。

完整构建日志

> DriverApp@0.0.1 android
> react-native run-android

info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1256 file(s) to forward-jetify. Using 8 workers...
info JS server already running.
info Installing the app...
> Task :amazon-cognito-identity-js:javaPreCompileDebug FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.2/userguide/command_line_interface.html#sec:command_line_warnings
8 actionable tasks: 8 executed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':amazon-cognito-identity-js:javaPreCompileDebug'.
> Could not resolve all files for configuration ':amazon-cognito-identity-js:debugCompileClasspath'.
   > Failed to transform react-native-0.71.0-rc.0-debug.aar (com.facebook.react:react-native:0.71.0-rc.0) to match attributes {artifactType=android-classes, com.android.build.api.attributes.BuildTypeAttr=debug, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-api}.
      > Execution failed for JetifyTransform: C:\Users\x\.gradle\caches\modules-2\files-2.1\com.facebook.react\react-native\0.71.0-rc.0\x\react-native-0.71.0-rc.0-debug.aar.
         > Java heap space

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

package.json配置

{
  "dependencies": {
    "@react-native-community/geolocation": "^2.0.2",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "^5.9.10",
    "@react-native-community/slider": "^4.2.3",
    "@react-native-picker/picker": "^2.4.1",
    "@react-navigation/drawer": "^5.12.3",
    "@react-navigation/native": "^5.9.2",
    "@react-navigation/stack": "^5.14.2",
    "amazon-cognito-identity-js": "^4.5.10",
    "aws-amplify": "^3.3.18",
    "aws-amplify-react-native": "^4.3.1",
    "react": "16.13.1",
    "react-native": "0.63.4",
    "react-native-camera": "^4.2.1",
    "react-native-geolocation-service": "^5.3.0-beta.4",
    "react-native-gesture-handler": "^1.9.0",
    "react-native-google-places-autocomplete": "^2.1.2",
    "react-native-image-picker": "^4.10.0",
    "react-native-maps": "0.27.1",
    "react-native-maps-directions": "^1.8.0",
    "react-native-material-cards": "^1.0.16",
    "react-native-reanimated": "^1.13.2",
    "react-native-safe-area-context": "^3.1.9",
    "react-native-screens": "^2.17.1",
    "react-native-vector-icons": "^7.1.0",
    "rn-qr-generator": "^1.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.8.4",
    "@babel/runtime": "^7.8.4",
    "@react-native-community/eslint-config": "^1.1.0",
    "@types/react-native": "~0.67.6",
    "babel-jest": "^25.1.0",
    "eslint": "^6.5.1",
    "jest": "^28.1.3",
    "metro-react-native-babel-preset": "^0.59.0",
    "react-test-renderer": "16.13.1"
  }
}

解决方案

1. 增加Gradle Java堆内存分配

修改项目根目录下的gradle.properties文件,添加或更新以下配置:

org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError

该配置将Gradle的JVM堆内存上限提升至4GB,同时调整元空间大小,避免堆溢出。

2. 清理Gradle缓存并重新构建

  • 执行命令清理项目构建缓存:
    ./gradlew cleanBuildCache
    
  • 删除本地Gradle缓存目录(Windows:C:\Users\x\.gradle\caches;macOS/Linux:~/.gradle/caches),然后重新执行npm run android。

3. 锁定React Native版本解决依赖冲突

从日志看,当前项目指定RN版本为0.63.4,但构建过程中出现了0.71.0-rc.0的依赖,推测是amazon-cognito-identity-js或其他第三方库间接引入。在android/build.gradle中添加强制依赖版本:

allprojects {
    repositories {
        // 保留现有仓库配置
    }
    configurations.all {
        resolutionStrategy {
            force 'com.facebook.react:react-native:0.63.4'
        }
    }
}

也可以升级amazon-cognito-identity-js到适配RN0.63.4的稳定版本,确保依赖兼容性。

4. 尝试禁用Jetifier(项目已迁移AndroidX时)

如果项目已完全迁移到AndroidX,可尝试禁用Jetify减少转换压力:

  • 临时禁用:执行构建时添加参数
    npm run android -- --no-jetifier
    
  • 永久禁用:修改android/gradle.properties
    android.useAndroidX=true
    android.enableJetifier=false
    

注意:若项目中存在未迁移到AndroidX的依赖,禁用Jetifier可能引发其他错误,需谨慎使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 09:40:24