java - 将 AdMob 与 Android 应用程序集成时出现问题
问题描述
我正在尝试将 AdMob 横幅添加与我的 android 应用程序集成。我已按照https://developers.google.com/admob/android/quick-start#update_your_androidmanifestxml的说明进行操作。
我已将我的 APPLICATION_ID 放入清单文件的元数据中,并使用 Google 提供的添加单元 ID 进行测试,即“ca-app-pub-3940256099942544/6300978111”。我不断收到以下错误:无法获取提供商 com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException Google 移动广告 SDK 初始化不正确。AdMob 发布商
我已经在互联网上搜索并找不到问题。
清单.xml
<application
<meta-data
android:name="com.google.android.gms.adds.APPLIATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx" />
<activity
....
</activity>
</application>
在 build.gradle 我有:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion '30.0.3'
defaultConfig {
applicationId "com.lornalaycock.retirementsimulator"
minSdkVersion 26
targetSdkVersion 30
versionCode 10
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
ndk {
debugSymbolLevel 'SYMBOL_TABLE'
}
}
debug {
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
ndkVersion "23.0.7599858"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests.returnDefaultValues=true;
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
implementation 'com.nononsenseapps:filepicker:4.1.0'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
implementation 'mk.webfactory.dz:maskededittext:2.0'
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation 'androidx.room:room-runtime:2.3.0'
implementation "com.google.android.material:material:1.5.0-alpha02"
implementation 'androidx.wear:wear:1.1.0'
annotationProcessor 'androidx.room:room-compiler:2.3.0'
implementation 'com.tom_roush:pdfbox-android:1.8.10.3'
implementation 'com.google.android.gms:play-services-ads:20.3.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:runner:1.4.0'
testImplementation 'androidx.test:core:1.4.0'
testImplementation "org.robolectric:robolectric:4.2.1"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test:core:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
项目构建.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'https://jitpack.io'}
maven {url "https://maven.google.com"}
maven {url "$rootDir/../node_modules/react-native/android"}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
在 xml 文件中:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
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>
在 MainActivity
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
任何想法,请帮助。扯掉我的头发。
经过几乎一整天的不同想法,我仍然有一个问题。我删除了所有与 AdMob 相关的代码,并创建了一个新项目,其中只有 Hello World 文本视图。然后我将以下行添加到 build.gradle。
implementation 'com.google.android.gms:play-services-ads:20.3.0'
同步和正在运行的 Hello World 我收到以下错误,这与我在尝试测试我的原始应用程序时遇到的错误相同。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admobtest, PID: 27348
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 here: *
* https://googlemobileadssdk.page.link/admob-android-update-manifest *
在项目 build.gradle 中,我在 allprojects 下添加了 mavenCentral()。
在我添加的清单文件中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
但似乎包含依赖项的行导致了崩溃。
我错过了什么吗?
解决方案
还有一件很重要的事我忘了说。在 Android Studio 中创建具有空活动的新项目后,我必须修改 settingd.gradle,如下所示:
//dependencyResolutionManagement {
// repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
// repositories {
// google()
// mavenCentral()
// jcenter() // Warning: this repository is going to shut down soon
// }
//}
rootProject.name = "TuesdayTest"
include ':app'
必须注释掉我的项目级别 build.gradle 中的这些行。我从别人遇到的问题中发现了这一点。似乎是 Android Studio 和 google() 和 mavenCentral() 中所有最新版本和依赖项的问题。此外,我仍然必须在文件中添加 jcenter() 才能从 GitHub 导入包。我认为谷歌已经把事情搞得一团糟。
推荐阅读
- snowflake-cloud-data-platform - 雪花中的安全变量
- java - Android Studio 视频不显示或录制
- windows - 转换为ASCII。扩展的 ASCII 字符不起作用。C语言
- r - 为什么热图行顺序与数据行顺序不一样?
- c++ - 使用带有 lambda 函数指针的模板
- r - 将 R 闪亮应用程序中生成的数据保存在 Amazon S3 存储桶中的 .csv 文件中
- python - Pandas Dateoffset 行为不一致
- java - 从单独的方法调用时,getKey(alias, keyPassword) 返回密钥的空值
- java - 将事件发布到事件队列 / edt - 未传递给 actionPerformed() 的事件
- java - 为什么其他玩家在我的 Last Stone Game (Nim) 实现中永远不会轮到它?