activity
activity

Reputation: 2733

How to set Admob appID programmatically (19.1.0)?

Before version 19.1.0, the appID could be set programmatically like this:

MobileAds.initialize (Context context, String appID)

The new method is

initialize(Context, OnInitializationCompleteListener)

Internally, that method passes null as appID:

public static void initialize(Context var0, OnInitializationCompleteListener var1) {
    zzxw.zzqq().zza(var0, (String)null, var1);
}

Does this mean that appID should no longer be set programmatically?

Upvotes: 9

Views: 6018

Answers (2)

Mujahid Khan
Mujahid Khan

Reputation: 1834

You have to put APPLICATION_ID on manifest file , you can find your App ID in the AdMob UI. For android:value insert your own AdMob App ID in quotes, as shown below.

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

after that you can change it programmatically

 try {
        ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        String myApiKey = bundle.getString("com.google.android.gms.ads.APPLICATION_ID");
        Log.d(TAG, "Name Found: " + myApiKey);
        ai.metaData.putString("com.google.android.gms.ads.APPLICATION_ID", "ca-app-pub-3940256099942544~3347511713");//you can replace your key APPLICATION_ID here
        String ApiKey = bundle.getString("com.google.android.gms.ads.APPLICATION_ID");
        Log.d(TAG, "ReNamed Found: " + ApiKey);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
    } catch (NullPointerException e) {
        Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());
    }

Upvotes: 4

gsanskar
gsanskar

Reputation: 669

You just need to set the app-id in the AndroidManifest.xml and the same will be picked up by the SDK automatically.

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

Refer: https://developers.google.com/admob/android/quick-start

Upvotes: -3

Related Questions