Solved: I am having problem with Initialize the Google Mobile Ads SDK

Question

Asked by Mody on January 07, 2022 (source).

I want to add Google Ads to my Android mobile app. So I went through this steps: https://developers.google.com/admob/android/quick-start?hl=de#kotlin

My problems:

1- When I add the dependencies for the Google Mobile Ads SDK to my module's app-level Gradle file, normally app/build.gradle:

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

I get the following error

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.1\6ca6bfbb6ed79157a08fdd6548cc9e4d\work-runtime-2.7.0\META-INF\com\android\build\gradle\aar-metadata.properties.

2- When I want to Initialize the Google Mobile Ads SDK on MainActivity.kt I get the following error

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- When I update compileSdkVersion to 32 I get this error:

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- When I update my MainActivity.kt from this:

package com.example.zwei

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

to this:

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) {}
    }
}

I got to much errors

What should I do to solve the errors?

Answer

Question answered by Sujal K (source).

Looked carefully at the error this time.

Soln(1): Try updating your compileSDKVersion to 31 or 32. Please let me know if the error still persists.

​ android​ { ​     compileSdkVersion 32 //Like this ​     buildToolsVersion ​"​32.0.0​" .... }

Soln(2): The other way is to actually "downgrade" the library to that stage when it didn't need the SDK version to be >=31(but I won't recommend doing this).

After doing Soln(1), do this: ext.kotlin_version = '1.4.32' (or the latest one available)

(Should solve the error you mentioned in the comment)

Also, make sure it's updated in Android Studio as well.app/build.gradlebuild.gradle(where the Kotlin version is present already)

ADMOB ANDROID DART FLUTTER
SHARE: