Java 17下执行gradlew无法解析navigation-safe-args-gradle-plugin问题
问题描述
使用Java 17通过./gradlew命令构建Android项目时,出现依赖解析失败错误,核心提示为:
Could not resolve androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0.
No matching variant of androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 13...
但通过Android Studio图形界面构建运行完全正常,该错误同时出现在Git Actions的CI工作流中,项目必须使用Java 17。
完整错误日志:
MacBook-Pro:My-Android me$ ./gradlew Starting a Gradle Daemon (subsequent builds will be faster) FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'My-Android'. > Could not resolve all files for configuration ':classpath'. > Could not resolve androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0. Required by: project : > No matching variant of androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 13, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.1' but: - Variant 'apiElements' capability androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0 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 17 and the consumer needed a component for use during runtime, compatible with Java 13 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.1') - Variant 'runtimeElements' capability androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 13 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.1') - Variant 'sourcesElements' capability androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0 declares a component for use during runtime, 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 13) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '8.1')
原因分析
- Gradle构建JVM版本不匹配:命令行(含CI)环境中Gradle使用的JVM版本为Java 13,而项目要求用Java 17,且navigation-safe-args插件2.6.0仅兼容Java 17,导致版本冲突。Android Studio会使用项目指定的Java 17环境,因此无此问题。
- 插件依赖声明错误:可能在构建脚本中错误地将navigation-safe-args插件作为runtime依赖声明,而非classpath依赖,导致Gradle按runtime组件匹配,与插件本身的compile-time属性冲突。
- Gradle缓存/Daemon状态异常:旧缓存或Daemon进程保留了Java 13的配置信息,导致新构建时出现不兼容。
解决办法
1. 强制Gradle使用Java 17
- 配置gradle.properties:在项目根目录的
gradle.properties中添加:
替换org.gradle.java.home=/path/to/your/java17/jdk org.gradle.jvmargs=-Xmx2048m --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED/path/to/your/java17/jdk为实际Java 17安装路径。 - 设置环境变量:命令行或CI工作流中设置:
export GRADLE_JAVA_HOME=/path/to/your/java17/jdk export JAVA_HOME=/path/to/your/java17/jdk - 统一项目编译版本:在模块
build.gradle中确保编译版本为Java 17:android { compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } // Kotlin项目需额外配置 kotlinOptions { jvmTarget = "17" } }
2. 修正插件声明方式
确保插件在根项目build.gradle中正确声明为classpath依赖:
- Plugins DSL方式(推荐):
plugins { id 'com.android.application' version '8.1.0' apply false id 'androidx.navigation.safeargs' version '2.6.0' apply false // Kotlin项目替换为id 'androidx.navigation.safeargs.kotlin' version '2.6.0' apply false } - buildscript方式:
buildscript { repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.1.0' classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0" } }
随后在应用模块build.gradle中应用插件:
plugins { id 'com.android.application' id 'androidx.navigation.safeargs' // Kotlin项目替换为id 'androidx.navigation.safeargs.kotlin' }
3. 清理缓存并重启Gradle Daemon
运行以下命令清理缓存并停止旧Daemon进程:
./gradlew clean build --no-build-cache ./gradlew --stop
之后重新执行构建命令即可。
Android Studio执行的具体构建命令
Android Studio会根据操作自动生成对应gradlew命令,常见命令包括:
- 构建Debug变体:
./gradlew assembleDebug - 构建并安装Debug变体:
./gradlew installDebug - 完整构建所有变体:
./gradlew build - 运行Lint检查:
./gradlew lint - 清理构建产物:
./gradlew clean
AS会自动将JDK环境设置为项目在Project Structure中指定的版本(通常为Java 17),因此不会出现命令行中的版本不兼容问题。
内容的提问来源于stack exchange,提问作者Jesus Dimrix
相关产品推荐
相关产品推荐

