Initialize the SDK in Android

Read time 2 minutes

To initialize the SDK, you must reference your Project's Game ID for the appropriate platform. You can locate the ID on the Unity Ads Monetization dashboard by selecting CURRENT PROJECT > Project Settings from the secondary navigation menu.

In your game script, you need to implement an IUnityAdsInitializationListener interface that handles initialization callbacks. The initialize method to initialize the SDK requires this listener as a parameter. Initialize the SDK early in your project's run-time life cycle before you need to load an ad. For example:

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import com.unity3d.ads.example.R;

import com.unity3d.ads.IUnityAdsInitializationListener;
import com.unity3d.ads.UnityAds;

public class InitializeAdsScript extends AppCompatActivity implements IUnityAdsInitializationListener {

  private String unityGameID = "1234567";
  private Boolean testMode = true;

  @Override
  protected void onCreate (Bundle savedInstanceState) {
     super.onCreate (savedInstanceState);
     setContentView (R.layout.activity_main);
     // Initialize the SDK:
     UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this);
  }

  @Override
  public void onInitializationComplete() {
  }

  @Override
  public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
  }
}

For the initialize function, the context parameter is the current Android Context. The unityGameID parameter is the Unity Game ID for your project, found in the Monetization dashboard, specifically the Settings page of your project. The IUnityAdsInitializationListener is the listener for the result of the initialization call. The true Boolean indicates that the game is in test mode, and will only show test ads.

Next steps: To continue your integration, refer to the Implement interstitial ads in Android documentation.