---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.1
alternate:
  - https://ads.yandex.com/helpcenter/en/support/migration/admob-ya-ads-android-kotlin.md
  - https://ads.yandex.com/helpcenter/ru/support/migration/admob-ya-ads-android-kotlin.md
  - https://ads.yandex.com/helpcenter/zh/support/migration/admob-ya-ads-android-kotlin.md
  - href: https://ads.yandex.com/helpcenter/en/support/migration/admob-ya-ads-android-kotlin.md
    type: text/markdown
    title: Markdown version
  - href: https://ads.yandex.com/helpcenter/en/llms.txt?revision=r22039017
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/en/llms.txt?revision=r22039017

# Migrating from Google AdMob to Yandex Mediation on Android (Kotlin)

To replace Google AdMob ads with Yandex 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

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

  ```java
  import com.google.android.gms.ads.MobileAds
  import com.google.android.gms.ads.initialization.InitializationStatus

  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {
              fun onInitializationComplete(initializationStatus: InitializationStatus?) {

              }
          }
      }
  }
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.common.MobileAds

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

           }
       }
   }
  ```

{% endlist %}


## Ad formats {#format}

### Interstitial {#interstitial}

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

{% list tabs %}

- Google AdMob

  ```java
  import com.google.android.gms.ads.AdRequest
  import com.google.android.gms.ads.LoadAdError
  import com.google.android.gms.ads.interstitial.InterstitialAd
  import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback

  class MainActivity : AppCompatActivity() {
      private var interstitialAd: InterstitialAd? = null

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {
              fun onInitializationComplete(initializationStatus: InitializationStatus?) {
              }
          }
          val adRequest: AdRequest = Builder().build()

          InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
              object : InterstitialAdLoadCallback() {
                  override fun onAdLoaded(interstitialAdLoaded: InterstitialAd) {
                      // The interstitialAd reference will be null until
                      // an ad is loaded.
                      interstitialAd = interstitialAdLoaded
                      Log.i("TAG", "onAdLoaded")
                  }

                  override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                      // Handle the error
                      Log.i("TAG", loadAdError?.toString())
                      interstitialAd = null
                  }
              })
      }
  }
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.common.MobileAds
  import com.yandex.mobile.ads.interstitial.InterstitialAd
  import com.yandex.mobile.ads.common.AdRequest

  class MainActivity : AppCompatActivity() {
      private var interstitialAd: InterstitialAd? = null

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {

          }

          val adRequest: AdRequest = Builder().build()

          interstitialAd = InterstitialAd(this)
          interstitialAd?.setAdUnitId("demo-interstitial-yandex")
          interstitialAd?.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

  ```java
  import com.google.android.gms.ads.AdError
  import com.google.android.gms.ads.FullScreenContentCallback

  interstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
      override fun onAdDismissedFullScreenContent() {
          // Called when fullscreen content is dismissed.
          Log.d("TAG", "The ad was dismissed.")
      }

      override fun onAdFailedToShowFullScreenContent(adError: AdError) {
          // Called when fullscreen content failed to show.
          Log.d("TAG", "The ad failed to show.")
      }

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

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.common.ImpressionData
  import com.yandex.mobile.ads.common.AdRequestError

  interstitialAd?.setInterstitialAdEventListener(object : InterstitialAdEventListener {
      override fun onAdLoaded() {
          Log.i("TAG", "onAdLoaded")
      }

      override fun onAdFailedToLoad(error: AdRequestError) {
          // Handle the error
          Log.i("TAG", error.description)
      }

      override fun onAdDismissed() {
          // Called when an interstitial ad has been dismissed.
          Log.d("TAG", "The ad was dismissed.")
      }

      override fun onAdShown() {
          // Called when an interstitial ad has been shown.
          Log.d("TAG", "The ad was shown.")
      }

      override fun onImpression(impressionData: ImpressionData?) {
          // Called when an impression was tracked
          Log.d("TAG", "The ad imprassion was tracked.")
      }

      override fun onAdClicked() {
          // Called when user clicked on the ad.
          Log.d("TAG", "The ad was clicked.")
      }

      override fun onReturnedToApplication() {
          // Called when user returned to application after click.
          Log.d("TAG", "The ad was clicked.")
      }

      override fun 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 usage. For example, between game levels or after the user completes a task. To display an interstitial ad, use the `show()` method.

{% list tabs %}

- Google AdMob

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

- Yandex Ads

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

{% endlist %}


### Banner {#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

  ```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
// ...
bannerAdView = (BannerAdView) findViewById(R.id.adView)
bannerAdView.setAdUnitId("demo-banner-yandex")
bannerAdView.setAdSize(AdSize.stickySize(320, 50))
// ...
```

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

  ```java
  import com.google.android.gms.ads.AdSize
  import com.google.android.gms.ads.AdView

  val adView = AdView(this)
  adView.setAdSize(AdSize.BANNER)
  adView.adUnitId = "ca-app-pub-3940256099942544/6300978111"
  // TODO: Add adView to your view hierarchy.
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.banner.AdSize
  import com.yandex.mobile.ads.banner.BannerAdView

  // Creating banner through xml
  val adView = (BannerAdView) findViewById(R.id.adView)
  // Software creation of a banner
  // val adView = BannerAdView(this)

  adView.setAdSize(AdSize.stickySize(320))
  adView.setAdUnitId("demo-banner-yandex")
  // TODO: Add adView to your view hierarchy.
  ```

{% endlist %}


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

{% list tabs %}

- Google AdMob

  ```java
  import com.google.android.gms.ads.AdRequest
  import com.google.android.gms.ads.AdView
  import com.google.android.gms.ads.MobileAds
  import com.google.android.gms.ads.initialization.InitializationStatus

  class MainActivity : AppCompatActivity() {
      private var adView: AdView? = null

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {
              fun onInitializationComplete(initializationStatus: InitializationStatus?) {

              }
          }

          adView = findViewById<AdView>(R.id.adView)
          val adRequest = AdRequest.Builder().build()
          adView?.loadAd(adRequest)
      }
  }
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.banner.BannerAdView
  import com.yandex.mobile.ads.common.AdRequest
  import com.yandex.mobile.ads.common.MobileAds

  class MainActivity : AppCompatActivity() {
      private var adView: BannerAdView? = null

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {

          }

          adView = findViewById<BannerAdView>(R.id.adView)
          val adRequest: AdRequest = Builder().build()
          adView?.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

  ```java
  import com.google.android.gms.ads.AdListener
  import com.google.android.gms.ads.LoadAdError

  adView?.adListener = object : AdListener() {
      override fun onAdLoaded() {
          // Code to be executed when an ad finishes loading.
      }

      override fun onAdFailedToLoad(adError: LoadAdError) {
          // Code to be executed when an ad request fails.
      }

      override fun onAdOpened() {
          // Code to be executed when an ad opens an overlay that
          // covers the screen.
      }

      override fun onAdClicked() {
          // Code to be executed when the user clicks on an ad.
      }

      override fun onAdClosed() {
          // Code to be executed when the user is about to return
          // to the app after tapping on an ad.
      }
  }
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.common.AdRequestError
  import com.yandex.mobile.ads.common.ImpressionData

  adView?.setBannerAdEventListener(object : BannerAdEventListener {
      override fun onAdLoaded() {
          // Code to be executed when an ad finishes loading.
      }

      override fun onAdFailedToLoad(error: AdRequestError) {
          // Code to be executed when an ad request fails.
      }

      override fun onAdClicked() {
          // Code to be executed when the user clicks on an ad.
      }

      override fun onImpression(impressionData: ImpressionData?) {
          // Called when an impression was tracked
      }

      override fun onLeftApplication() {
          // Called when user is about to leave application after tapping on an ad.
      }

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

  ```java
  import com.google.android.gms.ads.AdRequest
  import com.google.android.gms.ads.LoadAdError
  import com.google.android.gms.ads.MobileAds
  import com.google.android.gms.ads.initialization.InitializationStatus
  import com.google.android.gms.ads.rewarded.RewardedAd
  import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback

  class MainActivity : AppCompatActivity() {
      private var rewardedAd: RewardedAd? = null

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {
              fun onInitializationComplete(initializationStatus: InitializationStatus?) {
              }
          }

          val adRequest: AdRequest = Builder().build()

          RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
              adRequest, object : RewardedAdLoadCallback() {
                  override fun onAdFailedToLoad(loadAdError: LoadAdError) {
                      // Handle the error.
                      Log.d("TAG", loadAdError?.toString())
                      rewardedAd = null
                  }

                  override fun onAdLoaded(rewardedAd: RewardedAd) {
                      rewardedAd = rewardedAd
                      Log.d("TAG", "Ad was loaded.")
                  }
              })
      }
  }
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.common.AdRequest
  import com.yandex.mobile.ads.common.MobileAds
  import com.yandex.mobile.ads.rewarded.RewardedAd

  class MainActivity : AppCompatActivity() {
      private var rewardedAd: RewardedAd? = null

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          MobileAds.initialize(this) {

          }

          val adRequest: AdRequest = Builder().build()
          rewardedAd = RewardedAd(this)
          rewardedAd?.setAdUnitId("demo-rewarded-yandex")
      }
  }
  ```

{% endlist %}


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

{% list tabs %}

- Google AdMob

  ```java
  import com.google.android.gms.ads.FullScreenContentCallback
  import com.google.android.gms.ads.AdError

  rewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
      override fun onAdShowedFullScreenContent() {
          // Called when ad is shown.
          Log.d(TAG, "Ad was shown.")
      }

      override fun onAdFailedToShowFullScreenContent(adError: AdError) {
          // Called when ad fails to show.
          Log.d(TAG, "Ad failed to show.")
      }

      override fun 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.")
      }
  }
  ```

- Yandex Ads

  ```java
  import com.yandex.mobile.ads.rewarded.RewardedAdEventListener
  import com.yandex.mobile.ads.common.AdRequestError
  import com.yandex.mobile.ads.rewarded.Reward

  rewardedAd?.setRewardedAdEventListener(object : RewardedAdEventListener {
      override fun onAdLoaded() {
          Log.i("TAG", "onAdLoaded")
      }

      override fun onAdFailedToLoad(error: AdRequestError) {
          // Handle the error
          Log.i("TAG", error.description)
      }

      override fun onRewarded(reward: Reward) {
          // Handle the reward.
          Log.d("TAG", "The user earned the reward.")
          val rewardAmount: Int = reward.amount
          val rewardType: String = reward.type
      }

      override fun onAdDismissed() {
          // Called when an interstitial ad has been dismissed.
          Log.d("TAG", "The ad was dismissed.")
      }

      override fun onAdShown() {
          // Called when an interstitial ad has been shown.
          Log.d("TAG", "The ad was shown.")
      }

      override fun onImpression(impressionData: ImpressionData?) {
          // Called when an impression was tracked
          Log.d("TAG", "The ad impression was tracked.")
      }

      override fun onAdClicked() {
          // Called when user clicked on the ad.
          Log.d("TAG", "The ad was clicked.")
      }

      override fun onReturnedToApplication() {
          // Called when user returned to application after click.
          Log.d("TAG", "The ad was clicked.")
      }

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

  ```java
  rewardedAd?.let { ad ->
    ad.show(this, OnUserEarnedRewardListener { rewardItem ->
      // Handle the reward.
      val rewardAmount = rewardItem.amount
      val rewardType = rewardItem.type
      Log.d(TAG, "User earned the reward.")
    })
  } ?: run {
    Log.d(TAG, "The rewarded ad wasn't ready yet.")
  }
  ```

- Yandex Ads

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

{% endlist %}

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


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