无法获取提供商 com.google.android.gms.ads.MobileAdsInitProvider:java.lang.IllegalStateException

Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException

我将 Android Studio (3.2.1) 及其 SDK 更新到最新版本。之后,它需要通过添加这些标签来手动更新清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@drawable/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="AllowBackup,GoogleAppIndexingWarning">

    <meta-data
        android:name="com.google.android.gms.ads.APP_ID"
        android:value="ca-app-pub-#############"/>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".NewActivity" />
    <activity android:name=".PreviewActivity" />
    <activity android:name=".EditActivity" />
    <activity
        android:name=".preferencesdata.AboutActivity"
        android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
    <activity android:name=".preferencesdata.SettingsActivity"
        android:label="@string/settings">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>
</application>

我把应用程序的真实 ID 而不是应用程序 ID。该值也已更改为正确的广告单元 ID,如此处所示 https://developers.google.com/admob/android/quick-start。我也在 MainActivity.kt:

中调用了这个函数
MobileAds.initialize(this, resources.getString(R.string.banner_ad_unit_id)) 

但是,无法解决问题。它仍然打印如下:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.easyapps.cryptnote, PID: 3991
    java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException: 

    ******************************************************************************
    * The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers    *
    * should follow the instructions LINK to add a valid  *
    * App ID inside the AndroidManifest. Google Ad Manager publishers should     *
    * follow instructions here: LINK.                           *
    ******************************************************************************


        at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
        at android.app.ActivityThread.-wrap1(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: 

    ******************************************************************************
    * The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers    *
    * should follow the instructions here: LINK to add a valid  *
    * App ID inside the AndroidManifest. Google Ad Manager publishers should     *
    * follow instructions here: LINK.                           *
    ******************************************************************************


        at com.google.android.gms.internal.ads.zzmn.attachInfo(Unknown Source)
        at com.google.android.gms.ads.MobileAdsInitProvider.attachInfo(Unknown Source)
        at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
            ... 10 more

有人可以告诉我为什么会发生或帮助我解决它吗?我试图 google 这个问题,但没有解决方案。这似乎是一个新的尚未解决的问题。非常感谢。

我觉得名字应该是"APPLICATION_ID"而不是"APP_ID"

发生这种情况是由于更新了 AdMob SDK。现在您需要在清单文件中添加 appID。

<application> . . . 
      <meta-data 
           android:name="com.google.android.gms.ads.APPLICATION_ID" 
           android:value="ca-app-pub-################~##########"/>
</application>

您可以从这里获取appID -->Google addmob -->Select app --> App Settings --> App ID。

您可以从此处阅读更多内容 ads-developers

如果您想在放置自己的 ID 之前使用测试广告,请从此处检查 AdMob 测试 ID developers.google

希望对您有所帮助。谢谢:)

Google 更新了 Google 广告,因此如果您在 gradle 中更新 Admob 库,则需要将其添加到您的清单中。

<manifest>
   <application>
      <!-- TODO: Replace with your real AdMob app ID -->
      <meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID"
          android:value="ca-app-pub-################~##########"/>
  </application>
</manifest>

添加您的清单文件

<meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true" />

Google Android 的移动广告 SDK v17.0.0 刚刚发布,其中包含这些重要更改。

需要 AndroidManifest.xml 更改

从版本 17.0.0 开始,如果您是 AdMob 发布商,现在需要在 AndroidManifest.xml 文件中添加您的 AdMob 应用程序 ID。在 AdMob UI 中找到您的 AdMob 应用程序 ID 后,将其添加到清单中并添加以下标签:

<manifest>
    <application>
        <!-- TODO: Replace with your real AdMob app ID -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-################~##########"/>
    </application>
</manifest>

未能添加此标签将导致应用程序在应用程序启动时崩溃,并显示以 The Google Mobile Ads SDK was initialized incorrectly.

开头的消息

如果我使用 Google Ad Manager 而不是 AdMob 会怎样?

使用 Google Ad Manager 的发布商需要使用不同的代码将自己声明为 Ad Manager 应用,以避免同样的崩溃:

<manifest>
    <application>
        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true"/>
    </application>
</manifest>

有关详细信息,请参阅 link

在 Unity3D V3.18 和 Ad SDK V 7.45.0 中,您需要设置这些数据@菜单:资产 > Google 移动广告 > 设置 & 它会自动将这些数据写入清单文件。

将 APPLICATION_ID 和 AD_MANAGER_APP 直接添加到清单文件中(其他答案中已经给出)对我来说仍然崩溃。

重要提示:从 Google 移动广告 SDK 版本 17.0.0 开始,此步骤是必需的。添加此标记失败会导致崩溃并显示以下消息:Google 移动广告 SDK 初始化不正确。

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>

对于在 Xamarin Forms Cross Platform 应用程序中出现此错误的用户,错误

Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException`

可能是因为安装了 Xamarin.GooglePlayServices.Ads 版本。当我使用 Version 71.1720.0 时出现此错误。因此我卸载了这个版本并将其恢复到 Version 60.1142.1 并且它没有崩溃。

教程请参考this Youtube Video

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"

如果您遇到上述错误并且在搜索时出现在此处,这可能不是因为您忘记在清单中插入 adMob 元数据。我遇到过这个问题,解决方案是在您的应用程序中实施 MultiDex。

这里是 Google 说明:link

如果您尚未发布应用,请尝试以下操作

 <meta-data
      android:name="com.google.android.gms.ads.AD_MANAGER_APP"
      android:value="true"/>

确保检查以下步骤。

1. 项目级别 Gradle 在所有项目中添加了 google()

allprojects {
    repositories {
        google()
    }
}

2. 应用级别 Gradle 添加了以下依赖项,与以下示例相同:

**implementation 'com.google.android.gms:play-services-ads:19.3.0'**

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
}

3.更新<meta-data>中提到的AndroidManifest.xml中的App-ID。将您的 App-ID 替换为 android:value

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>

4. adView 添加到布局中。确保你添加的是 ads:adUnitId 而不是 App-ID 两者都不同。

我遇到了错误,因为我对两者感到困惑。 App-ID 有“/”,adUnitId 有“~”。出于测试目的,请将“ca-app-pub-3940256099942544/6300978111”用作 adUnitId,否则您的帐户将被暂停。

当您准备发布时,您可以添加您的真实 ID 并发布。

<com.google.android.gms.ads.AdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      ads:adSize="BANNER"
      ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
  </com.google.android.gms.ads.AdView>

检查所有 4 个步骤,您将能够解决此错误。

使用新的 Admob 库,我们必须在 onCreate() 方法中初始化和加载广告,并在 AndroidManifest 文件中添加 App ID,如其他答案中所述。但不要将广告 ID 与应用程序 ID 混淆,从而将其放入 AndroidManifest,这是我犯的错误。

Ex App ID:  ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx 

Ex Ad ID:   ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx 

不要与 APPLICATION_ID 混淆。您不应该 将其替换 为您的应用程序 ID。 com.example.myapp。保持原样。

<application>
...
      <meta-data 
           android:name="com.google.android.gms.ads.APPLICATION_ID" 
           android:value="ca-app-pub-################~##########"/>
...
</application>

我已经添加了 但错误没有解决。解决方案是将 放在 android -> app -> src -> main 中 androidManifest.xml 文件中其他元数据的最后。像这样:

<application>
   ...
   ...
   < meta-data 
        android:name="com.google.android.gms.ads.APPLICATION_ID" 
        android:value="ca-app-pub-################~##########"/> 
</application>

解决方案是将元数据行放在 最后(意味着在 android 清单文件中存在的所有其他元数据下方,但就在关闭应用程序标记之前)。

确保你的元数据标签没有在里面 activity...去过那里,做到了。

一定要检查好,因为在 <meta-data ..../> 清单文件中它应该是 App ID 而不是 ADd ID

App ID: ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx 
Ad ID: ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx 

就我而言,我不小心将 meta-data 标签放在 activity 标签内,如下所示,

    <manifest>
      <application>
         <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
             <meta-data
               android:name="com.google.android.gms.ads.APPLICATION_ID"
               android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
       </activity>
    </application>
</manifest>

请确保在 activity 标签外添加 mata 标签

 <manifest>
  <application>
     <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- Specifies an Android theme to apply to this Activity as soon as
             the Android process has started. This theme is visible to the user
             while the Flutter UI initializes. After that, this theme continues
             to determine the Window background behind the Flutter UI. -->
        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
   </activity>
   <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
</application>