Google ads in android application

Google Mobile Ads SDK for Android

The Google Mobile Ads SDK is the latest generation in Google mobile advertising, featuring refined ad formats and streamlined APIs for access to mobile ad networks and advertising solutions. The SDK enables mobile app developers to maximize their monetization in native mobile apps.

This repository contains open source examples and developer resources for both the Google AdMob and Google Ad Manager components of the Google Mobile Ads SDK.

Google Mobile Ads SDK Developers forum

To report SDK feature requests, bugs, and crashes or to browse SDK-related discussions, please use our Google Mobile Ads SDK Developers forum. The forum provides the latest SDK announcements and updates as well as technical SDK support for our Android developers.

Check out our developers site for documentation on using the Mobile Ads SDK.

  • App Open: Java, Kotlin
  • Banner: Java, Kotlin
  • Interstitial: Java, Kotlin
  • Rewarded Interstitial: Java, Kotlin
  • Rewarded Video: Java, Kotlin
  • Native Advanced: Java, Kotlin
  • API Demo: Java, Kotlin — Provides additional examples for both AdMob and Ad Manager to help improve your mobile app integration of the Google Mobile Ads SDK.
  • Banner RecyclerView: Java — Provides an example for displaying ads from AdMob Banner ads in a RecyclerView widget.
  • App Open: Java, Kotlin
  • Banner: Java, Kotlin
  • Interstitial: Java, Kotlin
  • Rewarded Interstitial: Java, Kotlin
  • Rewarded Video: Java, Kotlin
  • Native Ads: Java, Kotlin

Please check out our releases for the latest downloads of our example apps.

GitHub issue tracker

To file bugs, make feature requests, or suggest improvements for the Android example apps, please use GitHub’s issue tracker.

For SDK support issues, please use the Google Mobile Ads SDK Developers forum.

Источник

Get Started

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.

Before you begin

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 of 16 or higher
  • A compileSdkVersion of 28 or higher

Configure your app

In your project-level build.gradle file, include Google’s Maven repository and Maven central repository in both your buildscript and allprojects sections:

Add the dependencies for the Google Mobile Ads SDK to your module’s app-level Gradle file, normally app/build.gradle :

Add your Ad Manager app ID (identified in the Ad Manager UI) to your app’s AndroidManifest.xml file. To do so, add a tag with android:name=»com.google.android.gms.ads.APPLICATION_ID» . You can find your app ID in the Ad Manager UI. For android:value , insert your own Ad Manager app ID, surrounded by quotation marks.

In a real app, use your actual Ad Manager app ID, not the one listed above. If you’re just looking to experiment with the SDK in a Hello World app, you can use the sample app ID shown above.

Note also that failure to add the tag as shown above results in a crash with the message:

(Optional) Declare AD_ID permission for previous versions to work with Android S.

If your app uses the Google Mobile Ads SDK version 20.4.0 or higher, you can skip this step since the SDK automatically declares the com.google.android.gms.permission.AD_ID permission and is able to access the Advertising ID whenever it’s available.

For apps that use the Google Mobile Ads SDK version 20.3.0 or lower and are targeting Android S, you must add the com.google.android.gms.permission.AD_ID permission in the AndroidManifest.xml file in order to target Android S:

To learn more about the com.google.android.gms.permission.AD_ID permission declaration, including how to disable it, please refer to this Play Console article.

Initialize the Google Mobile Ads SDK

Before loading ads, have your app initialize the Google 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.

Читайте также:  Android studio перерисовка view

Ads may be preloaded by the Google Mobile Ads SDK or mediation partner SDKs upon calling MobileAds.initialize() . If you need to obtain consent from users in the European Economic Area (EEA), set any request-specific flags (such as tagForChildDirectedTreatment or tag_for_under_age_of_consent ), or otherwise take action before loading ads, ensure you do so before initializing the Google Mobile Ads SDK.

Here’s an example of how to call the initialize() method in an Activity:

Example MainActivity (excerpt)

Kotlin

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 Google 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.

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.

Rewarded

Ads that reward users for watching short videos and interacting with playable ads and surveys. Used for monetizing free-to-play apps.

Additional resources

The Google Mobile Ads repository on GitHub demonstrates how to use the different ad formats that this API offers.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

App Open Ads

This guide is intended for publishers integrating app open ads using the Google Mobile Ads Android SDK.

App open ads are a special ad format intended for publishers wishing to monetize their app load screens. App open ads can be closed at any time, and are designed to be shown when your users bring your app to the foreground.

App open ads automatically show a small branding area so users know they’re in your app. Here is an example of what an app open ad looks like:

At a high level these are the important steps:

  1. Extend the Application class to initialize the GMA SDK.
  2. Create a utility class that loads an ad before you need to display it.
  3. Load an ad.
  4. Listen for ActivityLifecycleCallbacks.
  5. Show the ad and handle callbacks.
  6. Implement and register the LifecycleObserver interface to show an ad during foregrounding events.

Prerequisites

  • Google Mobile Ads SDK 19.4.0 or higher.
  • Follow the setup instructions in the Get Started guide.

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

The easiest way to load test ads is to use our dedicated test ad unit ID for app open ads:

It’s been specially configured to return test ads for every request, and you’re free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK’s test ads work, see Test Ads.

Extend the Application class

Create a new class called MyApplication and add the following code:

This will initialize the SDK and provide the skeleton where you’ll later register for app foregrounding events.

Next, add the following code to your AndroidManifest.xml :

Be sure to reference your actual package name.

Implement your utility class

Your ad should show quickly, so it’s best to load your ad before you need to display it. That way, you’ll have an ad ready to go as soon as your user enters into your app. Implement a utility class to make ad requests ahead of when you need to show the ad.

Читайте также:  Firewall для андроид без рут

