Android 8.1 无法获取提供者 com.google.android.gms.ads.MobileAds!!!应用程序崩溃

Android 8.1 Unable to get provider com.google.android.gms.ads.MobileAds!!! apk crash

我的 Android Studio 版本 3.4.1,我正在尝试将 MobileAds 代码添加到我使用 sdk 28 构建的项目中,但收到崩溃报告。不知道为什么????

我已经检查了所有类似的帖子,但有些已经过时,很遗憾无法找到任何解决方案

我的Android清单是

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

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-3940256099942544/6300978111"/>
        <activity
    android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:screenOrientation="portrait" />

MainActivity 布局

    <com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:layout_weight="1"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">

应用build.gradle

   android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
multiDexEnabled true
buildTypes {
release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.txt'
}
}
repositories {
jcenter()
mavenCentral()  // GPUImage for Android
}
dexOptions {
javaMaxHeapSize "4g"
}
productFlavors {
}
} 
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation files('libs/acra-4.5.0.jar')
implementation files('libs/ormlite-android-4.43.jar')
implementation files('libs/ormlite-core-4.43.jar')
// If you want to use the GPU Filters
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
 implementation 'com.google.firebase:firebase-ads:17.2.1'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'jp.wasabeef:glide-transformations:2.0.1'
implementation 'com.onesignal:OneSignal:3.10.9'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:exifinterface:28.0.0'
implementation 'com.google.android.gms:play-services-ads:17.2.1'
implementation 'com.google.android.gms:play-services-maps:+'

}
apply plugin: 'com.google.gms.google-services'

HomeActivity.java

    @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_new);
MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");

AdView mAdView = (AdView) findViewById(R.id.adView);
try{

    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    if (admov_status.equalsIgnoreCase("true")){
        mAdView.setVisibility(View.VISIBLE);
    }else if(admov_status.equalsIgnoreCase("false")) {
        mAdView.setVisibility(View.GONE);
    }
}catch (RuntimeException e){
    mAdView.setVisibility(View.GONE);
}
}

但错误仍然是 运行。错误 logcat 此处有详细信息

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

******************************************************************************
* Invalid application ID. Follow instructions here: Link to *
* find your app ID.                                                          *
******************************************************************************


    at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.IllegalStateException: 

******************************************************************************
* Invalid application ID. Follow instructions here: Link to *
* find your app ID.                                                          *
******************************************************************************


    at com.google.android.gms.internal.ads.zzabh.attachInfo(Unknown Source:19)
    at com.google.android.gms.ads.MobileAdsInitProvider.attachInfo(Unknown Source:3)
    at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
        ... 10 more

谁能帮我解决问题。

Admob 中有 2 个广告 ID。

1).应用ID

2).广告单元 ID

对于应用,无论您使用何种广告类型,appID 都是相同的。但是 adUnitID 因您使用的每种广告类型而异。例如,您可以在 App 中同时拥有横幅广告和插页式广告。对于这两种广告类型,appID 相同,但 adUnitID 不同。

根据您的代码,您在所有地方都使用了相同的 ID,这是不正确的。您需要在适当的地方使用appID、adUnitID。在布局 xml 中,您声明广告单元的地方应该有 adUnitID 和其他地方 appID.

Manifest.xml

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value=**appID**/>

MainActivity 布局

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:layout_weight="1"
ads:adSize="SMART_BANNER"
ads:adUnitId=**adUnitID**>

HomeActivity.java

MobileAds.initialize(this, **appID**);

所以在你的情况下,你似乎已经使用了 adUnitID 作为你应该使用 appID 的地方。这可能就是它抛出

中的错误的原因
******************************************************************************
* Invalid application ID. Follow instructions here: Link to *
* find your app ID.                                                          *
******************************************************************************

请参阅此 link,了解如何为您的 adMob

找出 appIDadUnitID

我遇到了同样的问题,很难理解上面的答案,然后我试了几下就成功了,后来我明白了上面的答案。 所以我将我的观察和经验用图形表示,以便于集成

要遵循的步骤:

  • 创建一个 AdMob 帐户如果您没有 (https://apps.admob.com/signup/)
  • 创建一个应用程序转到左侧面板并提供那里询问的所需详细信息(参考图片 - 1)

Image - 1

  • 一旦提供了询问的详细信息,您将看到如 Image - 2
  • 中所示的屏幕

Image - 2

  • 然后您可以去添加“广告单元”或点击“完成”。 (如果你点击完成,你将被重定向到 AppOverview 页面,在那里你可以添加“广告单元”)。
  • 现在点击应用程序 -> 查看所有应用程序(再次 参考图片 -1,查看选项)
  • 然后您将被重定向到显示所有应用程序信息的页面(参考图片 - 3)

Image - 3

  • 点击此处显示的 AppId,这就是您的应用程序 ID。将其复制并粘贴到 Manifest 文件中的值字符串中,如下所示,您还可以参考 Image -4

Image -4

           <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-8623492450664484~4654380864">
  • 然后转到您的 Activity.xml 文件(您添加 Ad Mob 横幅的位置),然后添加 "Ad Unit Id" 如下所示

            <com.google.android.gms.ads.AdView
             android:id="@+id/adView"
             android:layout_width="416dp"
             android:layout_height="52dp"
             android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
             ads:adSize="BANNER"
             ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
             tools:ignore="MissingConstraints" />
    

如果要测试,请使用下面提到的“AdUnitId”(AdMob 开发人员文档建议)

ca-app-pub-3940256099942544/6300978111

然后在你对应的Activity (Kotlin File)

中加入下面两行
    val adRequest = AdRequest.Builder().build()
    mAdView?.loadAd(adRequest)

现在 运行 应用程序,您将看到广告横幅。希望这会有所帮助

不是您的答案,但可能对其他人有帮助:

当您不在基础 build.gradle(应用程序)文件中也包含依赖项时,动态功能模块会导致问题(只有动态清单是不够的)。

我遇到了同样的问题。做了上面的推荐后我也加了

        repositories {
        google()
         jcenter()
        mavenCentral()  // GPUImage for Android
    }

在build.gradle那么问题就解决了。另外,请注意在清单中使用应用程序 ID 而不是 UnitId