<br />

<br />

This guide shows you how to integrate a mediation adapter with your
Android app.

## Prerequisites

Before you can integrate mediation for an ad format, you need to integrate that
ad format into your app:

- [Banner Ads](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/banner.md.txt)
- [Interstitial Ads](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/interstitial.md.txt)
- [Native Ads](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/native.md.txt)
- [Rewarded Ads](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/rewarded.md.txt)
- [Rewarded Interstitial
  Ads](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/rewarded-interstitial)

New to mediation? Read

[Introduction to mediation](https://support.google.com/admanager/answer/6272813).

## Initialize GMA Next-Gen SDK

The quick start guide shows you how to [initialize the GMA Next-Gen SDK](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/quick-start.md.txt#initialize_the_mobile_ads_sdk).
During that initialization call, mediation adapters also
get initialized. It is important to wait for initialization to complete before
you load ads in order to verify full participation from every ad network on the
first ad request.

> [!IMPORTANT]
> **Important:** Bidding adapters require you to explicitly initialize GMA Next-Gen SDK.

The following sample code shows how you can check each adapter's initialization
status prior to making an ad request.

### Kotlin

```kotlin
import com.google.android.libraries.ads.mobile.sdk.MobileAds
import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize GMA Next-Gen SDK on a background thread.
MobileAds.initialize(this@MainActivity, InitializationConfig.Builder("SAMPLE_APP_ID").build()) {
initializationStatus -\>
for ((adapterName, adapterStatus) in initializationStatus.adapterStatusMap) {
Log.d(
"MyApp",
String.format(
"Adapter name: %s, Status code: %s, Status string: %s, Latency: %d",
adapterName,
adapterStatus.initializationState,
adapterStatus.description,
adapterStatus.latency,
),
)
}
// Adapter initialization is complete.
}
// Other methods on MobileAds can now be called.
}
  }
}
```

### Java

```java
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
import com.google.android.libraries.ads.mobile.sdk.initialization.AdapterStatus;
import com.google.android.libraries.ads.mobile.sdk.initialization.InitializationConfig;

public class MainActivity extends AppCompatActivity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(
() -\> {
// Initialize GMA Next-Gen SDK on a background thread.
MobileAds.initialize(
this,
new InitializationConfig.Builder("SAMPLE_APP_ID")
.build(),
initializationStatus -\> {
Map\<String, AdapterStatus\> adapterStatusMap =
initializationStatus.getAdapterStatusMap();
for (String adapterClass : adapterStatusMap.keySet()) {
AdapterStatus adapterStatus = adapterStatusMap.get(adapterClass);
Log.d(
"MyApp",
String.format(
"Adapter name: %s, Status code: %s, Status description: %s,"
+ " Latency: %d",
adapterClass,
adapterStatus.getInitializationState(),
adapterStatus.getDescription(),
adapterStatus.getLatency()));
}
// Adapter initialization is complete.
});
// Other methods on MobileAds can now be called.
})
.start();
  }
}
```

## Exclude `com.google.android.gms` modules in mediation integrations

Mediation adapters continue to depend on Google Mobile Ads SDK (Legacy). However
,GMA Next-Gen SDK includes all classes required by mediation adapters.
To avoid compile errors related to duplicate symbols, you need to exclude
Google Mobile Ads SDK (Legacy) from being pulled in as a dependency by mediation
adapters.

In your app-level `build.gradle` file, exclude both `play-services-ads` and
`play-services-ads-lite` modules globally from all dependencies:

### Kotlin

```kotlin
configurations.configureEach {
    exclude(group = "com.google.android.gms", module = "play-services-ads")
    exclude(group = "com.google.android.gms", module = "play-services-ads-lite")
}
```

### Groovy

```groovy
configurations.configureEach {
    exclude group: "com.google.android.gms", module: "play-services-ads"
    exclude group: "com.google.android.gms", module: "play-services-ads-lite"
}
```

## Check which ad network adapter class loaded the ad

Here is some sample code that logs the ad network class name for a banner ad:

### Kotlin

```kotlin
BannerAd.load(
  BannerAdRequest.Builder("AD_UNIT_ID", AdSize.BANNER).build(),
  object : AdLoadCallback<BannerAd> {
    override fun onAdLoaded(ad: BannerAd) {
      Log.d(
        "MyApp", "Adapter class name: " +
          ad.getResponseInfo().mediationAdapterClassName
      )
    }
  }
)
```

### Java

```java
BannerAd.load(
  new BannerAdRequest.Builder("AD_UNIT_ID", AdSize.BANNER).build(),
  new AdLoadCallback<BannerAd>() {
    @Override
    public void onAdLoaded(@NonNull BannerAd ad) {
      Log.d("MyApp",
          "Adapter class name: " + ad.getResponseInfo().getMediationAdapterClassName());
    }
  }
);
```

<br />

<br />

## Use banner ads with mediation

Make sure to disable refresh in all third-party ad source UIs for banner ad
units used in mediation. This prevents a
double refresh since Ad Manager also triggers a refresh
based on your banner ad unit's refresh rate.

<br />

## US states privacy laws and GDPR

If you need to comply with the [U.S. states privacy
laws](https://support.google.com/admanager/answer/9561023) or [General Data Protection
Regulation (GDPR)](https://support.google.com/admanager/answer/7666366), follow the
steps in [US state regulations
settings](https://support.google.com/admanager/answer/10115735) or [GDPR
settings](https://support.google.com/admanager/answer/10113006#adding_ad_partners_to_published_gdpr_messages) to add your
mediation partners in Ad Manager Privacy \& messaging's
US states or GDPR ad partners list. Failure to do so can lead to partners
failing to serve ads on your app.

Learn more about enabling [restricted data processing
(RDP)](https://developers.google.com/ad-manager/mobile-ads-sdk/android/next-gen/privacy/us-states) and obtaining GDPR consent with the
[Google User Messaging Platform (UMP) SDK](https://developers.google.com/ad-manager/mobile-ads-sdk/android/privacy.md.txt).