Android Studio: Unable to load class 'com.goog

2019-08-21 01:34发布

问题:

When i try opening my projects on Android Studio 3.1.2, I get this Gradle Project Sync failed error with the message below

Unable to load class 'com.google.common.base.Preconditions'. Possible causes for this unexpected error include:

  • Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network)
  • 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)
  • 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.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

gradle-wrapper.properties file

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

build.gradle file

buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
repositories {
    mavenCentral()
}
dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

回答1:

The problem has been solved.

I deleted the .gradle folder in my home user directory which made Android Studio re-download all the files for the current version of gradle. It took quite a while to download and sync the first time but it has solved the problem.

Thank you very much for your assistance @Luis Henriques



回答2:

Clean and rebuild your project;

Check if you are working on offline mode:

File > Settings > Search for "offline" > uncheck "offline work"

After unchecking offline work, clean and rebuild again.

If it doesn't work, my guess is exactly what it says:

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.

Let me know if it worked.


UPDATE:

Why do you have:

repositories {
    mavenCentral()
}
...
dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

Instead of:

buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
        ...

Also use "implementation" instead of "compile". So:

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

And why did you set these up in your project.gradle instead of in your app.gradle?

I think your gradle file is poorly structured. Hence the problem. Why don't you see how the default gradle files are set up and compare them with yours?

Hope this helps.



回答3:

Your local gradle distribution my be corrupt. Delete it and redownload it.