---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://ads.yandex.com/helpcenter/en/support/migration/admob-ya-ads-android-java.md
  - https://ads.yandex.com/helpcenter/ru/support/migration/admob-ya-ads-android-java.md
  - https://ads.yandex.com/helpcenter/zh/support/migration/admob-ya-ads-android-java.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/en/llms.txt

# Migrating from Google AdMob (ex. AdMob) to Yandex Mediation on Android (Java)

To replace Google AdMob (ex. AdMob) ads with Yandex Ads mediation on Android, make the following changes to your code.


## Integrating the SDK {#sdk}

Add dependencies for the Yandex Mobile Ads SDK to your module's [Gradle](https://gradle.org/) file at the app level, usually `app/build.gradle`.

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```bash
  dependencies {
      implementation 'com.google.android.gms:play-services-ads:22.5.0'
  }
  ```

- Yandex Ads

  ```bash
  dependencies {
      implementation 'com.yandex.android:mobileads:6.2.0'
      implementation 'com.yandex.ads.mediation:mobileads-admob:22.5.0.0'
      implementation 'com.google.android.gms:play-services-ads:22.5.0'
  }
  ```

{% endlist %}

Before loading ads, initialize the library using the `initialize()` method:

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  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) {
              }
          });
      }
  }
  ```

- Yandex Ads

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

          MobileAds.initialize(this, new InitializationListener() {
              @Override
              public void onInitializationCompleted() {
              }
          });
      }
  }
  ```

{% endlist %}


## Ad formats {#format}

### Interstitial {#interstitial}

#### Loading ads {#loading-interstitial-ads}

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  // ...
  import com.google.android.gms.ads.interstitial.InterstitialAd;
  import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

  public class MainActivity extends Activity {

      private InterstitialAd mInterstitialAd;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
          @Override
          public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });
        AdRequest adRequest = new AdRequest.Builder().build();

        InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
          new InterstitialAdLoadCallback() {
        @Override
        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
          // The mInterstitialAd reference will be null until
          // an ad is loaded.
          mInterstitialAd = interstitialAd;
          Log.i(TAG, "onAdLoaded");
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          // Handle the error
          Log.i(TAG, loadAdError.getMessage());
          mInterstitialAd = null;
        }
      });
    }
  }
  ```

- Yandex Ads

  ```java
  // ...
  import com.yandex.mobile.ads.interstitial.InterstitialAd;
  import com.yandex.mobile.ads.interstitial.InterstitialAdEventListener;

  public class MainActivity extends Activity {

      private InterstitialAd mInterstitialAd;

      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          MobileAds.initialize(this, new InitializationListener() {
              @Override
              public void onInitializationCompleted() {
              }
          });
          final AdRequest adRequest = new AdRequest.Builder().build();

          mInterstitialAd = new InterstitialAd(this);
          mInterstitialAd.setAdUnitId("demo-interstitial-yandex");
          mInterstitialAd.loadAd(adRequest);
      }
  }
  ```

{% endlist %}

#### Setting ad callbacks {#setting-callbacks-interstitial}

Before displaying an ad, set callbacks to track events related to your ad.


{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
    @Override
    public void onAdDismissedFullScreenContent() {
      // Called when fullscreen content is dismissed.
      Log.d("TAG", "The ad was dismissed.");
    }

    @Override
    public void onAdFailedToShowFullScreenContent(AdError adError) {
      // Called when fullscreen content failed to show.
      Log.d("TAG", "The ad failed to show.");
    }

    @Override
    public void onAdShowedFullScreenContent() {
      // Called when fullscreen content is shown.
      // Make sure to set your reference to null so you don't
      // show it a second time.
      mInterstitialAd = null;
      Log.d("TAG", "The ad was shown.");
    }
  });
  ```

- Yandex Ads

  ```java
  mInterstitialAd.setInterstitialAdEventListener(new InterstitialAdEventListener() {
      @Override
      public void onAdLoaded() {
          Log.i(TAG, "onAdLoaded");
      }

      @Override
      public void onAdFailedToLoad(@NonNull AdRequestError error) {
          // Handle the error
          Log.i(TAG, error.getDescription());
      }

      @Override
      public void onAdDismissed() {
         // Called when an interstitial ad has been dismissed.
         Log.d("TAG", "The ad was dismissed.");
      }

      @Override
      public void onAdShown() {
        // Called when an interstitial ad has been shown.
        Log.d("TAG", "The ad was shown.");
      }

      @Override
      public void onImpression(@Nullable final ImpressionData impressionData) {
           // Called when an impression was tracked
         Log.d("TAG", "The ad imprassion was tracked.");
      }

      @Override
      public void onAdClicked() {
          // Called when user clicked on the ad.
        Log.d("TAG", "The ad was clicked.");
      }

      @Override
      public void onReturnedToApplication() {
      // Called when user returned to application after click.
        Log.d("TAG", "The ad was clicked.");
      }

      @Override
      public void onLeftApplication() {
  // Called when user is about to leave application after tapping on an ad.
      Log.d("TAG", "The ad left application after click.");
      }
  });
  ```

