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?
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With