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

Gradle命令行测试执行失败:navigation-safe-args插件兼容问题

问题解决:Gradle命令行无法解析navigation-safe-args-gradle-plugin:2.6.0(Java版本/插件API不兼容)

问题概述

多模块Android项目,更新gradlew及依赖库后,所有测试相关Gradle命令行执行均失败,报错无法解析androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0,提示Java版本(Java11与Java17)及插件API版本不兼容,但IDE中通过快捷键执行RUN ALL TESTS可正常运行,需解决流水线命令行测试执行问题。

执行过的命令

./gradlew assemblePrdDebug testPrdDebug jacocoTestPrdDebugUnitTestReport sonarqube --stacktrace
./gradlew assemblePrdDebug testPrdDebug createDebugCoverageReport sonarqube --stacktrace
./gradlew clean test createDebugCoverageReport jacocoTestReport --info --stacktrace --warning-mode all
./gradlew jacocoCombinedTestReports
./gradlew tasks
./gradlew jacocoReport 
./gradlew testVariantNameUnitTest --tests
./gradlew :data:connectedPrdDebugAndroidTest
./gradlew :common:testDebugUnitTest
./gradlew :data:connectedPrdDebugAndroidTest --stacktrace --info --scan

错误详情

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 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.2' 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 11
          - Other compatible attribute:
              - Doesn't say anything about org.gradle.plugin.api-version (required '8.2')
      - 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 11
          - Other compatible attribute:
              - Doesn't say anything about org.gradle.plugin.api-version (required '8.2')
      - 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 11)
              - 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.2')
Exception
org.gradle.api.ProjectConfigurationException: A problem occurred configuring root project 'canais-mobile-android'.  
at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(LifecycleProjectEvaluator.java:84)  
•••
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':classpath'.   
at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.mapFailure(DefaultConfiguration.java:1769) 
•••
Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve androidx.navigation:navigation-safe-args-gradle-plugin:2.6.0.   
Required by:    
    project :   
Caused by: org.gradle.internal.component.NoMatchingConfigurationSelectionException: 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 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.2' 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 11   
      - Other compatible attribute: ...

错误原因

  • navigation-safe-args-gradle-plugin:2.6.0 要求运行环境为Java 17,但命令行/流水线环境使用的是Java 11,版本不兼容。
  • IDE能正常运行是因为Android Studio默认自带Java 17适配Gradle 8.x版本,而命令行环境未同步配置。
  • Gradle 8.2的插件API版本与插件的兼容性要求不匹配,加剧了依赖解析失败问题。

解决方案

1. 统一命令行/流水线的Java版本为17

  • 本地命令行:检查当前Java版本,若不是17则切换:
    # 检查版本
    java -version
    # 临时指定JDK 17路径执行命令
    export JAVA_HOME=/path/to/jdk-17
    ./gradlew [你的测试命令]
    
  • 流水线环境:在CI配置中指定JDK 17,例如GitHub Actions:
    steps:
      - name: 配置JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: '17'
          distribution: 'temurin'
    

2. 同步Gradle配置

  • 在项目根目录gradle.properties中明确Java版本:
    org.gradle.java.home=/path/to/jdk-17
    org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
    android.compileSdk=34 # 确保compileSdk与插件版本兼容
    
  • 确认根build.gradle中插件声明正确,且与Gradle版本匹配:
    plugins {
        id 'com.android.application' version '8.2.0' apply false
        id 'androidx.navigation.safeargs' version '2.6.0' apply false
    }
    
    同时检查gradle/wrapper/gradle-wrapper.properties中的Gradle版本:
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
    

3. 修复插件依赖声明方式

确保插件是作为Gradle插件而非普通依赖声明:

// 根build.gradle中声明
plugins {
    id 'androidx.navigation.safeargs' version '2.6.0' apply false
}

// 在需要使用的模块build.gradle中应用
plugins {
    id 'com.android.application'
    id 'androidx.navigation.safeargs'
}

4. 清理缓存并重新构建

执行命令清理Gradle缓存,避免缓存导致的版本冲突:

./gradlew clean build --refresh-dependencies

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 00:43:21