Set up direct access for Ad Exchange

Stay organized with collections Save and categorize content based on your preferences.

The Google Mobile Ads SDK supports monetizing your apps with an Ad Exchange property. This guide shows you how to configure your apps for all ad formats supported by Ad Exchange.

Prerequisites

Configure your app for accessing Ad Exchange

Add your Ad Manager app ID (identified in the Ad Manager UI) to your app's AndroidManifest.xml file through a <meta-data> tag with android:name="com.google.android.gms.ads.APPLICATION_ID". For android:value, insert your own Ad Manager app ID, surrounded by quotation marks.

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

Note also that failure to add the <meta-data> tag as shown above results in a crash with the message:

The Google Mobile Ads SDK was initialized incorrectly.

Next, you can initialize the Google Mobile Ads SDK and select an ad format to display. The rest of this guide implements the banner format to illustrate how you can load an ad from Ad Exchange. The same steps can apply to any ad formats supported by the Google Mobile Ads SDK.

Load an ad from Ad Exchange

You can use an Ad Exchange web property code with a trailing forward slash, in your app in place of an ad unit ID.

For example, to load a banner ad by placing an AdManagerAdView in the layout for the Activity as follow:

# main_activity.xml
...
  <com.google.android.gms.ads.admanager.AdManagerAdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adManagerAdView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      ads:adSize="BANNER"
      ads:adUnitId="ca-mb-app-pub-5629679302779023/">
  </com.google.android.gms.ads.admanager.AdManagerAdView>
...

Note that failure to add a trailing forward slash to the Ad Exchange web property code as shown above results in an ad request error with the message:

Invalid Request. Cannot determine request type. Is your ad unit id correct?

Note also that you can convert an Ad Exchange web property code into an ad unit. After that you can use the Ad Manager UI to generate an Ad Exchange Tag and copy it into your app. The generated tag should have the Ad Exchange web property code, followed by descendant ad unit IDs without a trailing forward slash, for example: ca-mb-app-pub-5629679302779023/banner

Alternatively, you can create an AdManagerAdView programmatically:

Java

AdManagerAdView adView = new AdManagerAdView(this);

adView.setAdSizes(AdSize.BANNER);
adView.setAdUnitId("ca-mb-app-pub-5629679302779023/");

// TODO: Add adView to your view hierarchy.

Kotlin

val adView = AdManagerAdView(this)

adView.adSizes = AdSize.BANNER
adView.adUnitId = "ca-mb-app-pub-5629679302779023/"

// TODO: Add adView to your view hierarchy.

Once the AdManagerAdView is in place, you can call the loadAd() method in the AdManagerAdView class and customize the behavior of your ad using ad events.

That's it! Your app is now ready to load and display banner ads from Ad Exchange.

In addition, you can use an Ad Exchange web property to load and display other ad formats from Ad Exchange by following respective guides: