Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue. Once you've integrated the SDK, you can choose an ad format (such as native or rewarded video) and follow the steps to implement it.
To prepare your app, complete the steps in the following sections.
App prerequisites
- Use Android Studio 3.2 or higher
Make sure that your app's build file uses the following values:
- A
minSdkVersion
of19
or higher - A
compileSdkVersion
of28
or higher
Configure your app in 3 steps.
build.gradle
file, include Google's Maven repository and Maven central repository in both your buildscript
and allprojects
sections:2. Add the dependencies for the Google Mobile Ads SDK to your module's app-level Gradle file, normally
app/build.gradle
:implementation 'com.google.android.gms:play-services-ads:21.0.0'
3. Add your AdMob app ID (identified in the AdMob UI) to your app's AndroidManifest.xml
file. To do so, add a <meta-data>
tag with android:name="com.google.android.gms.ads.APPLICATION_ID"
. You can find your app ID in the AdMob UI. For android:value
, insert your own AdMob app ID, surrounded by quotation marks.
Code snippet used in the video:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aarappstudios.admobseries">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AdmobSeries">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 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-3940256099942544~3347511713"/>
</application>
</manifest>
MainActivity.java
package com.aarappstudios.admobseries;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {
}
});
mAdView=findViewById(R.id.adView);
AdRequest adRequest=new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
activity_main.xml
package com.aarappstudios.admobseries;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {
}
});
mAdView=findViewById(R.id.adView);
AdRequest adRequest=new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
0 Comments