问题
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