Migrating from Google AdMob (ex. AdMob) to Yandex mediation on Android (Kotlin)

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

Integrating the SDK

Add dependencies for the Yandex Mobile Ads SDK to your module's Gradle file at the app level, usually app/build.gradle.

dependencies {
    implementation 'com.google.android.gms:play-services-ads:22.5.0'
}
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'
}

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

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?) {

            }
        }
    }
}
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) {

         }
     }
 }

Ad formats

Interstitial

Loading ads

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

Setting ad callbacks

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

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

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

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

Add an AdView object to a layout

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

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

Specify the required attributes in the code:

// ...
bannerAdView = (BannerAdView) findViewById(R.id.adView)
bannerAdView.setAdUnitId("demo-banner-yandex")
bannerAdView.setAdSize(AdSize.stickySize(context, 320))
// ...
  • adSize: Desired banner size.

  • 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 Partner Interface.

You can also create a BannerAdView object programmatically:

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.
import com.yandex.mobile.ads.banner.AdSize
import com.yandex.mobile.ads.banner.BannerAdView

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

Loading ads

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

Subscribing to ad display events

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

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.
    }
}
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.
    }
})

Rewarded ads

Loading rewarded ad objects

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

Setting ad callbacks

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

Show ads

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

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.")
}
if (rewardedAd?.isLoaded == true) {
    rewardedAd?.show()
} else {
    Log.d("TAG", "The rewarded ad wasn't ready yet.")
}

Contact support

Next