且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.lang.ClassNotFoundException:找不到类“ com.google.android.gms.ads.MobileAdsInitProvider”

更新时间:2022-03-18 05:32:07

您收到此错误,因为您使用过multiDex,但是缺少一些实现部分。请按照以下步骤解决该错误。

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

1)在应用级gradle文件的defaultconfig中添加 multiDexEnabled true

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

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

2)如果您的minSdkVersion小于21然后在下方添加依赖项。

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

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

3)使用MultiDexApplication类作为Application类。有三种方法可以将MultiDexApplication用作Application类

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

i)只需在AndroidManifest.xml文件中设置MultiDexApplication类

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)如果您已经在使用自定义应用程序类,则在自定义应用程序类中扩展MultiDexApplication

ii) If you are already using custom application class then extent MultiDexApplication in custom application class

public class MyApplication extends MultiDexApplication { ... }

iii)如果无法扩展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);
  }
}

注意:我遇到了同样的错误,只是通过扩展MultiDexApplication类来解决