[FIXED] Gradle DSL method not found: 'androidTestImplementation()'

Issue

I’m trying to use uiautomator so looked the tutorial here https://developer.android.com/training/testing/ui-testing/uiautomator-testing#java

In the tutorial, it says:

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'
}

So I added that line, so my build.gradle files is like this:

// 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
}

And I tried to run the app. But I got an error:

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

Solution

You are using the wrong build.gradle file and the wrong block.

You have to move the androidTestImplementation from the top-level file to the module/build.gradle file in the dependencies block (not the dependencies in the buildscript block)::

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

   android {
    //...
   }

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

Answered By – Gabriele Mariotti

Answer Checked By – David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published