我在Android 4.4及以下版本中遇到此问题。我已经集成了Google admob,当我运行该应用程序时,该应用程序崩溃,并显示ClassNotFoundException。
请在下面找到堆栈跟踪。
Process: app.learnkannada.com.learnkannadakannadakali, PID: 1320
java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.MobileAdsInitProvider" on path: DexPathList[[zip file "/data/app/app.learnkannada.com.learnkannadakannadakali-2.apk"],nativeLibraryDirectories=[/data/app-lib/app.learnkannada.com.learnkannadakannadakali-2, /system/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:4793)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.MobileAdsInitProvider" on path: DexPathList[[zip file "/data/app/app.learnkannada.com.learnkannadakannadakali-2.apk"],nativeLibraryDirectories=[/data/app-lib/app.learnkannada.com.learnkannadakannadakali-2, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.ActivityThread.installProvider(ActivityThread.java:4778)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我尝试在Google上查找,但找不到此问题。要求社区对此提供帮助。
注意:请注意,我已经在Android Manifest中添加了移动广告元数据。
最佳答案
您收到此错误是因为您使用过multiDex,但是缺少一些实现部分。请按照以下步骤解决错误。
1)在应用程序级gradle文件的defaultconfig中添加“multiDexEnabled true”
android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 28
multiDexEnabled true
}
...
}
2)如果您的minSdkVersion小于21,则添加以下依赖项。
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
3)使用MultiDexApplication类作为Application类。有三种方法可以将MultiDexApplication用作Application类
i)只需在AndroidManifest.xml文件中设置MultiDexApplication类
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
</manifest>
ii)如果您已经在使用自定义应用程序类,则在自定义应用程序类中扩展MultiDexApplication
public class MyApplication extends MultiDexApplication { ... }
iii)如果由于已经扩展了其他类而无法扩展MultiDexApplication,并且无法更改它,则在自定义应用程序类中使用以下方法
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
注意:我遇到了同样的错误,只是通过扩展MultiDexApplication类来解决了