使用Cordova构建Bundle时无法解析com.android.tools.build:gradle:7.4.2
解决Cordova构建Android Bundle时的依赖解析失败问题
错误信息
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'project name'. > Could not resolve all files for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:7.4.2. Required by: project : > No matching variant of com.android.tools.build:gradle:7.4.2 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but: - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.2 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') * 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 Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 10s Command failed with exit code 1: D:\project_name\platforms\android\gradlew -b D:\project_name\platforms\android\build.gradle :app:bundleRelease
当前环境信息
- Gradle版本: Gradle 8.3,Kotlin 1.9.0,Groovy 3.0.17,Ant 1.10.13,JVM 1.8.0_301
- Java版本: java version "20.0.2" 2023-07-18
问题根源
- Android Gradle插件(AGP)与Gradle版本不匹配: AGP 7.4.2要求Gradle版本范围为7.5-7.6,但当前使用的是Gradle 8.3,版本跨度超出兼容范围。
- Java版本冲突: AGP 7.4.2要求运行环境为Java 11,但当前环境同时存在Java 8(JVM)和Java 20,版本不兼容导致依赖解析失败。
解决方案
1. 统一Java版本为Java 11
- 卸载系统中已安装的Java 20,安装Java 11(OpenJDK或Oracle JDK均可)。
- 配置系统环境变量:
- 设置
JAVA_HOME指向Java 11的安装目录。 - 将Java 11的
bin目录添加到PATH环境变量最前端,确保优先调用。
- 设置
- 验证版本:打开命令提示符,执行
java -version和javac -version,确认输出为Java 11。
2. 调整AGP与Gradle版本匹配
修改Cordova Android平台的配置文件:
- 打开
platforms/android/build.gradle文件,找到dependencies块中的AGP依赖,将版本改为与Gradle 8.3兼容的版本(推荐AGP 8.1.x,如8.1.4):dependencies { classpath 'com.android.tools.build:gradle:8.1.4' // 其他依赖... } - 打开
platforms/android/gradle/wrapper/gradle-wrapper.properties文件,确认distributionUrl指向Gradle 8.3(与AGP 8.1.x兼容):distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
3. 清理Cordova构建缓存
执行以下命令清理旧平台和缓存:
cordova platform remove android cordova platform add android cordova clean
4. 重新构建Android Bundle
执行构建命令:
cordova build android --release --bundle
内容的提问来源于stack exchange,提问作者Kirti Umrigar
相关产品推荐
相关产品推荐

