添加AdMob依赖至build.gradle后应用启动即崩溃问题排查
AdMob依赖添加后应用启动崩溃原因分析
问题背景
原本运行正常的应用,添加AdMob广告依赖(implementation 'com.google.android.gms:play-services-ads:22.5.0')后启动立即崩溃,设备为Android 7.0(SDK 24),确认版本兼容,排查崩溃原因。
崩溃错误日志
E FATAL EXCEPTION: main Process: com.myapp.www.myapp, PID: 6415 java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException: ****************************************************************************** * Invalid application ID. Follow instructions here: * * https://googlemobileadssdk.page.link/admob-android-update-manifest * * to find your app ID. * * Google Ad Manager publishers should follow instructions here: * * https://googlemobileadssdk.page.link/ad-manager-android-update-manifest. * ****************************************************************************** at android.app.ActivityThread.installProvider(ActivityThread.java:5977) at android.app.ActivityThread.installContentProviders(ActivityThread.java:5545) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5484) at android.app.ActivityThread.-wrap2(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1067) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928) Caused by: java.lang.IllegalStateException: ****************************************************************************** * Invalid application ID. Follow instructions here: * * https://googlemobileadssdk.page.link/admob-android-update-manifest * * to find your app ID. * * Google Ad Manager publishers should follow instructions here: * * https://googlemobileadssdk.page.link/ad-manager-android-update-manifest. * ****************************************************************************** at com.google.android.gms.ads.internal.client.zzek.attachInfo(com.google.android.gms:play-services-ads-lite@@22.5.0:15) at com.google.android.gms.ads.MobileAdsInitProvider.attachInfo(com.google.android.gms:play-services-ads-lite@@22.5.0:1) at android.app.ActivityThread.installProvider(ActivityThread.java:5974) ... 10 more
相关配置文件
项目build.gradle
buildscript { repositories { google() mavenCentral() } } // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false } //allprojects { // repositories { // google() // mavenCentral() // } //}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.myapp.www.myapp"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="com.android.vending.BILLING" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="com.google.android.gms.permission.AD_ID" /> <!-- android:dataExtractionRules="@xml/data_extraction_rules"--> <!-- android:fullBackupContent="@xml/backup_rules"--> <!-- tools:targetApi="33"--> <!-- tools:ignore="ExtraText">--> <!-- <meta-data--> <!-- android:name="android.app.lib_name"--> <!-- android:value="" />--> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher_foreground" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round_foreground" android:supportsRtl="true" android:theme="@style/Theme.MyApp"> <activity android:name=".MainActivity" android:configChanges="orientation|screenSize" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 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>
应用模块build.gradle
plugins { id 'com.android.application' } android { compileSdk 34 defaultConfig { applicationId "com.myapp.www.myapp" minSdk 19 targetSdk 34 versionCode 1021 versionName "1.021" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true javaCompileOptions { gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:deprecation" } } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { def billing_version = "6.0.1" /* This is for the billing library we are using */ /* implementation 'com.anjlab.android.iab.v3:library:1.0.44' */ /* New Google's Billing */ /* implementation 'com.android.billingclient:billing:2.2.0' */ implementation "com.android.billingclient:billing:$billing_version" /* TEMP ADVERTS */ /* This is for adverts */ /* implementation 'com.google.android.gms:play-services-ads:15.0.0' */ /* Consent for Adverts GDPR */ /* /* implementation 'com.google.android.ads.consent:consent-library:1.0.6' */ /* implementation 'com.google.android.ads.consent:consent-library:1.0.7' */ /* Adverts */ implementation 'com.google.android.gms:play-services-ads:22.5.0' /* GDPR */ /* implementation 'com.google.android.ump:user-messaging-platform:2.1.0' */ implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' /* This for ImmutableList */ implementation 'com.google.guava:guava:31.1-android' implementation 'androidx.multidex:multidex:2.0.1' }
MainActivity初始化代码
MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } });
崩溃原因
核心原因:AdMob应用ID无效
从崩溃日志中的Invalid application ID提示可直接定位问题:你在AndroidManifest.xml中配置的AdMob应用ID是占位符ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy,并非从AdMob后台获取的真实有效ID。版本兼容确认
Android 7.0(SDK 24)完全兼容play-services-ads:22.5.0,该版本最低SDK要求为19,你的应用minSdk也设置为19,因此崩溃与系统版本无关。
解决方法
将AndroidManifest.xml中的占位符ID替换为你在AdMob后台创建应用时生成的真实应用ID,格式为ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy(其中xxxxxxxxxxxxxxxx和yyyyyyyyyy为实际ID数值)。
内容的提问来源于stack exchange,提问作者Rewind
相关产品推荐
相关产品推荐

