Android unable to instantiate application ClassNotFoundException Hilt

Viewed 59

SOVLED

I had to take the migration steps from the Dagger 2.34 release note and remove the old androidx.hilt:hilt-lifecycle-viewmodel dependency. https://github.com/google/dagger/releases/tag/dagger-2.34

Old post:

I am working on an application using hilt dagger. It crashes and i dont know why. Checked a lot of topics on the internet but none of them helped me.

Stack trace

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.runcontrol, PID: 27112
java.lang.RuntimeException: Unable to instantiate application com.example.runcontrol.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.runcontrol.MyApplication" on path: DexPathList[[zip file "/data/app/~~Xr0Z9AQ4bYhnwaCpKBE8fg==/com.example.runcontrol-5mHfaI69wWEPqxwnjaC1fg==/base.apk"],nativeLibraryDirectories=[/data/app/~~Xr0Z9AQ4bYhnwaCpKBE8fg==/com.example.runcontrol-5mHfaI69wWEPqxwnjaC1fg==/lib/arm64, /system/lib64, /system/system_ext/lib64, /system/product/lib64]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:1248)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6964)
    at android.app.ActivityThread.access$1500(ActivityThread.java:258)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1983)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8061)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.runcontrol.MyApplication" on path: DexPathList[[zip file "/data/app/~~Xr0Z9AQ4bYhnwaCpKBE8fg==/com.example.runcontrol-5mHfaI69wWEPqxwnjaC1fg==/base.apk"],nativeLibraryDirectories=[/data/app/~~Xr0Z9AQ4bYhnwaCpKBE8fg==/com.example.runcontrol-5mHfaI69wWEPqxwnjaC1fg==/lib/arm64, /system/lib64, /system/system_ext/lib64, /system/product/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
    at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
    at android.app.Instrumentation.newApplication(Instrumentation.java:1159)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:1240)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6964) 
    at android.app.ActivityThread.access$1500(ActivityThread.java:258) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1983) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:236) 
    at android.app.ActivityThread.main(ActivityThread.java:8061) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
    Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/runcontrol/Hilt_MyApplication;
    at java.lang.VMClassLoader.findLoadedClass(Native Method)
    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
            ... 14 more
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.runcontrol.Hilt_MyApplication" on path: DexPathList[[zip file "/data/app/~~Xr0Z9AQ4bYhnwaCpKBE8fg==/com.example.runcontrol-5mHfaI69wWEPqxwnjaC1fg==/base.apk"],nativeLibraryDirectories=[/data/app/~~Xr0Z9AQ4bYhnwaCpKBE8fg==/com.example.runcontrol-5mHfaI69wWEPqxwnjaC1fg==/lib/arm64, /system/lib64, /system/system_ext/lib64, /system/product/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            ... 17 more
 D/OOMEventManagerFK: checkEventAndDumpForJE: 0
 I/Process: Sending signal. PID: 27112 SIG: 9
 Disconnected from the target VM, address: 'localhost:34313', transport: 'socket'

Manifest

<application
    android:allowBackup="true"
    android:name=".MyApplication"

MyApplication.kt

package com.example.runcontrol

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MyApplication: Application() {
}

When I remove @HiltAndroidApp here, it works fine until my app should run foreground service annotated with @AndroidEntryPoint which uses @Inject.

My build.gradle project

plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
id 'androidx.navigation.safeargs.kotlin' version '2.4.1' apply false
id 'com.google.dagger.hilt.android' version '2.43.2' apply false
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
}

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