使用Github Actions构建React Native安卓应用遇依赖解析错误求助
React Native安卓应用Github Actions构建失败解决方案
错误信息
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'Bayanihan'. > Could not determine the dependencies of null. > Could not resolve all task dependencies for configuration ':classpath'. > Could not find com.android.tools.build:gradle:. Required by: project : > Could not resolve com.facebook.react:react-native-gradle-plugin. Required by: project : > No matching variant of project :gradle-plugin was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0.1' but: - Variant 'apiElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1') - Variant 'mainSourceElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a component, and its dependencies declared externally: BUILD FAILED in 1m 1s Error: Process completed with exit code 1.
环境版本
- Java 8
- Gradle 8
- Android 34
- Node.js 16
问题分析
- Android Gradle插件版本缺失:根项目构建配置中未指定
com.android.tools.build:gradle的具体版本,导致依赖解析失败。 - Java版本不兼容:
react-native-gradle-plugin要求Java 11环境,但当前使用Java 8,存在运行时环境冲突。 - Gradle插件API版本不匹配:构建配置要求
org.gradle.plugin.api-version为8.0.1,但插件变体未满足该属性要求。
解决方案
指定Android Gradle插件版本
打开根目录的build.gradle(或build.gradle.kts),为com.android.tools.build:gradle添加与Gradle 8兼容的版本(如8.0.2),同时指定对应React Native版本的react-native-gradle-plugin:buildscript { repositories { google() mavenCentral() } dependencies { // 适配Gradle 8的AGP版本 classpath("com.android.tools.build:gradle:8.0.2") // 替换为项目实际使用的React Native版本,例如0.72.6 classpath("com.facebook.react:react-native-gradle-plugin:0.72.6") } }升级Java版本到11
修改Github Actions工作流文件(如.github/workflows/android.yml),将JDK版本设置为11:steps: - name: Set up JDK 11 uses: actions/setup-java@v4 with: java-version: '11' distribution: 'temurin'确认Gradle版本匹配
检查gradle/wrapper/gradle-wrapper.properties中的distributionUrl是否对应要求的Gradle 8.0.1:distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-all.zip清理并刷新依赖
在Github Actions构建步骤中添加依赖清理与同步命令:./gradlew clean ./gradlew assembleRelease --refresh-dependencies
内容的提问来源于stack exchange,提问作者Mounika Krishna
相关产品推荐
相关产品推荐

