Gradle DSL 方法未找到:'androidTestImplementation()'。

4
我正在尝试使用uiautomator,查看了这里的教程 https://developer.android.com/training/testing/ui-testing/uiautomator-testing#java 在教程中,它说:

在你的Android应用模块的build.gradle文件中,你必须设置对UI Automator库的依赖引用:

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
2个回答

3
你正在使用错误的 build.gradle 文件和错误的块。
你需要将 androidTestImplementation 从顶级文件移动到 module/build.gradle 文件中的 dependencies 块(而不是 buildscript 块中的 dependencies):
   apply plugin: 'com.android.application'
   apply plugin: 'kotlin-android'

   android {
    //...
   }

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接