To adopt Android latency and stability improvements, use
GMA Next-Gen SDK. This guide covers configuring Google Mobile Ads Flutter Plugin to use
GMA Next-Gen SDK for Android.

## Prerequisites

Before you continue, do the following:

- Install Google Mobile Ads Flutter Plugin version 9.0.0 or later.
- [Set up Google Mobile Ads Flutter Plugin](https://developers.google.com/ad-manager/mobile-ads-sdk/flutter/quick-start.md.txt).
- Have a Flutter project.

## Configure Android Gradle settings

To use GMA Next-Gen SDK on Android, you must update your Gradle
configurations:

- [Set the minimum and compile Android API levels](https://developers.google.com/admob/android/next-gen/migration.md.txt#set_the_minimum_and_compile_android_api_levels).
- If you use mediation, follow the migration steps to [exclude `com.google.android.gms` modules](https://developers.google.com/admob/android/next-gen/migration.md.txt#exclude_comgoogleandroidgms_modules_in_mediation_integrations) to avoid duplicate class errors.

## Run with environment declaration

To use GMA Next-Gen SDK when building and running your Android app,
you must supply the `USE_NEXT_GEN_SDK` environment declaration set to the
`true` value.

Each time you build or run your app, run the following:

```bash
flutter run --dart-define USE_NEXT_GEN_SDK=true
```

## Run with IDE configuration

If you run your app using an Integrated Development Environment (IDE), you can
configure your environment declarations in your launch configuration instead of
passing the flag using the command line.

For example, you can define these variables in a JSON file and add them to your
IDE setup. For details, see
[specifying environment declarations](https://dart.dev/libraries/core/environment-declarations#specifying-environment-declarations).

## Update native ads templates

If you use native ads templates, make the following updates to support
GMA Next-Gen SDK:

### Update `NativeAdFactory` package import

For Android, update the import statement for the `NativeAdFactory` class:

### Java

```java
// Replace this import:
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory;

// With this import:
import io.flutter.plugins.googlemobileads.NativeAdFactory;
```

### Kotlin

```kotlin
// Replace this import:
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory

// With this import:
import io.flutter.plugins.googlemobileads.NativeAdFactory
```

### Update native ad view layout XML views

In the layout XML files for your native ads, update the native ad view class:

```xml
<!-- Replace this element: -->
<com.google.android.gms.ads.nativead.NativeAdView ... />

<!-- With this element: -->
<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView ... />
```

## Configure dependencies for both setups

If your project must support both Google Mobile Ads SDK (Legacy) and
GMA Next-Gen SDK, you can configure your `android/app/build.gradle`
file to conditionally define dependencies based on the environment declaration.

Google Mobile Ads Flutter Plugin provides a sample configuration in the
[example app `build.gradle` file](https://github.com/googleads/googleads-mobile-flutter/blob/c6718235bcc04631c40ce198edabde8f537a10c2/packages/google_mobile_ads/example/android/app/build.gradle#L36-L50).