javaandroidbuild.gradleclassnotfoundexceptionnoclassdeffounderror

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/view/MenuHost;


I'm making a google sign in process in my app.

Whenever I open my app it crashes and gives NoClassDefFoundError

I checked the logcat and found this:-

    --------- beginning of crash
2022-05-13 18:59:08.881 3810-3810/com.java.test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.java.test, PID: 3810
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/view/MenuHost;
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1244)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3183)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.view.MenuHost" on path: DexPathList[[zip file "/data/app/com.java.test-NhlLILW0268bo0W5tj1Gxw==/base.apk"],nativeLibraryDirectories=[/data/app/com.java.test-NhlLILW0268bo0W5tj1Gxw==/lib/arm64, /system/lib64, /system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) 
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1244) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3183) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7397) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935) 

This is build.gradle(module) :-

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        configurations.all {
            resolutionStrategy {
                force 'androidx.core:core:1.6.0'
            }
        }
        applicationId "com.java.test"
        compileSdkVersion 31
        minSdkVersion 28
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.gms:play-services-auth:20.2.0'
    implementation 'com.google.firebase:firebase-auth:21.0.4'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Why this error is occurring? Please help me!


Solution

  • I found the problem while debugging.

    The problem was in andoidx.appcompat class not found

    In app build.gradle, I changed this line of dependencies:

    implementation 'androidx.appcompat:appcompat:1.4.1'
    

    To:

    implementation 'androidx.appcompat:appcompat:1.3.1'
    

    Maybe the newer version has changed the classpath.