Unity Ads Android SDK API reference - Kotlin and Java
Access the Unity Ads SDK public API reference to view available classes, methods, and properties you can use in Kotlin and Java to integrate and control ad behavior in your Android app.
Read time 16 minutesLast updated a month ago
Use this reference for the Unity Ads Android SDK API in Java and Kotlin as you integrate Unity Ads into your Android applications.
Before using this API, ensure that you've imported the library to enable monetization in your app. Refer to Import the UnityAds library for more information.
UnityAdsClasses
BannerAd
The class manages the loading and displaying of banner ads.
BannerAdBannerLoadConfiguration
Encapsulates the options needed to load a .
BannerAdParameter | Description | Required |
|---|---|---|
| The unique identifier for your banner Placement. | Yes |
| The | Yes |
| Ad markup for bidding scenarios. | No |
| Mediation partner information. | No |
| Used by mediation for tracking performances. | No |
| A map of additional key-value parameters. | No |
| A BannerShowListener to handle banner lifecycle events. | No |
val bannerLoadConfiguration = BannerLoadConfiguration.Builder("banner_placement", bannerSize) .withAdMarkup("ad_markup") .withMediationInfo(mediationInfo) .withMediationAdUnitId("mediation_ad_unit_id") .withExtras(mapOf("key" to "value")) .withListener(bannerListener) .build()
BannerLoadConfiguration.Builder
The builder class for creating a instance.
BannerLoadConfigurationConstructor
Builder(placementId: String, bannerSize: BannerSize)
Parameter | Description | Required |
|---|---|---|
| The unique identifier for your banner Placement. | Yes |
| The | Yes |
Returns: A new instance.
BannerLoadConfiguration.BuilderBuilder methods
Method | Parameter | Required | Returns | Description |
|---|---|---|---|---|
| | | | Sets the ad markup and returns the builder for chaining. |
| | | | Sets the mediation info and returns the builder for chaining. |
| | | | Sets the mediation ad unit ID and returns the builder for chaining. |
| | | | Sets the extras map and returns the builder for chaining. |
| | | | Sets the show listener and returns the builder for chaining. |
| None | | | Creates and returns the |
val bannerLoadConfiguration = BannerLoadConfiguration.Builder("banner_placement", bannerSize) .withAdMarkup("ad_markup") .withMediationInfo(mediationInfo) .withMediationAdUnitId("mediation_ad_unit_id") .withExtras(mapOf("key" to "value")) .withListener(bannerListener) .build()
load()
Loads a banner ad with the specified BannerLoadConfiguration.
Parameter | Description | Required |
|---|---|---|
| An instance of | Yes |
| A | Yes |
Returns: (void) - This is an asynchronous method that returns immediately. The result is provided through the callback.
UnitBannerLoadListenerval bannerSize = BannerSize(320, 50)val loadConfig = BannerLoadConfiguration.Builder("banner_placement", bannerSize).build()BannerAd.load(loadConfig, { bannerAd, error -> if (bannerAd != null) { // Banner loaded successfully, you can now add it to your view hierarchy this.bannerAd = bannerAd } else { // Handle load error }})
view
A view property that provides access to the banner ad instance. Use this property to insert the banner ad within your app's existing view hierarchy at the desired location.
Returns: (nullable View) - The Android View containing the banner ad, or null if the ad has not been loaded successfully.
View?// Assuming 'bannerAd' is a loaded BannerAd instance// Assuming 'adPlaceholder' is a ViewGroup (e.g., LinearLayout, FrameLayout) in your layout where you want to display the adbannerAd.view?.let { adPlaceholder.addView(it)}
BannerView
The class was the previous API for loading and displaying banner ads.
BannerViewBannerView.IListener
BannerView.IListenerBannerSize
A data class representing the dimensions of a banner ad.
Parameter | Description | Required |
|---|---|---|
| The width of the banner in pixels. | Yes |
| The height of the banner in pixels. | Yes |
val bannerSize = BannerSize(320, 50)
UnityBannerSize
UnityBannerSizeInitializationConfiguration
A class containing all the settings for SDK initialization. An instance of this class is created using its .
BuilderParameter | Description | Required |
|---|---|---|
| Your unique game identifier from the Unity Ads Monetization dashboard. | Yes |
| A boolean that enables test mode. When true, only test ads are shown. Defaults to false. | No |
| Specifies the level of detail for log output. See | No |
| An object containing information about the mediation partner. See | No |
| A map of additional key-value parameters for advanced configurations. | No |
InitializationConfiguration.Builder
The builder class for creating an instance.
InitializationConfigurationConstructor
Builder(gameId: String)
Parameter | Description | Required |
|---|---|---|
| Your unique game identifier from the Unity Ads Monetization dashboard. | Yes |
Returns: A new instance.
InitializationConfiguration.BuilderBuilder methods
Method | Parameter | Required | Returns | Description |
|---|---|---|---|---|
| | | | Sets the test mode and returns the builder for chaining. |
| | | | Sets the log level and returns the builder for chaining. |
| | | | Sets the mediation info and returns the builder for chaining. |
| | | | Sets the extras map and returns the builder for chaining. |
| None | | | Creates and returns the |
val config = InitializationConfiguration.Builder("YOUR_GAME_ID") .withTestMode(true) .withLogLevel(LogLevel.DEBUG) .withMediationInfo(mediationInfo) .withExtras(mapOf("key" to "value")) .build()
initialize()
Initializes the Unity Ads SDK with a specified configuration. The is called when initialization completes with either success or failure.
InitializationListenerParameter | Description | Required |
|---|---|---|
| The | No |
| The | No |
Returns: (void) - This is an asynchronous method that returns immediately. The result is provided through the callback.
UnitInitializationListener@OptIn(UnityAdsExperimental::class)fun initializeUnityAds(context: Context) { val config = InitializationConfiguration.Builder("YOUR_GAME_ID") .withTestMode(true) .build() val listener = InitializationListener { error -> if (error == null) { // Initialization successful } else { // Handle initialization error } } UnityAds.initialize(config, listener)}
InterstitialAd
The class manages the loading and showing of full-screen interstitial ads.
InterstitialAdload()
Loads an interstitial ad with the specified LoadConfiguration.
Parameter | Description | Required |
|---|---|---|
| An instance of | No |
| A | No |
InterstitialAd.load(loadConfig, { interstitialAd, error -> if (interstitialAd != null) { // Ad loaded successfully, ready to be shown this.interstitialAd = interstitialAd } else { // Handle load error }})
show()
Displays a previously loaded interstitial ad.
Parameter | Description | Required |
|---|---|---|
| An instance of | No |
| An optional | No |
Returns: (void) - This method returns immediately after starting the ad display process. Lifecycle events are provided through the callback.
UnitShowListenerinterstitialAd.show(showConfig, interstitialShowListener)
LoadConfiguration
Encapsulates the options for loading and formats.
InterstitialAdRewardedAdParameter | Description | Required |
|---|---|---|
| The unique identifier for your ad Placement, found on the Unity Monetization Dashboard. | Yes |
| Ad markup for bidding scenarios. | No |
| Mediation partner information. | No |
| Used by mediation for tracking performances. | No |
| A map of additional key-value parameters. | No |
val loadConfiguration = LoadConfiguration.Builder("interstitial_placement") .withAdMarkup("ad_markup") .withMediationInfo(mediationInfo) .withMediationAdUnitId("mediation_ad_unit_id") .withExtras(mapOf("key" to "value")) // Calling this multiple time will override the previous value. .build()
LoadConfiguration.Builder
The builder class for creating a instance.
LoadConfigurationConstructor
Builder(placementId: String)
Parameter | Description | Required |
|---|---|---|
| The unique identifier for your ad Placement, found on the Unity Monetization Dashboard. | Yes |
Returns: A new instance.
LoadConfiguration.BuilderBuilder methods
Method | Parameter | Required | Returns | Description |
|---|---|---|---|---|
| | No | | Sets the ad markup and returns the builder for chaining. |
| | No | | Sets the mediation info and returns the builder for chaining. |
| | No | | Sets the mediation ad unit ID and returns the builder for chaining. |
| | No | | Sets the extras map and returns the builder for chaining. |
| None | No | | Creates and returns the |
UnityAdsLoadOptions
UnityAdsLoadOptionsMediationInfo
A class containing information about the mediation partner being used.
Parameter | Description | Required |
|---|---|---|
| The name of the mediation partner. | Yes |
| The version of the mediation partner's SDK. | Yes |
| The version of the Unity Ads adapter for the mediation partner. | Yes |
val mediationInfo = MediationInfo("partner_name", "1.2.3", "4.5.6")
MediationMetaData
MediationMetaDataRewardedAd
The class manages the loading and showing of rewarded video ads.
RewardedAdload()
Loads a rewarded ad with the specified .
LoadConfigurationParameter | Description | Required |
|---|---|---|
| An instance of | Yes |
| A | Yes |
RewardedAd.load(loadConfig, { rewardedAd, error -> if (rewardedAd != null) { // Ad loaded successfully, ready to be shown this.rewardedAd = rewardedAd } else { // Handle load error }})
show()
Displays a previously loaded rewarded ad.
Parameter | Description | Required |
|---|---|---|
| An instance of | Yes |
| An optional | Yes |
Returns: (void) - This method returns immediately after starting the ad display process. Lifecycle events are provided through the callback.
UnitShowListenerrewardedAd.show(showConfig, rewardedAdShowListener)
ShowConfiguration
Encapsulates additional options for showing and formats.
InterstitialAdRewardedAdParameter | Description | Required |
|---|---|---|
| Used to track completion. | No |
| A map of additional key-value parameters passed at the time of showing an ad. | No |
val showConfiguration = ShowConfiguration.Builder().withCustomRewardString("custom_reward_string").withExtras(mapOf("key" to "value")).build()
ShowConfiguration.Builder
The builder class for creating a instance.
ShowConfigurationConstructor
Builder()
Returns: A new instance.
ShowConfiguration.BuilderBuilder methods
Method | Parameter | Required | Returns | Description |
|---|---|---|---|---|
| | | | Sets the custom reward string and returns the builder for chaining. |
| | | | Sets the extras map and returns the builder for chaining. |
| None | | | Creates and returns the |
UnityAdsShowOptions
UnityAdsShowOptionsTokenConfiguration
A class containing the settings for generating a bidding token.
Parameter | Description | Required |
|---|---|---|
| The AdFormat for which to generate a token (e.g., | Yes |
| An object containing information about the mediation partner. See | No |
| A map of additional key-value parameters. | No |
val tokenConfig = TokenConfiguration.Builder(AdFormat.REWARDED) .withMediationInfo(mediationInfo) .withExtras(extras) .build()
TokenConfiguration.Builder
The builder class for creating a instance.
TokenConfigurationConstructor
Builder(adFormat: AdFormat)
Parameter | Description | Required |
|---|---|---|
| The AdFormat for which to generate a token (e.g., | Yes |
Returns: A new instance.
TokenConfiguration.BuilderBuilder methods
Method | Parameter | Required | Returns | Description |
|---|---|---|---|---|
| | | | Sets the mediation info and returns the builder for chaining. |
| | | | Sets the extras map and returns the builder for chaining. |
| None | | | Creates and returns the |
getToken
Fetches a bidding token based on the provided and passes it to the token listener.
TokenConfigurationParameter | Description | Required |
|---|---|---|
| A | Yes |
| A listener that is called with the token if it's successfully fetched, or null if an error occurs. | Yes |
Returns: (void) - This is an asynchronous method that returns immediately. The result is provided through the callback.
UnitTokenListenerUnityAds.getToken(tokenConfig, listener)
UnityAds
The UnityAds class is a static class that serves as the primary entry point for the SDK. It handles initialization, token retrieval, and provides access to global SDK properties.
Static variables
isInitialized
A boolean property that determines if the Unity Ads SDK is initialized successfully.
Returns: true if the Unity Ads SDK has been successfully initialized.
val isInitialized = UnityAds.isInitialized
version
A string property that returns the current version of the Unity Ads SDK.
Returns: The current installed version of Unity Ads SDK.
val sdkVersion = UnityAds.version
userIdentifier
A string property that represents a unique ID for each user of your app.
Returns: The current value.
UnityAds.userIdentifier = "my-user-id"
Privacy
Use the following methods to set privacy and user consent flags, helping you comply with supported privacy regulations.
setUserConsent()
Displays the user's opt-in choices for personalized ads.
setUserOptOut()
Offers users the option to opt out of data collection and personalized ads.
setNonBehavioral()
Enables the display of contextual (non-personalized) ads.
UnityAds.userConsent = trueUnityAds.userOptOut = falseUnityAds.nonBehavioral = false
MetaData
MetaDataUnityAdsError
class UnityAdsError internal constructor( val code: Int, val message: String)
An exception returned by the public in case of error from Unity Ads.
Parameter | Description |
|---|---|
| The message associated with the error. |
| The code for the error. |
BannerErrorCode
BannerErrorCodeBannerErrorInfo
BannerErrorInfoUnityAds.UnityAdsInitializationError
UnityAds.UnityAdsInitializationErrorUnityAds.UnityAdsLoadError
UnityAds.UnityAdsLoadErrorUnityAds.UnityAdsShowError
UnityAds.UnityAdsShowErrorInterfaces
BannerLoadListener
An interface for handling the result of loading a . This listener is called when a banner ad load request completes, providing either a successfully loaded instance or an error.
BannerAdBannerAdMethod signature
fun interface BannerLoadListener { fun onBannerLoaded(bannerAd: BannerAd?, error: UnityAdsError?)}
Method | Parameters | Description |
|---|---|---|
| | Called when the banner ad load request completes. Exactly one of the parameters will be non-null. |
val bannerLoadListener = BannerLoadListener { bannerAd, error -> if (bannerAd != null) { // Banner loaded successfully this.bannerAd = bannerAd // Add banner.view to your view hierarchy } else { // Handle load error Log.e("UnityAds", "Banner load failed: ${error?.message}") }}
BannerShowListener
An interface for handling show lifecycle events for . This listener is called at various stages during the banner's display lifecycle.
BannerAdMethod signatures
interface BannerShowListener { fun onBannerShown(bannerAd: BannerAd) fun onBannerClicked(bannerAd: BannerAd) fun onBannerFailedToShow(bannerAd: BannerAd, error: UnityAdsError)}
Method | Parameters | Description |
|---|---|---|
| | Called when the banner ad is successfully shown on screen. |
| | Called when the user clicks on the banner ad. |
| | Called when the banner ad fails to show. |
val bannerShowListener = object : BannerShowListener { override fun onBannerShown(bannerAd: BannerAd) { Log.d("UnityAds", "Banner shown") } override fun onBannerClicked(bannerAd: BannerAd) { Log.d("UnityAds", "Banner clicked") } override fun onBannerFailedToShow(bannerAd: BannerAd, error: UnityAdsError) { Log.e("UnityAds", "Banner failed to show: ${error.message}") }}
InitializationListener
An interface for receiving callbacks about the status of SDK initialization. This listener is called when the SDK initialization process completes.
Method signature
fun interface InitializationListener { fun onInitializationComplete(error: UnityAdsError?)}
Method | Parameters | Description |
|---|---|---|
| | Called when SDK initialization completes. If error is null, initialization was successful. |
val listener = InitializationListener { error -> if (error == null) { Log.d("UnityAds", "SDK initialized successfully.") } else { Log.e("UnityAds", "SDK initialization failed: ${error.message}") }}
IUnityAdsInitializationListener
IUnityAdsInitializationListenerInterstitialLoadListener
An interface for handling the result of loading an . This listener is called when an interstitial ad load request completes, providing either a successfully loaded instance or an error.
InterstitialAdInterstitialAdMethod signature
fun interface InterstitialLoadListener { fun onInterstitialLoaded(interstitialAd: InterstitialAd?, error: UnityAdsError?)}
Method | Parameters | Description |
|---|---|---|
| | Called when the interstitial ad load request completes. Exactly one of the parameters will be non-null. |
val interstitialLoadListener = InterstitialLoadListener { interstitialAd, error -> if (interstitialAd != null) { // Interstitial ad loaded successfully this.interstitialAd = interstitialAd // You can now show the ad } else { // Handle load error Log.e("UnityAds", "Interstitial load failed: ${error?.message}") }}
RewardedLoadListener
An interface for handling the result of loading a . This listener is called when a rewarded ad load request completes, providing either a successfully loaded instance or an error.
RewardedAdRewardedAdMethod signature
fun interface RewardedLoadListener { fun onRewardedLoaded(rewardedAd: RewardedAd?, error: UnityAdsError?)}
Method | Parameters | Description |
|---|---|---|
| | Called when the rewarded ad load request completes. Exactly one of the parameters will be non-null. |
val rewardedLoadListener = RewardedLoadListener { rewardedAd, error -> if (rewardedAd != null) { // Rewarded ad loaded successfully this.rewardedAd = rewardedAd // You can now show the ad } else { // Handle load error Log.e("UnityAds", "Rewarded ad load failed: ${error?.message}") }}
IUnityAdsLoadListener
IUnityAdsLoadListenerShowListener
An interface for handling show lifecycle events for or . This generic interface provides callbacks for various stages of the ad display lifecycle.
ShowAdRewardedAdMethod signatures
interface ShowListener<T> { fun onStarted(unityAd: T) fun onClicked(unityAd: T) fun onCompleted(unityAd: T, state: ShowFinishState) fun onFailed(unityAd: T, error: UnityAdsError)}interface InterstitialShowListener : ShowListener<InterstitialAd>// Specialized listener for RewardedAd with reward callbackinterface RewardedShowListener : ShowListener<RewardedAd> { fun onRewarded(unityAd: RewardedAd)}
Method | Parameters | Description |
|---|---|---|
| | Called when the ad begins displaying on screen. |
| | Called when the user clicks on the ad. |
| | Called when the ad finishes showing. For rewarded ads, |
| | Called when the ad fails to show. |
| | Called when the user has earned a reward by watching the rewarded ad to completion. |
val interstitialListener = object : InterstitialShowListener { override fun onStarted(unityAd: InterstitialAd) { // Do something } override fun onClicked(unityAd: InterstitialAd) { // Do something } override fun onCompleted(unityAd: InterstitialAd, state: ShowFinishState) { // Do something } override fun onFailed(unityAd: InterstitialAd, error: UnityAdsError) { // Do something }}val rewardedListener = object : RewardedShowListener { override fun onStarted(unityAd: RewardedAd) { // Do something } override fun onClicked(unityAd: RewardedAd) { // Do something } override fun onCompleted(unityAd: RewardedAd, state: ShowFinishState) { // Do something } override fun onFailed(unityAd: RewardedAd, error: UnityAdsError) { // Do something } override fun onRewarded(unityAd: RewardedAd) { // Do something }}
IUnityAdsShowListener
IUnityAdsShowListenerTokenListener
An interface for receiving the result of a call. This listener is called when a bidding token request completes.
getTokenMethod signature
fun interface TokenListener { fun onTokenReady(token: String?)}
Method | Parameters | Description |
|---|---|---|
| | Called when the token request completes. The token should be passed to your mediation partner's SDK for bidding. |
val listener = TokenListener { token -> if (token != null) { // Pass the token to your mediation partner's SDK Log.d("UnityAds", "Token received: $token") } else { // Handle token retrieval failure Log.e("UnityAds", "Token retrieval failed") }}
onAdExpired
A method available on and instances that allows you to register a callback to handle ad expiration events. This callback is triggered when the ad is no longer valid and should not be shown.
InterstitialAdRewardedAdMethod signature
// On InterstitialAdvar onAdExpired: AdExpiredListener?// On RewardedAdvar onAdExpired: AdExpiredListener?fun interface AdExpiredListener { fun onAdExpired(ad: T) // T is InterstitialAd or RewardedAd}
Parameter | Description |
|---|---|
| (Required) A function that receives the expired ad instance when the ad expires. |
Returns: (void) - This method registers the callback and returns immediately.
Unit// For InterstitialAdinterstitialAd.onAdExpired = AdExpiredListener { ad -> // Handle ad expiration - load a new ad Log.d("UnityAds", "Interstitial ad expired") // Load a new ad}// For RewardedAdrewardedAd.onAdExpired = AdExpiredListener { ad -> // Handle ad expiration - load a new ad Log.d("UnityAds", "Rewarded ad expired") // Load a new ad}
Enums
AdFormat
Enumerated types for different ad formats.
Value | Description |
|---|---|
| A full-screen ad shown at natural breaks in the app, which can be skipped after a few seconds. |
| A full-screen ad that users opt-in to watch in exchange for an in-app reward. |
| An ad that occupies a portion of the screen, typically at the top or bottom. |
LogLevel
Enumerated types for different log verbosity levels.
Value | Description |
|---|---|
| No logs related to Unity Ads should be printed. |
| Default level. Shows standard integration and lifecycle messages. |
| Shows detailed logs for debugging purposes. |
| Essential errors from init/load/show operations, and critical failures to which a developer must pay attention. |
ShowFinishState
Enumerated types describing the final state of an ad after it has been shown.
Value | Description |
|---|---|
| The user skipped the ad before it finished. |
| The ad was watched to completion. For |
UnityAds.UnityAdsShowCompletionState
UnityAds.UnityAdsShowCompletionState