首页 > 解决方案 > 找不到可选库:android.test.runner

问题描述

我正在尝试遵循 android 开发人员文档的Set up project for AndroidX Test教程(在此处找到),但出现错误unable to find optional library: android.test.runner

这是我的 Gradle 文件:

...

android {

    ...

    defaultConfig {
        ...

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    ...

    useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'

}

...

dependencies {
    ...

    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:core:1.0.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
    androidTestImplementation 'androidx.test.ext:junit:1.0.0'
    androidTestImplementation 'androidx.test.ext:truth:1.0.0'
    androidTestImplementation 'com.google.truth:truth:0.42'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}

正如你所看到的,我有android.test.runner我的依赖项,但是当我useLibrary 'android.test.runner'在我的 android 块中添加该行时,我得到它找不到那个库的错误......

我的 Gradle 文件有问题吗?我正在按照教程中概述的说明进行操作,所以我不知道我可能会错过什么......

标签: androidtestinggradleandroid-gradle-pluginandroid-instrumentation

解决方案


我只是没有添加这些无用的行,即使在官方指南中它仍然有效。

但是如果你将旧版本迁移到 androidx,请记住你必须将编译版本更改为 28,否则会出现一些错误


推荐阅读