google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

[Dagger-Hilt] Error when applying obfuscation other than Proguard (Allatori) #4266

Open jeudi93 opened 8 months ago

jeudi93 commented 8 months ago

My project does not apply Proguard obfuscation, but uses the paid obfuscation library 'allatori'. https://allatori.com/

When obfuscation is applied using the 'allatori' library, a crash error occurs saying that the Application Class cannot be found as shown below.

E FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 29344
java.lang.RuntimeException: Unable to instantiate application com.example.myapplication.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.myapplication.MyApplication" on path: DexPathList[[dex file "/data/ data/com.example.myapplication/code_cache/.overlay/base.apk/classes3.dex", zip file "/data/app/~~mQrlQmr7Qn7zEWzSzORolw==/com.example.myapplication-0AZUonxkcl235IRbMJg4Gg==/base.apk" ],nativeLibraryDirectories=[/data/app/~~mQrlQmr7Qn7zEWzSzORolw==/com.example.myapplication-0AZUonxkcl235IRbMJg4Gg==/lib/arm64, /system/lib64]]
at android.app.LoadedApk.makeApplication(LoadedApk.java:1332)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7532)
at android.app.ActivityThread.access$1500(ActivityThread.java:301)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myapplication.MyApplication" on path: DexPathList[[dex file "/data/data/com.example.myapplication/code_cache/.overlay/base. apk/classes3.dex", zip file "/data/app/~~mQrlQmr7Qn7zEWzSzORolw==/com.example.myapplication-0AZUonxkcl235IRbMJg4Gg==/base.apk"],nativeLibraryDirectories=[/data/app/~~mQrlQmr7Qn7zEWzSzORolw== /com.example.myapplication-0AZUonxkcl235IRbMJg4Gg==/lib/arm64, /system/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:1158)
at android.app.LoadedApk.makeApplication(LoadedApk.java:1324)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7532)
at android.app.ActivityThread.access$1500(ActivityThread.java:301)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/myapplication/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.myapplication.Hilt_MyApplication" on path: DexPathList[[dex file "/data/data/com.example.myapplication/code_cache/.overlay/base. apk/classes3.dex", zip file "/data/app/~~mQrlQmr7Qn7zEWzSzORolw==/com.example.myapplication-0AZUonxkcl235IRbMJg4Gg==/base.apk"],nativeLibraryDirectories=[/data/app/~~mQrlQmr7Qn7zEWzSzORolw== /com.example.myapplication-0AZUonxkcl235IRbMJg4Gg==/lib/arm64, /system/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
[CIRCULAR REFERENCE:java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/myapplication/Hilt_MyApplication;]
  1. When applying obfuscation other than Proguard, Could you share the configuration that need to be set or keep the auto generated classes from the obfuscation?

  2. My Project Dagger-Hilt Dependencies . implementation("com.google.dagger:hilt-android:2.48") kapt("com.google.dagger:hilt-android-compiler:2.48")

Chang-Eric commented 8 months ago

I think you may need to seek help from Allatori as we're not familiar with that obfuscator. Also, right now, we only really support usage with R8 and Proguard. Note that we do package in some configuration for those where needed (like with @HiltViewModel), so please be aware that if Allatori doesn't read that configuration you'll need to replicate those rules for Allatori yourself too.

jeudi93 commented 8 months ago

@Chang-Eric I already checked with Allatori side and they told me to keep all dagger-hilt generated classes from obfuscation. The problem is, I am struggling to find out what classes were auto generated. Could you possibly guide me how to find out all those classes so that I will be able to keep it from obfuscation?

Chang-Eric commented 7 months ago

Er, this is an odd request as that really isn't necessary for other obfuscators like R8 and Proguard since generated classes should be treated the same as user written classes. The main way to find all of them though would be to find the classes we put the @Generated annotation on, but that is source retention so I don't know if you'll be able to use that.

junit74 commented 1 month ago

I resolved the same issue by adding 'hilt { enableAggregatingTask = false }' to build.gradle."