{% endlist %}

#### Show ads {#show-interstitial-ads}

Interstitial ads should be displayed during natural pauses in the app's operation. For example, between game levels or after the user completes a task. To show an interstitial ad, use the `show()` method.

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  if (mInterstitialAd != null) {
    mInterstitialAd.show(MyActivity.this);
  } else {
    Log.d("TAG", "The interstitial ad wasn't ready yet.");
  }
  ```

- Yandex Ads

  ```java
  if (mInterstitialAd.isLoaded()) {
    mInterstitialAd.show();
  } else {
    Log.d("TAG", "The interstitial ad wasn't ready yet.");
  }
  ```

{% endlist %}

### Banner ads {#banner}

#### Add an AdView object to a layout {#adview}

To display a banner, place `BannerAdView` in the appropriate Activity or Fragment. To do this, add it to the corresponding layout XML file:

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```xml
  # main_activity.xml
  ...
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
  ...
  ```

- Yandex Ads

  ```xml
  # main_activity.xml
  ...
    <com.yandex.mobile.ads.banner.BannerAdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">
    </com.yandex.mobile.ads.banner.BannerAdView>
  ...
  ```

{% endlist %}

Specify the required attributes in the code:

```java
// ...
mBannerAdView = (BannerAdView) findViewById(R.id.adView);
mBannerAdView.setAdUnitId("demo-banner-yandex");
mBannerAdView.setAdSize(AdSize.stickySize(context, 320));
// ...
```

- `adSize`: Desired [banner size](https://doc-static.yandex.net/src/support/RSYA-RMP/mobile-ads/ru/javadoc/com/yandex/mobile/ads/banner/BannerAdSize.html).

- `adUnitId`: Ad unit unique ID. If a banner in your app is displayed in different instances of Activity, we recommend creating separate ad unit IDs for each banner in the Yandex Advertising Network interface.

You can also create a `BannerAdView` object programmatically:

{% list tabs %}

- Google AdMob (ex. AdMob)
  ```java
  AdView adView = new AdView(this);
  adView.setAdSize(AdSize.BANNER);
  adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
  // TODO: Add adView to your view hierarchy.
  ```

- Yandex Ads

  ```java
  BannerAdView adView = new BannerAdView(this);
  adView.setAdSize(AdSize.stickySize(context, 320));
  adView.setAdUnitId("demo-banner-yandex");
  // TODO: Add adView to your view hierarchy.
  ```

{% endlist %}

#### Loading ads {#loading-adview}

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  package ...

  import ...
  import com.google.android.gms.ads.adRequest;
  import com.google.android.gms.ads.adView;

  public class MainActivity extends AppCompatActivity {
  private AdView mAdView;

  protected void  onCreate (Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          MobileAds.initialize(this, new OnInitializationCompleteListener() {
              @Override
              public void onInitializationComplete(InitializationStatus initializationStatus) {
              }
          });
          mAdView = findViewById(R.id.adView);
          AdRequest adRequest = new AdRequest.Builder().build();
          mAdView.loadAd(adRequest);
  }
  }
  ```

- Yandex Ads

  ```java
  package ...

  import ...
  import com.yandex.mobile.ads.banner.BannerAdEventListener;
  import com.yandex.mobile.ads.banner.BannerAdView;

  public class MainActivity extends AppCompatActivity {
  private BannerAdView mAdView;

  protected void  onCreate (Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          MobileAds.initialize(this, new InitializationListener() {
              @Override
              public void onInitializationCompleted() {
              }
          });
          mAdView = findViewById(R.id.adView);
          AdRequest adRequest = new AdRequest.Builder().build();
          mAdView.loadAd(adRequest);
  }
  }
  ```

{% endlist %}

#### Subscribing to ad display events {#ad-display-events}

