[Android] ClassNotFoundException: Didn’t find class “com.google.android.gms.ads.MobileAdsInitProvider”

I am facing this issue in Android Version 4.4 and below. I have integrated Google admob and when I run the app, the app crashes with ClassNotFoundException.

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) 

解法:
https://developer.android.com/studio/build/multidex.html

1. build.gradle 增加 multiDexEnabled true

defaultConfig {
multiDexEnabled true
}

2. dependencies 增加 com.android.support:multidex

 implementation 'com.android.support:multidex:1.0.3'

3.AndroidManifest.xml 裡使用 Application 在 application tag:

<application
android:name=”.TestApplication”

再服用下面的 code:

public class TestApplication extends Application {
private boolean DEBUGGABLE = false;

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
try {
MultiDex.install(this);
}catch (Exception e){
//
}
}

@Override
public void onCreate() {
super.onCreate();

try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
int flags = info.applicationInfo.flags;
DEBUGGABLE = (0 != (flags & ApplicationInfo.FLAG_DEBUGGABLE));
} catch (PackageManager.NameNotFoundException e) {
//
}
}
}

you should paste the following to the bottom of your project’s – build.gradle

// This is to fix the errors : java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == "com.android.support") {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "26.+"
            }
        }
    }
}
  1. i ended up using the suggestion in the URL below (instead of 1) –

developer.android.com/studio/build/multidex

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *