MobileAds.initialize(Context, String) is deprecated

Issue

Before it looked like this (passing R.string.admob_application_id):

MobileAds.initialize(this, getString(R.string.admob_application_id));

MobileAds method .initialize(Context context, String applicationCode)
now reads:

This method is deprecated. Use initialize(Context, OnInitializationCompleteListener) instead.

Which reads:

Initializes the Google Mobile Ads SDK.
Call this method as early as possible to reduce latency on the session’s first ad request.
If this method is not called, the first ad request automatically initializes the Google Mobile Ads SDK.

MobileAds.Settings also had been deprecated.


What I have tried so far is to get the InitializationStatus per adapter:

MobileAds.initialize(this, initializationStatus -> {

    /* get the adapter status */
    Map<String, AdapterStatus> map = initializationStatus.getAdapterStatusMap();
    for (Map.Entry<String, AdapterStatus> entry : map.entrySet()) {
        AdapterStatus adapterStatus = entry.getValue();
        AdapterStatus.State state = adapterStatus.getInitializationState();
        Log.d(LOG_TAG, "key = " + entry.getKey() + ", state = " + state.name() + ", desc = " + adapterStatus.getDescription());
    }
});

On an emulator it says READY, but .getDescription() has a rather confusing message:

Google Mobile Ads SDK initialization functionality unavailable for this session.
Ad requests can be made at any time.

On a physical device with internet connection it shows four more adapters:

key = com.google.android.gms.ads.MobileAds, state = READY, desc = 
key = com.google.ads.mediation.vungle.VungleMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.applovin.AppLovinMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.adcolony.AdColonyMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.tapjoy.TapjoyMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.

om.google.android.gms.ads.MobileAds reads READY, the other com.google.ads.mediation.* read NOT_READY and Failed to create Adapter (there is no mediation set up, but it can load test ads).

MobileAds.initialize(this); also works (OnInitializationCompleteListener is optional).


It seems to work without the R.string.admob_application_id, therefore my actual question is:

Is setting R.string.admob_publisher_id and R.string.admob_application_id still required – or is it only the ad-unit ID now? Most of the documentation still uses the deprecated initialization method (alike the one shown above), therefore this isn’t clear.

Solution

Get started with AdMob in your Android project seems to answer the application ID part:

Add your AdMob App ID to your app’s AndroidManifest.xml file by adding the <meta-data> tag as shown below.

Important: This step is required as of Google Mobile Ads SDK v17.0.0. If you don’t add this <meta-data> tag, your app will crash with the message: "The Google Mobile Ads SDK was initialized incorrectly."

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ADMOB_APP_ID"/>
    </application>
</manifest>

And the publisher ID still is required for Requesting Consent from European Users.

Answered By – Martin Zeitler

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published