我在初始化 Google 移动广告 SDK 时遇到问题

I am having problem with Initialize the Google Mobile Ads SDK

我想在我的 Android 移动应用程序中添加 Google 广告。所以我经历了这个步骤: https://developers.google.com/admob/android/quick-start?hl=de#kotlin

我的问题:

1- 当我将 Google 移动广告 SDK 的依赖项添加到我模块的应用级 Gradle 文件时,通常 app/build.gradle:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.android.gms:play-services-ads:20.5.0' }

我收到以下错误

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > The minCompileSdk (31) specified in a
     dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
     is greater than this module's compileSdkVersion (android-30).
     Dependency: androidx.work:work-runtime:2.7.0.
     AAR metadata file: C:\Users\moham\.gradle\caches\transforms-2\files-2.1ca6bfbb6ed79157a08fdd6548cc9e4d\work-runtime-2.7.0\META-INF\com\android\build\gradle\aar-metadata.properties.

2- 当我想在 MainActivity.kt 上初始化 Google 移动广告 SDK 时,出现以下错误

e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (4, 12): Unresolved reference: google
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (6, 22): Unresolved reference: AppCompatActivity
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (7, 5): 'onCreate' overrides nothing
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (7, 47): Unresolved reference: Bundle
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (8, 15): Unresolved reference: onCreate
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (9, 9): Unresolved reference: setContentView
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (9, 33): Unresolved reference: activity_main
e: C:\Users\moham\flutter\zwei\android\app\src\main\kotlin\com\example\zwei\MainActivity.kt: (11, 9): Unresolved reference: MobileAds

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

3- 当我将 compileSdkVersion 更新为 32 时出现此错误:

e: C:/Users/moham/.gradle/caches/transforms-2/files-2.1/683fded33b117c0eb9bb9deabca1a71e/work-runtime-2.7.0-api.jar!/META-INF/work-runtime_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.

4- 当我从这里更新我的 MainActivity.kt 时:

package com.example.zwei

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

对此:

package com.example.zwei

import io.flutter.embedding.android.FlutterActivity
import com.google.android.gms.ads.MobileAds

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        MobileAds.initialize(this) {}
    }
}

我有很多错误

我应该怎么做才能解决错误?

仔细看了这次的错误

索恩(1): 尝试将您的 compileSDKVersion 更新为 31 或 32。如果错误仍然存​​在,请告诉我。

android { compileSdkVersion 32 //像这样 buildToolsVersion “ 32.0.0 ” .... }

索恩(2): 另一种方法是在不需要 SDK 版本 >=31 时将库实际“降级”到那个阶段(但我不建议这样做)。

完成 Soln(1) 后,执行以下操作: ext.kotlin_version = '1.4.32'(或可用的最新版本)

(应该解决你在评论中提到的错误)

此外,请确保它也在 Android Studio 中更新。