ClassNotFoundException: “androidx.work.impl.WorkManagerInitializer”

谁说我不能喝 提交于 2021-02-08 19:59:35

问题


A new exception appeared on some devices after upgrading Google AdMob Ads library version 19.4.0 to 19.5.0:

Caused by java.lang.ClassNotFoundException 
Didn't find class "androidx.work.impl.WorkManagerInitializer" on path: ...
dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:196)
androidx.core.app.CoreComponentFactory.instantiateProvider (CoreComponentFactory.java)
android.app.ActivityThread.installProvider (ActivityThread.java:7213)
android.app.ActivityThread.installContentProviders (ActivityThread.java:6769)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:941)

The exception appeared on devices with Android 8 & 10.

AdMob library 19.5.0 adds dependency on WorkManager 2.1.0 (via Play Services Ads Lite library): https://mvnrepository.com/artifact/com.google.android.gms/play-services-ads-lite/19.5.0

There is a similar question on this issue, but it seems to be unrelated (older Android OS versions with multiple dex, while here it's single dex and newer OS versions).

For now I downgraded to AdMob 19.4.0, which does not include WorkManager dependency.

Update (2020.12.18)

  • The exception frequency is 1 per 1000 devices running Android 10.
  • Forcing WorkManager 2.4.0 (instead of 2.1.0) also generates the exception.
  • The issue is unrelated to AdMob. Adding WorkManager to the project with AdMob 19.4.0 reproduced the issue.
  • My current assumption is that the issue is caused by a collision between the content providers of AudienceNetwork & WorkManager. Adding AudienceNetwork to a project with WorkManager generated a same exception, with a different class: Didn't find class "com.facebook.ads.AudienceNetworkContentProvider" on some Android 10 devices.

回答1:


Workaround: manual initialization of WorkManager does not cause this exception.

Remove the WorkManager default content provider initialization by adding the following to the Manifest.xml:

<provider
    android:name="androidx.work.impl.WorkManagerInitializer"
    android:authorities="${applicationId}.workmanager-init"
    tools:node="remove"
    android:exported="false"
    />

Add manual initialization of the WorkManager to the Application.onCreate() method:

Configuration myConfig = new Configuration.Builder()
        .build();
WorkManager.initialize(this, myConfig);



回答2:


Why not just add this line to your proguard file?

-keep class androidx.work.** { *; }


来源:https://stackoverflow.com/questions/64588254/classnotfoundexception-androidx-work-impl-workmanagerinitializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!