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

标签 android android-studio android-gradle-plugin android-testing

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

在教程中,它说:

In the build.gradle file of your Android app module, you must set a dependency reference to the UI Automator library:

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 文件和错误的 block 。

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

   apply plugin: 'com.android.application'
   apply plugin: 'kotlin-android'

   android {
    //...
   }

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

关于android - 找不到 Gradle DSL 方法 : 'androidTestImplementation()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64328543/

相关文章:

android - 如何从 ScrollView 中的编辑文本中获取文本?

android - 添加一个按钮以查看

android - 如何从所有依赖项中排除模块但保留显式声明 Gradle

android - 安全异常 : Permission Denial: reading (only on emulator)

java - 在android中将资源读取为InputStream

安卓工作室 - "waiting for target device to come online"

java - 错误 : The Android Gradle plugin supports only Kotlin Gradle plugin version 1. 3.0 及更高版本

Android Studio 没有在设备上安装应用程序,没有错误消息

java - 在Android Studio中构建多种口味时,找不到与包名称匹配的客户端

Android studio/Gradle javadoc 任务