Create a new class called AppOpenManager and fill it out as follows:

This class shell manages an instance variable to keep track of a loaded ad, the ad unit ID, and the AppOpenAdLoadCallback .

Now that you have a utility class, you can instantiate it in your MyApplication class:

Load an ad

The next step is to fill out the fetchAd() method. Add the following to your AppOpenManager class:

The AppOpenAdLoadCallback has methods that get called when the AppOpenAd finishes loading.

Keep track of current activity

In order to show the ad, you’ll need an Activity context. To keep track of the most current Activity being used by your user, make your AppOpenManager class implement the Application.ActivityLifecycleCallbacks interface:

By keeping track of the current activity, you have a context to use to show the ad. You now need to register this interface using the registerActivityLifecycleCallbacks Application method in your AppOpenManager constructor.

registerActivityLifecycleCallbacks allows you to listen for all Activity events. By listening for when activities are started and destroyed, you can keep track of a reference to the current Activity , which you then will use in presenting your app open ad.

Show the ad and handle fullscreen callback events

Add the following method to your AppOpenManager class:

This method shows the ad, passing in a FullScreenContentCallback anonymous class to handle events such as when the ad is presented, fails to present, or when it is dismissed. If a user returns to your app after having left it by clicking on an app open ad, it makes sure they’re not presented with another app open ad.

Listen for app foregrounding events

Add the libraries to your gradle file

In order to be notified of app foregrounding events, you need to register a LifecycleObserver . First, edit your application-level build.gradle file to include the LifecycleObserver libraries:

Implement the LifecycleObserver interface

You can listen for foregrounding events in your AppOpenManager class by implementing the LifecycleObserver interface. Edit your class by adding the following:

By registering your LifecycleObserver , your app will be alerted to app launch and foregrounding events and be able to show the ad at the appropriate times.

Consider ad expiration

To ensure you don’t show an expired ad, add a method to the AppOpenManager that checks how long it has been since your ad reference loaded. Then, use that method to check if the ad is still valid. Update your AppOpenManager method like so:

Cold starts and loading screens

The documentation thus far assumes that you only show app open ads when users foreground your app when it is suspended in memory. «Cold starts» occur when your app is launched but was not previously suspended in memory.

An example of a cold start is when a user opens your app for the first time. With cold starts, you won’t have a previously loaded app open ad that’s ready to be shown right away. The delay between when you request an ad and receive an ad back can create a situation where users are able to briefly use your app before being surprised by an out of context ad. This should be avoided because it is a bad user experience.

The preferred way to use app open ads on cold starts is to use a loading screen to load your game or app assets, and to only show the ad from the loading screen. If your app has completed loading and has sent the user to the main content of your app, do not show the ad.

Best practices

App open ads help you monetize your app’s loading screen, when the app first launches and during app switches, but it’s important to keep best practices in mind so that your users enjoy using your app. It’s best to:

  • Show your first app open ad after your users have used your app a few times.
  • Show app open ads during times when your users would otherwise be waiting for your app to load.
  • If you have a loading screen under the app open ad, and your loading screen completes loading before the ad is dismissed, you may want to dismiss your loading screen in the onAdDismissedFullScreenContent() method.

Examples on GitHub

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Читайте также:  Андроид почему включается переадресация

Источник

Get Started

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.

Before you begin

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 of 16 or higher
  • A compileSdkVersion of 28 or higher

Set up your app in your AdMob account

Register your app as an AdMob app by completing the following steps:

Register your app with AdMob. This step creates an AdMob app with a unique AdMob App ID that is needed later in this guide.

Configure your app

In your project-level build.gradle file, include Google’s Maven repository and Maven central repository in both your buildscript and allprojects sections:

Add the dependencies for the Google Mobile Ads SDK to your module’s app-level Gradle file, normally app/build.gradle :

Add your AdMob app ID (identified in the AdMob UI) to your app’s AndroidManifest.xml file. To do so, add a 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.

In a real app, use your actual AdMob app ID, not the one listed above. If you’re just looking to experiment with the SDK in a Hello World app, you can use the sample app ID shown above.

Note also that failure to add the tag as shown above results in a crash with the message:

(Optional) Declare AD_ID permission for previous versions to work with Android S.

If your app uses the Google Mobile Ads SDK version 20.4.0 or higher, you can skip this step since the SDK automatically declares the com.google.android.gms.permission.AD_ID permission and is able to access the Advertising ID whenever it’s available.

For apps that use the Google Mobile Ads SDK version 20.3.0 or lower and are targeting Android S, you must add the com.google.android.gms.permission.AD_ID permission in the AndroidManifest.xml file in order to target Android S:

To learn more about the com.google.android.gms.permission.AD_ID permission declaration, including how to disable it, please refer to this Play Console article.

Initialize the Google Mobile Ads SDK

Before loading ads, have your app initialize the Google 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.

Ads may be preloaded by the Google Mobile Ads SDK or mediation partner SDKs upon calling MobileAds.initialize() . If you need to obtain consent from users in the European Economic Area (EEA), set any request-specific flags (such as tagForChildDirectedTreatment or tag_for_under_age_of_consent ), or otherwise take action before loading ads, ensure you do so before initializing the Google Mobile Ads SDK.

Here’s an example of how to call the initialize() method in an Activity:

Example MainActivity (excerpt)

Kotlin

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 Google Mobile Ads SDK is now imported and you’re ready to implement an ad. AdMob offers a number of different ad formats, so you can choose the one that best fits your app’s user experience.

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.

Rewarded

Ads that reward users for watching short videos and interacting with playable ads and surveys. Used for monetizing free-to-play apps.

Additional resources

The Google Mobile Ads repository on GitHub demonstrates how to use the different ad formats that this API offers.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Оцените статью