'Unable to sync Gradle with Firebase Crashlytic 2.6.0 using Android Studio 4.2

Project-level build.gradle

buildscript {
    addRepos(repositories)
    dependencies {

        classpath 'com.google.gms:google-services:4.3.5'

        // Crashlytic
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.6.0'

    }
}

App-level build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

dependencies {

    // Crashlytic
    implementation 'com.google.firebase:firebase-crashlytics:18.0.0'

}

With gradle 4.3.5 and Firebase crashlytic 2.6.0 My app stop compiling with the error below

class org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar$2 cannot be cast to class org.gradle.api.file.Directory (org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar$2 and org.gradle.api.file.Directory are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @68be2bc2)
class org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar$2 cannot be cast to class org.gradle.api.file.Directory (org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar$2 and org.gradle.api.file.Directory are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @68be2bc2)

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.

But everything works if I change Firebase crashlytic version to 2.3.0

Do you guys have any idea why is this happen?



Solution 1:[1]

UPDATED ANSWER:

The reason for the compiling issue is that version of Gradle and Crashlytics Gradle plug-in is incompatible.

As described here, starting from Crashlytics Gradle plugin version 2.5.0:

The Crashlytics Gradle plugin is compatible with Gradle v5.6.4+ and the Android Gradle plugin v3.4.2+. Support for previous Gradle and Android Gradle plugin versions have been removed.

So, the Solution is to use the latest Gradle and firebase-crashlytics-gradle versions. Thanks to @Mher for describing the compatible up-to-date versions here. Follow his answer for updating versions. Don't forget to change compileOptions in app level build.gradle as:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

Warning: My old answer below is a workaround to fix the Gradle and Crashlytics compatibility for those who are not ready to update the Gradle version. The proper solution is to update the Gradle version. In the old answer below, the mentioned firebase-crashlytics-gradle:2.4.1 was buggy for me and it was not able to upload mapping files to the Firebase console which deobfuscates the crash reports. So, try to follow the above-updated answer instead of downgrading the Crashlytics Gradle version.

OLD ANSER FOR WHO CANNOT UPDATE THE GRADLE VERSION:

I hit the exact same issue. Seems the latest crashlytics-gradle plugin is incompatible with something. Using the lower version of crashlytics-gradle plugin helped. Instead of firebase-crashlytics-gradle:2.6.0 Try to use this version:

classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'

That worked for me... If that doesn't work for you, you can maybe try even lower versions. A list of versions can be found here:

https://firebase.google.com/support/release-notes/android#crashlytics_gradle_plugin_v2-0-0

When you search for "Crashlytics Gradle plugin version", you can see all released versions for Crashlytics Gradle plugin.

I also had to change compileOptions in app level build.gradle as:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

When target and source compatibility was not set to 1_8, project compiled but was crashing when trying to run the app:

    --------- beginning of crash
2021-06-09 17:22:28.765 29519-29519/com.resmed.myair.cpl E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.resmed.myair.cpl, PID: 29519
    java.lang.BootstrapMethodError: Exception from call site #45 bootstrap method
        at com.google.firebase.components.ComponentDiscovery.discoverLazy(ComponentDiscovery.java:112)
        at com.google.firebase.FirebaseApp.<init>(FirebaseApp.java:418)
        at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:299)
        at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:267)
        at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:252)
        at com.google.firebase.provider.FirebaseInitProvider.onCreate(FirebaseInitProvider.java:51)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(FirebaseInitProvider.java:45)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NoClassDefFoundError: Invalid descriptor: minimize.
        at com.google.firebase.components.ComponentDiscovery.discoverLazy(ComponentDiscovery.java:112) 
        at com.google.firebase.FirebaseApp.<init>(FirebaseApp.java:418) 
        at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:299) 
        at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:267) 
        at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:252) 
        at com.google.firebase.provider.FirebaseInitProvider.onCreate(FirebaseInitProvider.java:51) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1917) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1892) 
        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(FirebaseInitProvider.java:45) 
        at android.app.ActivityThread.installProvider(ActivityThread.java:6239) 
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722) 
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494)

Solution 2:[2]

In my case

classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'

solved the error but crashlytics didn't send errors to server.

I changed versions in \android\build.gradle to

classpath 'com.android.tools.build:gradle:4.2.0'
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'

and in \android\gradle\wrapper\gradle-wrapper.properties to

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

and now everything works just fine.

Solution 3:[3]

I had to switch from:

classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'

to

classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.0'

and then it worked. Seems like some new versions of the library aren't necessarily compatible with gradle.

Just try going down a few versions and build again.

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 Rohit Singh
Solution 2 Mher Aghabalyan
Solution 3 Dominik