'Could not find method androidTestCompile() for arguments on project ':app' of type org.gradle.api.Project

Today I installed Android Studio and after opening the program I see this error in the messages Gradle sync part ->


Error:(31, 0) Could not find method androidTestCompile() for arguments [com.android.support.test.espresso:espresso-contrib:2.2.2, build_4zwzygftganlohhaghm2dsums$_run_closure3@7658f1d6] on project ':app' of type org.gradle.api.Project.
<a href="openFile:C:\Users\Dany\AndroidStudioProjects\MyApplication3\app\build.gradle">Open File</a>

and my gradle-module has this code in it ->


apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.dany.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
}
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'appcompat-v7'
}androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'appcompat-v7'
}


Solution 1:[1]

Replacing androidTestCompile with androidTestImplementation in the dependencies block worked for me.

Solution 2:[2]

Call androidTestCompile within the dependencies block. In addition, remove the duplicated versions of espresso-contrib:2.2.2.

Rectified build.gradle

dependencies 
{
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', 
    {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2')
    {
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'appcompat-v7'
   })
    compile 'com.android.support:appcompat-v7:25.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
}

Solution 3:[3]

Using this solved my error

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0',
            {
                exclude group: 'com.android.support', module: 'support-annotations'
            })
    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2')
            {
                exclude module: 'support-annotations'
                exclude module: 'support-v4'
                exclude module: 'support-v13'
                exclude module: 'recyclerview-v7'
                exclude module: 'appcompat-v7'
            }

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
}

Solution 4:[4]

I had the same issue, I solved it with replacing "androidTestCompile" with "testInstrumentationRunner"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glenn Mascarenhas
Solution 2 Zoe stands with Ukraine
Solution 3 Vinay John
Solution 4 Ahmed KHABER