gradle 无法解析 java 库中的 androidx 库

问题描述 投票:0回答:1

我正在使用 androidx.test 库在我的 java 库 gradle 项目中设置仪器测试。我在 build.gradle 文件中添加了以下依赖项:

plugins {
    id 'java-library'
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation 'androidx.test:runner:1.6.2'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

但是,Gradle 无法解决其某些传递依赖项,从而导致构建失败。我是 Gradle 新手,不知道如何解决这些缺失的依赖项。

这是我收到的错误消息:

:lib:test: Could not resolve androidx.tracing:tracing:1.1.0.
Required by:
    project :lib > androidx.test:runner:1.6.2
    project :lib > androidx.test:runner:1.6.2 > androidx.test:monitor:1.7.2

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

我尝试过的:

  1. 清除 Gradle 缓存。
  2. 使Android Studio失效并重启。
  3. 检查 androidx.test:runner 的最新版本。

这些步骤都没有解决问题。

问题:

  1. 如何配置 Gradle 以正确解决 androidx.test 的传递依赖关系?
  2. 我是否需要手动添加缺少的依赖项,或者有没有办法确保自动获取所有必需的库?

我正在尝试导入的 Maven 存储库链接

./gradlew :lib:依赖项的输出



        > Task :lib:dependencies

        ------------------------------------------------------------
        Project ':lib'
        ------------------------------------------------------------

        annotationProcessor - Annotation processors and their dependencies for source set 'main'.
        No dependencies

        api - API dependencies for the 'main' feature. (n)
        No dependencies

        apiElements - API elements for the 'main' feature. (n)
        No dependencies

        compileClasspath - Compile classpath for source set 'main'.
        \--- androidx.test:runner:1.6.2
            +--- androidx.annotation:annotation:1.7.0-beta01
            |    \--- androidx.annotation:annotation-jvm:1.7.0-beta01
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 -> 1.8.20
            |              +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.8.20
            |              \--- org.jetbrains:annotations:13.0
            +--- androidx.test.services:storage:1.5.0
            |    +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |    \--- androidx.test:monitor:1.7.0 -> 1.7.2
            |         +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |         +--- androidx.tracing:tracing:1.1.0 FAILED
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.8.20 (*)
            +--- androidx.test:monitor:1.7.2 (*)
            +--- androidx.tracing:tracing:1.1.0 FAILED
            \--- junit:junit:4.13.2
                \--- org.hamcrest:hamcrest-core:1.3

        compileOnly - Compile-only dependencies for the 'main' feature. (n)
        No dependencies

        compileOnlyApi - Compile-only API dependencies for the 'main' feature. (n)
        No dependencies

        default - Configuration for default artifacts. (n)
        No dependencies

        implementation - Implementation dependencies for the 'main' feature. (n)
        \--- androidx.test:runner:1.6.2 (n)

        mainSourceElements - List of source directories contained in the Main SourceSet. (n)
        No dependencies

        runtimeClasspath - Runtime classpath of source set 'main'.
        \--- androidx.test:runner:1.6.2
            +--- androidx.annotation:annotation:1.7.0-beta01
            |    \--- androidx.annotation:annotation-jvm:1.7.0-beta01
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 -> 1.8.20
            |              +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.8.20
            |              \--- org.jetbrains:annotations:13.0
            +--- androidx.test.services:storage:1.5.0
            |    +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |    \--- androidx.test:monitor:1.7.0 -> 1.7.2
            |         +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |         +--- androidx.tracing:tracing:1.1.0 FAILED
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.8.20 (*)
            +--- androidx.test:monitor:1.7.2 (*)
            +--- androidx.tracing:tracing:1.1.0 FAILED
            \--- junit:junit:4.13.2
                \--- org.hamcrest:hamcrest-core:1.3

        runtimeElements - Runtime elements for the 'main' feature. (n)
        No dependencies

        runtimeOnly - Runtime-only dependencies for the 'main' feature. (n)
        No dependencies

        testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
        No dependencies

        testCompileClasspath - Compile classpath for source set 'test'.
        \--- androidx.test:runner:1.6.2
            +--- androidx.annotation:annotation:1.7.0-beta01
            |    \--- androidx.annotation:annotation-jvm:1.7.0-beta01
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 -> 1.8.20
            |              +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.8.20
            |              \--- org.jetbrains:annotations:13.0
            +--- androidx.test.services:storage:1.5.0
            |    +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |    \--- androidx.test:monitor:1.7.0 -> 1.7.2
            |         +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |         +--- androidx.tracing:tracing:1.1.0 FAILED
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.8.20 (*)
            +--- androidx.test:monitor:1.7.2 (*)
            +--- androidx.tracing:tracing:1.1.0 FAILED
            \--- junit:junit:4.13.2
                \--- org.hamcrest:hamcrest-core:1.3

        testCompileOnly - Compile only dependencies for source set 'test'. (n)
        No dependencies

        testImplementation - Implementation only dependencies for source set 'test'. (n)
        No dependencies

        testRuntimeClasspath - Runtime classpath of source set 'test'.
        \--- androidx.test:runner:1.6.2
            +--- androidx.annotation:annotation:1.7.0-beta01
            |    \--- androidx.annotation:annotation-jvm:1.7.0-beta01
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 -> 1.8.20
            |              +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.8.20
            |              \--- org.jetbrains:annotations:13.0
            +--- androidx.test.services:storage:1.5.0
            |    +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |    \--- androidx.test:monitor:1.7.0 -> 1.7.2
            |         +--- androidx.annotation:annotation:1.7.0-beta01 (*)
            |         +--- androidx.tracing:tracing:1.1.0 FAILED
            |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.8.20 (*)
            +--- androidx.test:monitor:1.7.2 (*)
            +--- androidx.tracing:tracing:1.1.0 FAILED
            \--- junit:junit:4.13.2
                \--- org.hamcrest:hamcrest-core:1.3

        testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
        No dependencies

        (*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependency subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.

        (n) - A dependency or dependency configuration that cannot be resolved.

        A web-based, searchable dependency report is available by adding the --scan option.

        BUILD SUCCESSFUL in 666ms
        1 actionable task: 1 executed


java gradle androidx-test
1个回答
0
投票

这些 AndroidX 库并不真正适合在常见的 Java 项目中使用,
因为一般来说,

androidx.test
仅仅属于
androidTestImplementation
,
这意味着它最终取决于 AGP 来创建源集
androidTest

上面写着:

+--- androidx.tracing:tracing:1.1.0 FAILED

尝试提供较新的版本,类似:

androidTestImplementation "androidx.tracing:tracing:1.2.0"

虽然请求的版本

1.1.0
可以在存储库中找到
google()

https://mvnrepository.com/artifact/androidx.tracing/tracing

© www.soinside.com 2019 - 2024. All rights reserved.