android / codelab-android-hilt

Apache License 2.0
350 stars 186 forks source link

Getting NoClassDefFoundError after introducing AndroidEntryPoint #8

Closed titovnick closed 3 years ago

titovnick commented 3 years ago

Hi,

I started migrating our multi-modular application written with Dagger 2 + Dagger Android to Hilt, but faced one issue. I migrated Application class, put aliases for scopes, added aggregation module. Everything compiles and looks fine. Then I started to migrate modules and after adding first AndroidEntryPoint annotation to activity started getting

java.lang.NoClassDefFoundError: Failed resolution of: Lsome/package/location/LocationOwner;
       at some.package.DaggerMyApplication_HiltComponents_SingletonC.builder(DaggerMyApplication_HiltComponents_SingletonC.java:1847)
       at some.package.Hilt_MyApplication$1.get(Hilt_MyApplication.java:22)
       at dagger.hilt.android.internal.managers.ApplicationComponentManager.generatedComponent(ApplicationComponentManager.java:40)
       at some.package.Hilt_MyApplication.generatedComponent(Hilt_MyApplication.java:35)
       at dagger.hilt.EntryPoints.get(EntryPoints.java:46)
       at some.package.MyApplication$appInjector$2.invoke(MyApplication.kt:53)
       at some.package.MyApplication$appInjector$2.invoke(MyApplication.kt:29)
       at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
       at some.package.MyApplication.getAppInjector(Unknown Source:2)
       at some.package.MyApplication.attachBaseContext(MyApplication.kt:64)
       at android.app.Application.attach(Application.java:351)
       at android.app.Instrumentation.newApplication(Instrumentation.java:1149)
       at android.app.LoadedApk.makeApplication(LoadedApk.java:1218)
       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431)
       at android.app.ActivityThread.access$1300(ActivityThread.java:219)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7356)
       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:930)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "some.package.location.LocationOwner" on path: DexPathList[[zip file "/data/app/some.package.ca.debug-hgB-s5vfiqRKdcnrQtd2qQ==/base.apk"],nativeLibraryDirectories=[/data/app/some.package.ca.debug-hgB-s5vfiqRKdcnrQtd2qQ==/lib/x86, /data/app/some.package.ca.debug-hgB-s5vfiqRKdcnrQtd2qQ==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
       at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)

LocationOwner is just an interface that some Activites extends, not even the one that I am currently migrating. If I remove it, it will start complaining on other interfaces inside the app with the same ClassNotFoundException.

Haven't found anything related in the documentation, have no idea why it is happening.

Will appreciate any help, thanks!

manuelvicnt commented 3 years ago

It seems like there's a problem with Dex files. Are you using multidex in your app? I assume that you have the LocationOwner as a transitive dependency of the app module, right? You're importing the other module as api and not implementation.

titovnick commented 3 years ago

Thanks for your answer! This particular interface live in the API module and as far as I can see it is being imported with implementation project(':api'). We do use Multidex, but as minSdk is 21, we do not use support multidex.

manuelvicnt commented 3 years ago

That might be the issue, try with api project(':api')

AGP doesn't give Hilt or Dagger enough information for the annotation processor so api needs to be used. This is currently under investigation and the team plans to improve it. Was implementation working with Dagger?

titovnick commented 3 years ago

Yes, it works for dagger right now. Will try with 'api' and will write you back

titovnick commented 3 years ago

Okay, with api project(':module') it works, but had to replace two or three in order to make it work. Why do you think it works for Dagger but not for Hilt? Thanks for your help, will never figure it out myself.

manuelvicnt commented 3 years ago

I'm surprised that it worked for Dagger, to be honest. Not sure if it wasn't picking up the right implementation or what but we've seen problems like it in the past with Dagger.

This is a known problem that the team is trying to fix as using api in these cases is not a good practice. Hope it gets fixed in the upcoming released. Stay tuned :)