[Android] 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

Posted in :

歡迎來到AndroidX 還有支援 Android 5.0 (API 21)+ 的世界,舊的硬體和OS 會慢慢「凋零」,Max猜想:「可以大膽地直接支援 Android 5.0+ 」了。

Google官方教學文章:
https://developer.android.com/studio/build/multidex#java

I got this error in a multi-module setup when trying to build the different modules together as one APK for API < 21. I already refactored to AndroidX, but the multidex docs don’t mention AndroidX yet.

If you are using AndroidX, make sure to replace the old multidex dependency

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

with the new one

implementation 'androidx.multidex:multidex:2.0.1'

You are getting this error because you have use multiDex but some implementation part is missing. Follow below steps to resolve the error.

implementation part is missing. Follow below steps to resolve the error.

1) Add “multiDexEnabled true” in defaultconfig in app-level gradle file

android {
    defaultConfig {
        ...
        minSdkVersion 21
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

2) If your minSdkVersion is less than 21 then add below dependency.

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}

3) Use MultiDexApplication class as Application class. There are three way to use MultiDexApplication as Application class

i) Just set MultiDexApplication class in AndroidManifest.xml file

<?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) If you are already using custom application class then extent MultiDexApplication in custom application class

public class MyApplication extends MultiDexApplication { ... }

iii) If it is not possible to extend MultiDexApplication because you already extend other class and can not change it then use below method in your custom application class

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

發佈留言

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