Get Started on Android
Before You Begin ๐
- Create and set up your Chartboost account
- Download and review the Chartboost Sample App
Amazon integration uses the same SDK as Android. These integration instructions apply to games for Google Play or the Amazon App Store.
Requirements and Optional Permissions ๐
- The Chartboost SDK requires API level 21 (Android OS 5) or higher
- The Chartboost Android SDK requires the use of AndroidX
- Required permission:
android.permission.INTERNET
- Required permission:
android.permission.ACCESS_NETWORK_STATE
- Optional (but recommended) permission:
android.permission.READ_PHONE_STATE
Allows the SDK to handle calls that interrupt video playback - Chartboost SDK utilizes ExoPlayer v2.18.7. For errors related to ExoPlayer, review our ExoPlayer dependencies for video ads.
SDK Size ๐
- Defined methods: 4943
- Referenced methods: 5014
- APK size: 471.1 KB
Setting Up Chartboost SDK ๐
1. Add the Chartboost repository and dependencies with the following code to your Gradle file. ๐
repositories {
mavenCentral()
maven { url 'https://cboost.jfrog.io/artifactory/chartboost-ads/' }
}
dependencies {
implementation 'com.chartboost:chartboost-sdk:9.7.0'
}
2. Add the Google Play Services Library as a dependency of your project. When you use our latest SDK, you will need โplay-services-baseโ and โplay-services-ads-identifierโ. ๐
- The Google Play Services library has its own set of integration instructions, including additions to your Android Manifest and ProGuard configuration.
- Follow Googleโs instructions to integrate the Play Services Library.
- You should then find something similar to the following two lines in your
build.gradle
file:
implementation "com.google.android.gms:play-services-base:$project.ext.googlePlayServicesVersion"
implementation "com.google.android.gms:play-services-ads-identifier:$project.ext.googlePlayServicesVersion"
3. Add the following attribute to activities in the AndroidManifest.xml
file if you display Chartboost ads with multiple orientations in those activities. This will help smoothen video playback during orientation changes. ๐
android:configChanges="keyboardHidden|orientation|screenSize"
4. Import the Chartboost SDK into any activity that uses Chartboost with the following: ๐
-
import com.chartboost.sdk.Chartboost import com.chartboost.sdk.ads.Interstitial import com.chartboost.sdk.ads.Rewarded import com.chartboost.sdk.ads.Banner
-
import com.chartboost.sdk.Chartboost; import com.chartboost.sdk.ads.Interstitial; import com.chartboost.sdk.ads.Rewarded; import com.chartboost.sdk.ads.Banner;
If youโre using additional SDK features, youโll also want to import these activities:
-
import com.chartboost.sdk.LoggingLevel import com.chartboost.sdk.Analytics import com.chartboost.sdk.events.CacheError import com.chartboost.sdk.events.CacheEvent import com.chartboost.sdk.events.ClickError import com.chartboost.sdk.events.ClickEvent import com.chartboost.sdk.events.DismissEvent import com.chartboost.sdk.events.ImpressionEvent import com.chartboost.sdk.events.ShowError import com.chartboost.sdk.events.ShowEvent
-
import com.chartboost.sdk.LoggingLevel; import com.chartboost.sdk.Analytics; import com.chartboost.sdk.events.CacheError; import com.chartboost.sdk.events.CacheEvent; import com.chartboost.sdk.events.ClickError; import com.chartboost.sdk.events.ClickEvent; import com.chartboost.sdk.events.DismissEvent; import com.chartboost.sdk.events.ImpressionEvent; import com.chartboost.sdk.events.ShowError; import com.chartboost.sdk.events.ShowEvent;
5. Initialize Chartboost SDK ๐
startWithAppId
must always be called on bootup, regardless of any other actions your app takes.- Publishers should call the
addDataUseConsent
API from the Chartboost SDK and pass in the appropriate value for whether consent exists, does not exist, or is unknown. Publishers are required by the Terms of Service to obtain consent from their users before Chartboost will process any personal data and pass it to the Chartboost SDK via the above method. This method should be called beforestartWithAppId
. startWithAppId
doesnโt require Activity anymore. From version 8.0.1+, the context should be passed instead.
-
Chartboost.startWithAppId(applicationContext, appId, appSignature) { startError -> if (startError == null) { Toast.makeText(this@SelectionActivity.applicationContext, "SDK is initialized", Toast.LENGTH_SHORT).show() checkKnownConsentStatus() } else { Toast.makeText(this@SelectionActivity.applicationContext, "SDK initialized with error: ${startError.code.name()}", Toast.LENGTH_SHORT).show() } }
-
Chartboost.startWithAppId(getApplicationContext(), appId, appSignature, startError -> { if (startError == null) { Toast.makeText(SelectionActivity.this, "SDK is initialized", Toast.LENGTH_SHORT).show(); checkKnownConsentStatus(); } else { Toast.makeText(SelectionActivity.this, "SDK initialized with error: "+startError.getCode().name(), Toast.LENGTH_SHORT).show(); } });
6. Add your app ID and app signature ๐
- Replace
appID
andappSignature
with your app ID and app signature. - Chartboost App ID is a unique App identifier in our systems, therefore it is required to use a different Chartboost App ID per each app.
To Show a Banner Ad ๐
Create a banner object without Mediation object:
-
val chartboostBanner = Banner(context, "location", Banner.BannerSize.STANDARD, object : BannerCallback { override fun onAdLoaded(cacheEvent: CacheEvent, cacheError: CacheError?) { } override fun onAdRequestedToShow(showEvent: ShowEvent) { } override fun onAdShown(showEvent: ShowEvent, showError: ShowError?) { } override fun onAdClicked(clickEvent: ClickEvent, clickError: ClickError?) { } override fun onImpressionRecorded(impressionEvent: ImpressionEvent) { } }, null)
-
Banner chartboostBanner = new Banner(context, "location", Banner.BannerSize.STANDARD, new BannerCallback() { @Override public void onAdLoaded(@NonNull CacheEvent cacheEvent, @Nullable CacheError cacheError) { } @Override public void onAdRequestedToShow(@NonNull ShowEvent showEvent) { } @Override public void onAdShown(@NonNull ShowEvent showEvent, @Nullable ShowError showError) { } @Override public void onAdClicked(@NonNull ClickEvent clickEvent, @Nullable ClickError clickError) { } @Override public void onImpressionRecorded(@NonNull ImpressionEvent impressionEvent) { } }, null);
Create a banner object with mediation:
-
val mediation = Mediation("Mediation", "1.0.0", "1.0.0.1") val chartboostBanner = Banner(context, "location", Banner.BannerSize.STANDARD, callback, mediation)
-
Mediation mediation = new Mediation("Mediation", "1.0.0", "1.0.0.1"); chartboostBanner = new Banner(context, "location", Banner.BannerSize.STANDARD, callback, mediation);
Attach the banner to a holder. The banner object is extending FrameLayout and needs to be attached to the view to be displayed on the screen.
-
val bannerHolder = findViewById<RelativeLayout>(R.id.example_banner_holder).apply { addView(chartboostBanner) }
-
RelativeLayout bannerHolder = findViewById(R.id.example_banner_holder); bannerHolder.addView(chartboostBanner);
CacheBanner:
-
chartboostBanner.cache()
-
chartboostBanner.cache();
Show banner:
-
override fun onAdLoaded(cacheEvent: CacheEvent, cacheError: CacheError?) { if (cacheError != null) { /* Handle error */ } else { chartboostBanner.show() } }
-
public void onAdLoaded(CacheEvent cacheEvent, @Nullable CacheError cacheError) { if (cacheError != null) { /* Handle error */ } else { chartboostBanner.show(); } }
Remove banner:
-
chartboostBanner.detach()
-
chartboostBanner.detach();
Public Banner Errors:
-
val cacheError = CacheError(CacheError.Code.INTERNAL, Exception("banner cache error")) cacheError.code val showError = ShowError(ShowError.Code.INTERNAL, Exception("banner show error")) showError.code val clickError = ClickError(ClickError.Code.INTERNAL, Exception("banner click error")) clickError.code
-
CacheError cacheError = new CacheError(CacheError.Code.INTERNAL, new Exception("banner cache error")); cacheError.getCode(); ShowError showError = new ShowError(ShowError.Code.INTERNAL, new Exception("banner show error")); showError.getCode(); ClickError clickError = new ClickError(ClickError.Code.INTERNAL, new Exception("banner click error")); clickError.getCode();
To Show a Static or Video Interstitial Ad ๐
Create interstitial object without mediation object:
-
val chartboostInterstitial = Interstitial("location", object : InterstitialCallback { override fun onAdDismiss(dismissEvent: DismissEvent) { } override fun onAdLoaded(cacheEvent: CacheEvent, cacheError: CacheError) { // after this is successful ad can be shown } override fun onAdRequestedToShow(showEvent: ShowEvent) { } override fun onAdShown(showEvent: ShowEvent, showError: ShowError?) { } override fun onAdClicked(clickEvent: ClickEvent, clickError: ClickError?) { } override fun onImpressionRecorded(impressionEvent: ImpressionEvent) { } }, null)
-
Interstitial chartboostInterstitial = new Interstitial("location", new InterstitialCallback() { @Override public void onAdDismiss(@NonNull DismissEvent dismissEvent) { } @Override public void onAdLoaded(@NonNull CacheEvent cacheEvent, @Nullable CacheError cacheError) { // after this is successful ad can be shown } @Override public void onAdRequestedToShow(@NonNull ShowEvent showEvent) { } @Override public void onAdShown(@NonNull ShowEvent showEvent, @Nullable ShowError showError) { } @Override public void onAdClicked(@NonNull ClickEvent clickEvent, @Nullable ClickError clickError) { } @Override public void onImpressionRecorded(@NonNull ImpressionEvent impressionEvent) { } }, null);
Create interstitial with mediation object:
-
val mediation = Mediation("Mediation", "1.0.0", "1.0.0.1") val chartboostInterstitial = Interstitial("location", callback, mediation)
-
Mediation mediation = new Mediation("Mediation", "1.0.0", "1.0.0.1"); chartboostInterstitial = new Interstitial("location", callback, mediation);
Cache interstitial:
-
chartboostInterstitial.cache()
-
chartboostInterstitial.cache();
Show interstitial:
-
if (chartboostInterstitial.isCached()) { // check is cached is not mandatory chartboostInterstitial.show() }
-
if (chartboostInterstitial.isCached()) { // check is cached is not mandatory chartboostInterstitial.show(); }
- For customized control over how ads behave in your game, the Chartboost SDK offers delegate methods and named locations.
showInterstitial
directly after cacheInterstitial
for the same location, or the SDK will fail silently.To Show a Rewarded Video Ad ๐
Create a rewarded object without Mediation object:
-
val chartboostRewarded = Rewarded("location", object : RewardedCallback { override fun onRewardEarned(rewardEvent: RewardEvent) { } override fun onAdDismiss(dismissEvent: DismissEvent) { } override fun onAdLoaded(cacheEvent: CacheEvent, cacheError: CacheError?) { // after this is successful ad can be shown } override fun onAdRequestedToShow(showEvent: ShowEvent) { } override fun onAdShown(showEvent: ShowEvent, showError: ShowError?) { } override fun onAdClicked(clickEvent: ClickEvent, clickError: ClickError?) { } override fun onImpressionRecorded(impressionEvent: ImpressionEvent) { } }, null)
-
Rewarded chartboostRewarded = new Rewarded("location", new RewardedCallback() { @Override public void onRewardEarned(@NonNull RewardEvent rewardEvent) { } @Override public void onAdDismiss(@NonNull DismissEvent dismissEvent) { } @Override public void onAdLoaded(@NonNull CacheEvent cacheEvent, @Nullable CacheError cacheError) { // after this is successful ad can be shown } @Override public void onAdRequestedToShow(@NonNull ShowEvent showEvent) { } @Override public void onAdShown(@NonNull ShowEvent showEvent, @Nullable ShowError showError) { } @Override public void onAdClicked(@NonNull ClickEvent clickEvent, @Nullable ClickError clickError) { } @Override public void onImpressionRecorded(@NonNull ImpressionEvent impressionEvent) { } }, null);
Create a rewarded object with Mediation object:
-
val mediation = Mediation("Mediation", "1.0.0", "1.0.0.1") val chartboostRewarded = Rewarded("start", callback, mediation)
-
Mediation mediation = new Mediation("Mediation", "1.0.0", "1.0.0.1"); chartboostRewarded = new Rewarded("start", callback, mediation);
Cache rewarded:
-
chartboostRewarded.cache()
-
chartboostRewarded.cache();
Show rewarded:
-
chartboostRewarded.show()
-
chartboostRewarded.show();
- For customized control over how ads behave in your game, the Chartboost SDK offers delegate methods and named locations.
ExoPlayer Dependency for Video Ads ๐
Chartboost SDK utilizes ExoPlayer v2.18.7. If you have a dependency on ExoPlayer, you may get an error similar to the following:
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
> Duplicate class com.google.android.exoplayer2.ui.DownloadNotificationHelper found in modules exoplayer-core-2.18.7-runtime (com.google.android.exoplayer:exoplayer-core:2.18.7) and exoplayer-ui-2.15.1-runtime (com.google.android.exoplayer:exoplayer-ui:2.15.1)
To resolve this, exclude the ExoPlayer dependency from Chartboost SDK in Gradle:
dependencies {
implementation('com.chartboost:chartboost-sdk:9.5.0') {
exclude group: 'com.google.android.exoplayer'
}
}
Testing Your SDK Integration ๐
- Run your project on an Android device.
- Use Test Mode to see if test ads show up.
SDK Configuration Methods ๐
These methods allow you to access Chartboost SDK functionality and settings.
-
/** * Set the Chartboost SDK logging level. * @param level The logging level. * @return true if was set successfully, false otherwise */ fun setLoggingLevel(level: LoggingLevel)