To customize an ad's behavior, you can subscribe to ad lifecycle events.

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  mAdView.setAdListener(new AdListener() {
      @Override
      public void onAdLoaded() {
      // Code to be executed when an ad finishes loading.
    }
      @Override
      public void onAdFailedToLoad(LoadAdError adError) {
      // Code to be executed when an ad request fails.
      }
      @Override
      public void onAdOpened() {
      // Code to be executed when an ad opens an overlay that
      // covers the screen.
      }
      @Override
      public void onAdClicked() {
      // Code to be executed when the user clicks on an ad.
      }
      @Override
      public void onAdClosed() {
      // Code to be executed when the user is about to return
        // to the app after tapping on an ad.
      }
  });
  ```

- Yandex Ads

  ```java
  mAdView.setBannerAdEventListener(new BannerAdEventListener() {
    @Override
    public void onAdLoaded() {
       // Code to be executed when an ad finishes loading.
  }
  @Override
    public onAdFailedToLoad(@NonNull AdRequestError error) {
       // Code to be executed when an ad request fails.
  }
    @Override
    public void onAdClicked() {
        // Code to be executed when the user clicks on an ad.
  }
    @Override
    public void onImpression(@Nullable final ImpressionData impressionData) {
        // Called when an impression was tracked
    }
    @Override
    public void onLeftApplication() {
        // Called when user is about to leave application after tapping on an ad.
    }
  @Override
    public void onReturnedToApplication() {
        // Code to be executed when the user is about to return
        // to the app after tapping on an ad.
  }
  });
  ```

{% endlist %}


### Rewarded ads {#rewarded-ad}

#### Loading rewarded ad objects {#loading-rewarded-ad}

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  import com.google.android.gms.ads.rewarded.RewardedAd;

  public class MainActivity extends Activity {
    private RewardedAd mRewardedAd;
    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

      AdRequest adRequest = new AdRequest.Builder().build();

      RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
        adRequest, new RewardedAdLoadCallback() {
          @Override
          public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            // Handle the error.
            Log.d(TAG, loadAdError.getMessage());
            mRewardedAd = null;
          }

          @Override
          public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
            mRewardedAd = rewardedAd;
            Log.d(TAG, "Ad was loaded.");
          }
      });
    }
  }
  ```

- Yandex Ads

  ```java
  ...
  import com.yandex.mobile.ads.rewarded.RewardedAd;
  import com.yandex.mobile.ads.rewarded.RewardedAdEventListener;

  public class MainActivity extends Activity {
    private RewardedAd mRewardedAd;
    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

      AdRequest adRequest = new AdRequest.Builder().build();

  mRewardedAd = new RewardedAd(this);
      mRewardedAd.setAdUnitId("demo-rewarded-yandex");
    }
  }
  ```

{% endlist %}

#### Setting ad callbacks {#setting-callbacks-rewarded-ad}

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
    @Override
    public void onAdShowedFullScreenContent() {
      // Called when ad is shown.
      Log.d(TAG, "Ad was shown.");
    }

    @Override
    public void onAdFailedToShowFullScreenContent(AdError adError) {
      // Called when ad fails to show.
      Log.d(TAG, "Ad failed to show.");
    }

    @Override
    public void onAdDismissedFullScreenContent() {
      // Called when ad is dismissed.
      // Set the ad reference to null so you don't show the ad a second time.
      Log.d(TAG, "Ad was dismissed.");
      mRewardedAd = null;
    }
  });
  ```

- Yandex Ads

  ```java
  mRewardedAd.setRewardedAdEventListener(new RewardedAdEventListener() {    @Override
      public void onAdLoaded() {
          Log.i(TAG, "onAdLoaded");
      }

      @Override
      public void onAdFailedToLoad(@NonNull AdRequestError error) {
          // Handle the error
          Log.i(TAG, error.getDescription());
      }

      public void onRewarded(@NonNull final Reward reward) {
          // Handle the reward.
          Log.d(TAG, "The user earned the reward.");
          int rewardAmount = rewardItem.getAmount();
          String rewardType = rewardItem.getType();
      }

      @Override
      public void onAdDismissed() {
         // Called when an interstitial ad has been dismissed.
         Log.d("TAG", "The ad was dismissed.");
      }

      @Override
      public void onAdShown() {
         // Called when an interstitial ad has been shown.
         Log.d("TAG", "The ad was shown.");
      }

      @Override
      public void onImpression(@Nullable final ImpressionData impressionData) {
             // Called when an impression was tracked
         Log.d("TAG", "The ad imprassion was tracked.");
      }

      @Override
      public void onAdClicked() {
              // Called when user clicked on the ad.
        Log.d("TAG", "The ad was clicked.");
      }

      @Override
      public void onReturnedToApplication() {
              // Called when user returned to application after click.
        Log.d("TAG", "The ad was clicked.");
      }

      @Override
      public void onLeftApplication() {
              // Called when user is about to leave application after tapping on an ad.
        Log.d("TAG", "The ad left application after click.");
      }
  });
  ```

{% endlist %}

#### Show ads {#show-rewarded-ads}

To display rewarded ads, use the `RewardedAdEventListener` object to handle reward events.

{% list tabs %}

- Google AdMob (ex. AdMob)

  ```java
  if (mRewardedAd != null) {
    Activity activityContext = MainActivity.this;
    mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
      @Override
      public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
        // Handle the reward.
        Log.d(TAG, "The user earned the reward.");
        int rewardAmount = rewardItem.getAmount();
        String rewardType = rewardItem.getType();
      }
    });
  } else {
    Log.d(TAG, "The rewarded ad wasn't ready yet.");
  }
  ```

- Yandex Ads

  ```java
  if (mRewardedAd.isLoaded()) {
    mRewardedAd.show();
  } else {
    Log.d(TAG, "The rewarded ad wasn't ready yet.");
  }
  ```

{% endlist %}


<!-- source: en/support/_includes/support.md -->
[Contact support](../contact){.button}


<!-- endsource: en/support/_includes/support.md -->
