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.
Prerequisites
- Use Android Studio 3.2 or higher
minSdkVersion
16 or highercompileSdkVersion
28 or higher
Import the Mobile Ads SDK
Apps can import the Google Mobile Ads SDK with a Gradle
dependency that points to
Google's Maven repository. First, make sure
that google()
is referenced in the allprojects
section of your
project-level build.gradle
file.
Example project-level build.gradle (excerpt)
allprojects { repositories { google() } }
Next, open the app-level build.gradle
file for your app, and look for
a "dependencies" section.
Example app-level build.gradle (excerpt)
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'com.google.android.gms:play-services-ads:19.7.0' }
Add the line in bold above, which instruct Gradle to pull in the latest version of the Mobile Ads SDK and additional related dependencies. Once that's done, save the file and perform a Gradle sync.
Update your AndroidManifest.xml
Add your Ad Manager app ID (identified in the
Ad Manager UI)
to your app's AndroidManifest.xml
file by adding a <meta-data>
tag with
android:name="com.google.android.gms.ads.APPLICATION_ID"
, as shown below.
You can find your app ID in the Ad Manager UI. For android:value
, insert your
own Ad Manager app ID in quotes, as shown below.
<manifest> <application> <!-- Sample Ad Manager 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> </manifest>
Initialize the Mobile Ads SDK
Before loading ads, have your app initialize the Mobile Ads SDK by calling
MobileAds.initialize()
which initializes the SDK and calls back a completion listener once
initialization is complete (or after a 30-second timeout). This needs
to be done only once, ideally at app launch.
Here's an example of how to call the initialize()
method in an Activity:
Example MainActivity (excerpt)
Java
package ... import ... 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 { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); } }
Kotlin
package ... import ... 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) {} } ... }
If you're using mediation, wait until the completion handler is called before loading ads, as this will ensure that all mediation adapters are initialized.
Select an ad format
The Mobile Ads SDK is now imported and you're ready to implement an ad. Ad Manager offers a number of different ad formats, so you can choose the one that best fits your app's user experience.
Banner
Rectangular ads that appear at the top or bottom of the device screen. Banner ads stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start.
Interstitial
Full-screen ads that cover the interface of an app until closed by the user. They're best used at natural pauses in the flow of an app's execution, such as between levels of a game or just after a task is completed.
Native
Customizable ads that match the look and feel of your app. You decide how and where they're placed, so the layout is more consistent with your app's design.
Google Ad Manager offers two ways to implement native ads: Native Styles and Custom Rendering.
Native Styles has been designed to make the implementation of native ads as easy as possible, and it's a great choice if you are new to the format. Custom Rendering has been designed to maximize the freedom you have in creating your presentations.
Implement Native Styles | Implement Custom Rendering |
Rewarded
Ads that reward users for watching short videos and interacting with playable ads and surveys. Good for monetizing free-to-play users.
Implement Rewarded Ads | Implement Rewarded Ads (New APIs) |