找不到 Gradle DSL 方法:'androidTestImplementation()'



我正在尝试使用uiautomator,所以在这里查看教程 https://developer.android.com/training/testing/ui-testing/uiautomator-testing#java

在教程中,它说:

在 Android 应用模块的 build.gradle 文件中,您必须设置对 UI 自动操作库的依赖项引用:

dependencies {
...
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}

所以我添加了那行,所以我的 build.gradle 文件是这样的:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//        androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
//        androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
//        androidTestImplementation('androidx.test.uiautomator:uiautomator:2.2.0')
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

我尝试运行该应用程序。但是我得到了一个错误:

Gradle DSL method not found: 'androidTestImplementation()'
Possible causes:
The project 'My Application' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 4.0.2 and sync project
The project 'My Application' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file
The build file may be missing a Gradle plugin.
Apply Gradle plugin

您使用了错误的build.gradle文件和错误的块。

您必须将androidTestImplementation从顶级文件移动到dependencies块中的module/build.gradle文件(而不是buildscript块中的dependencies):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
//...
}
dependencies {
//...
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}

将依赖项实现移动到应用级build.gradle文件。 你已经把它们放在项目级别的build.gradle文件中。

相关内容

  • 没有找到相关文章

最新更新