实施 'com.google.android.gms:play-services-ads:18.1.0'

Implementation 'com.google.android.gms:play-services-ads:18.1.0'

我想在我的项目中添加广告,但是当我添加以下行时,它不起作用:

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

这些是来自 build.gradle 的依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation 'com.google.android.gms:play-services-ads:18.1.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

您是否在 top-level build.gradle 文件中引用了 google() 存储库或 maven { url "https://maven.google.com" }?您在日志中有一些特定的错误吗?

尝试创建一个新项目并复制旧项目文件并粘贴到新项目中。如果有帮助!

您可能没有添加 Google Services Gradle Plugin to the project, as well as the google-services.json file (which can be downloaded from the Firebase console) - 因此您的应用程序无法通过身份验证。

com.google.android.gms.ads.APPLICATION_ID 添加到 AndroidManifest.xml 具有相同的效果,因为 Play Services 插件将 google-services.json 中的值添加到 build-time。

...仅使用 AdMob 时,应首选第二个选项。