Rewarded, interstitial and banner ads from CleanAds. Kotlin, minSdk 21, no
third-party dependencies (only the Android framework and org.json).
// settings.gradle.kts
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://jitpack.io")
}
}
// app/build.gradle.kts
dependencies {
implementation("com.github.cleanads:cleanads-android:1.0.0")
}The library manifest adds the INTERNET permission and declares the full-screen CleanAdsActivity.
ProGuard/R8 consumer rules ship with the AAR, so you don't need to add any.
Get a placement id (pl_...) by creating a free publisher account at cleanads.ai/signup.
class App : Application() {
override fun onCreate() {
super.onCreate()
CleanAds.init(this, test = BuildConfig.DEBUG) // test = true always serves non-billable sample ads
// Local server from the emulator: CleanAds.init(this, apiUrl = "http://10.0.2.2:4000", test = true)
// (plain http also needs android:usesCleartextTraffic="true" in your debug manifest)
}
}CleanAds.showRewarded(activity, "pl_xxx", userId = "42", listener = object : CleanAdsListener {
override fun onShown() {}
override fun onRewarded() { /* user watched long enough */ }
override fun onClosed() {}
override fun onFailed(reason: String) { /* "no_fill", "network_error", ... */ }
})Grant valuable rewards from your server's reward callback (signed, sent once per impression, see
docs/API.md); onRewarded() is for UI feedback. userId is echoed in that callback.
CleanAds.showInterstitial(activity, "pl_xxx", listener = object : CleanAdsListener {
override fun onClosed() = continueGame()
override fun onFailed(reason: String) = continueGame()
})<ai.cleanads.sdk.CleanAdsBannerView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="100dp"
app:cleanadsPlacementId="pl_banner" />or in code:
val banner = CleanAdsBannerView(context)
container.addView(banner, ViewGroup.LayoutParams.MATCH_PARENT, heightPx)
banner.listener = object : CleanAdsBannerView.Listener {
override fun onFailed(reason: String) {}
}
banner.load("pl_banner")The banner stays GONE until an ad loads, and reports an impression only once at least half of it is
actually visible on screen.
- Callbacks run on the main thread. Sequence:
onShown→onRewarded(rewarded only) →onClosed.onFailedis terminal and replaces the others. - Failure reasons: server reasons (
no_fill,unknown_placement,app_not_active,placement_paused,rate_limited, ...) plusnetwork_error,http_<code>,invalid_response,media_error,media_timeout,cancelled,already_showing,activity_destroyed,not_initialized. - Rewarded: countdown of
minViewSeconds(or the video length if shorter). Closing early asks "Close and lose reward?". Interstitial: close button appears afterminViewSeconds. - The countdown pauses while the ad is not in the foreground (e.g. after tapping the CTA).
- Tapping the media or CTA opens the click URL in the browser. Nothing is ever clicked automatically.
- Device id: a random UUID stored in the app's SharedPreferences. No advertising id or hardware ids.
- Network calls use a 10 s timeout. Events are fire-and-forget with one retry.
- Only one full-screen ad is shown at a time. The presenting Activity is held weakly while loading. The ad survives rotation; if the process is killed while it is on screen, it just finishes.
sample/ shows a rewarded, an interstitial and an XML banner placement in test mode.
./gradlew :cleanads:assembleRelease # AAR in cleanads/build/outputs/aar
./gradlew :sample:installDebug