65

When i am trying to compile a imported project from github, my gradle build always fails with the following exeption.

Unable to find method 'org.gradle.api.tasks.testing.Test.getTestClassesDirs()Lorg/gradle/api/file/FileCollection;'
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)</li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)</li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes

I followed the instructions given but it didn't work out. Additionally i couldn't get any further information about the error by searching it in the internet

app\build.gradle:

   apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "iammert.com.androidarchitecture"
        minSdkVersion 21
        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'
        }
    }

    dataBinding {
        enabled = true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    /*androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })*/
    //  testCompile 'junit:junit:4.12'

    //support lib
    implementation rootProject.ext.supportLibAppCompat
    implementation rootProject.ext.supportLibDesign

    implementation rootProject.ext.archRuntime
    implementation rootProject.ext.archExtension
    annotationProcessor rootProject.ext.archCompiler

    implementation rootProject.ext.roomRuntime
    annotationProcessor rootProject.ext.roomCompiler

    implementation rootProject.ext.okhttp
    implementation rootProject.ext.retrofit
    implementation rootProject.ext.gsonConverter

    //dagger
    annotationProcessor rootProject.ext.daggerCompiler
    implementation rootProject.ext.dagger
    implementation rootProject.ext.daggerAndroid
    implementation rootProject.ext.daggerAndroidSupport
    annotationProcessor rootProject.ext.daggerAndroidProcessor

    //ui
    implementation rootProject.ext.picasso

}

\build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext {
    supportLibVersion = '25.3.1'
    daggerVersion = '2.11'
    retrofitVersion = '2.1.0'
    gsonConverterVersion = '2.1.0'
    okhttpVersion = '3.8.0'
    picassoVersion = '2.5.2'
    archVersion = '1.0.0-alpha1'

    supportLibAppCompat = "com.android.support:appcompat-v7:$supportLibVersion"
    supportLibDesign = "com.android.support:design:$supportLibVersion"
    archRuntime = "android.arch.lifecycle:runtime:$archVersion"
    archExtension = "android.arch.lifecycle:extensions:$archVersion"
    archCompiler = "android.arch.lifecycle:compiler:$archVersion"
    roomRuntime = "android.arch.persistence.room:runtime:$archVersion"
    roomCompiler = "android.arch.persistence.room:compiler:$archVersion"
    dagger = "com.google.dagger:dagger:$daggerVersion"
    daggerCompiler = "com.google.dagger:dagger-compiler:$daggerVersion"
    daggerAndroid = "com.google.dagger:dagger-android:$daggerVersion"
    daggerAndroidSupport = "com.google.dagger:dagger-android-support:$daggerVersion"
    daggerAndroidProcessor = "com.google.dagger:dagger-android-processor:$daggerVersion"
    retrofit = "com.squareup.retrofit2:retrofit:$retrofitVersion"
    gsonConverter = "com.squareup.retrofit2:converter-gson:$gsonConverterVersion"
    okhttp = "com.squareup.okhttp3:okhttp:$okhttpVersion"
    picasso = "com.squareup.picasso:picasso:$picassoVersion"
}

gradle\gradle-wrapper.properties:

#Thu May 18 17:31:31 EEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip

Does anyone know why this error occures?

Yannick
  • 4,833
  • 8
  • 38
  • 63

11 Answers11

115

I had to change the distributionUrl in the gradle-wrapper.properties to distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip to get the build running again. This seems to be a similar problem Gradle sync failed: Unable to find method.

Lukas Thum
  • 1,294
  • 1
  • 8
  • 12
  • 2
    I had the same problem. Non of the answers worked for me. Finally downgraded to previous version and It's working! I guess it's a bug related to latest Gradle versions. – Siamak Nov 19 '18 at 14:07
  • 1
    What does it do? – c-an Aug 25 '20 at 07:48
  • Yes, this worked for me, but just one more tip: If you have any other Android projects that do build, take a look at the gradle-wrapper.properties for them and then use the same distributionUrl to fix your broken project. – Anthony. Sep 20 '20 at 03:35
  • 2
    for me downgrading from 7.1 to 6.9 worked. I have no explanation. – fatCop Aug 03 '21 at 12:06
9

I fix same as this error by add classpath 'com.android.tools.build:gradle:3.2.1' in the build.gradle project

buildscript {

    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
6

If you are getting this error after deleting .gradle and .idea files then the error is happening because the Gradle files are not available for this project and Android studio is unable to download the same version of the Gradle dependency. You can switch to a lower version of the Gradle choose from here and then go to gradle-wrapper-properties file and change the version, in my case I changed it from distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip to distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip. After downloading the older version of the Gradle dependency a Gradle update dialog box would prompt through which you can get the latest version back which is 5.1.1 in this case.

Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
  • 2
    At this day, the version to use is 4.10.1 : `distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip` – Sebastien H. Aug 13 '20 at 09:10
6

Error: Unable to find method ''java.lang.String org.gradle.api.artifacts.result.ComponentSelectionReason.getDescription()''

I faced the same issue while using spring-boot version 2.5.4 in IntelliJ 2019.3.5.

Findings:

What was the culprit: May be gradle or may intelliJ.

What was Fix: by default gradle-wrapper.properties had the following line distributionUrl=https://services.gradle.org/distributions/gradle-7.1.1-bin.zip

I replaced this line with following line: distributionUrl=https://services.gradle.org/distributions/gradle-6.8.3-bin.zip

It started working.

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
JITU
  • 91
  • 1
  • 3
3

In the file SampleProject/gradle/wrapper/gradle-wrapper.properties

change

distributionUrl=https://services.gradle.org/distributions/gradle-5.1.1-all.zip

to

distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
2

change on gradle.wrapper.properties your value of distributionUrl=https://services.gradle.org/distributions/gradle-4.10.1-all.zip change to this and android studio will give you automatically update on your gradle and it should be fine

1

when you import a project from external resources some files will be generated as it contains information specific to your local configuration, and one of them is gradle-wrapper.properties, downgrade to a lower version and rebuild the project and the error should be gone

distributionUrl=https://services.gradle.org/distributions/gradle-5.1.1-all.zip

to

distributionUrl=https://services.gradle.org/distributions/gradle-4.4-all.zip
Mohamed Dernoun
  • 776
  • 5
  • 13
0

Looks like you are failing to resolve your testInstrumentation. Are you using facebook.screenshot api? if so you need to include it.

Otherwise, just comment out the testInstrument section and see if you can build without the tests for now if you don't care about them. otherwise you need to fix your dependency usage of it.

*comment out testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

This causes a line to get created in your manifest and it is probably not resolving properly.

Sam
  • 5,342
  • 1
  • 23
  • 39
0

for android studio path "folder project/gradle/wrapper/gradle-wrapper.properties" file change URL value distributionUrl=https://services.gradle.org/distributions/gradle-4.10.1-all.zip apropriate (previous) version from http://services.gradle.org/distributions/

vitalinvent
  • 433
  • 1
  • 5
  • 11
0

i faced same issue double check your distributionUrl should support with your classpath otherwise you getting error

Build.gradle (Android)

 classpath 'com.android.tools.build:gradle:3.6.1'
 classpath 'com.google.gms:google-services:4.3.3'

Gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
Balaji
  • 9,657
  • 5
  • 47
  • 47
-1

Working fine with the following line with AndroidStudio 3.5

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52