LouisCAD / Splitties

A collection of hand-crafted extensions for your Kotlin projects.
https://splitties.louiscad.com
Apache License 2.0
2.52k stars 158 forks source link

Weird NoClassDefFoundError: kotlin.collections.EmptyList under api 21 #278

Closed RavioliWonton closed 3 years ago

RavioliWonton commented 3 years ago

After upgraded to Kotlin 1.5.21, I came into this error with com.louiscad.splitties:splitties-appctx:3.0.0-beta02:

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: androidx.startup.StartupException: java.lang.NoClassDefFoundError: kotlin.collections.EmptyList at android.app.ActivityThread.installProvider(ActivityThread.java:4563) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132) at android.app.ActivityThread.access$1300(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: androidx.startup.StartupException: java.lang.NoClassDefFoundError: kotlin.collections.EmptyList at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:162) at androidx.startup.AppInitializer.discoverAndInitialize(AppInitializer.java:198) at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:38) at android.content.ContentProvider.attachInfo(ContentProvider.java:1058) at android.app.ActivityThread.installProvider(ActivityThread.java:4560) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132)  at android.app.ActivityThread.access$1300(ActivityThread.java:130)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:4745)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.NoClassDefFoundError: kotlin.collections.EmptyList at kotlin.collections.CollectionsKt__CollectionsKt.emptyList(Collections.kt:71) at splitties.init.AppCtxInitializer.dependencies(AppCtxInitializer.kt:30) at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:143) at androidx.startup.AppInitializer.discoverAndInitialize(AppInitializer.java:198)  at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:38)  at android.content.ContentProvider.attachInfo(ContentProvider.java:1058)  at android.app.ActivityThread.installProvider(ActivityThread.java:4560)  at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132)  at android.app.ActivityThread.access$1300(ActivityThread.java:130)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:4745)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  at dalvik.system.NativeStart.main(Native Method) 

RavioliWonton commented 3 years ago

I believe there is something weird about multidex with kotlin. It's not work by configuring multiDexKeepFile. I will keep tracking this issue and close this for it's not Splitties's fault.

RavioliWonton commented 3 years ago

I understand what happened. Since migrating to androidx.startup, I cannot use appCtx in Application's static code block or companion object in kotlin(which is initialized through Application's construction, this instance is not accessible refer to document), there is a reproduce project. If this should be an intended behaviour, please close this, or I can issue a bug report to google. Reopen the issue.

LouisCAD commented 3 years ago

You can workaround by calling context?.injectAsAppCtx() early in an init block in your Application class.

RavioliWonton commented 3 years ago

context?.injectAsAppCtx()

Thanks for the reply, but it's not working in initialization of Application class. Inject into applicationContext will get NullPointerException, and inject into baseContext will get the same error of NoClassDefFoundError.

LouisCAD commented 3 years ago

Can you make a small reproducing project?

RavioliWonton commented 3 years ago

Can you make a small reproducing project?

Sure, it's here.

LouisCAD commented 3 years ago

The project you shared doesn't reproduce the bug it seems. The app doesn't crash on launch, and I see no relevant logs.

RavioliWonton commented 3 years ago

The project you shared doesn't reproduce the bug it seems. The app doesn't crash on launch, and I see no relevant logs.

I'm sorry I forget to mention that this situation will only appear on devices under api 21, which need MultiDex to move past 65K limitation.

LouisCAD commented 3 years ago

Hello, can you try to use AndroidX App Startup 1.1.0-rc01?

See the related changelog here: https://developer.android.com/jetpack/androidx/releases/startup#1.1.0-rc01

LouisCAD commented 3 years ago

Should be fixed in 3.0.0-rc01 via AndroidX App Startup upgrade.

RavioliWonton commented 3 years ago

It fixed my problem, thank you!