A lightweight Kotlin-based library to easily integrate Google AdMob Ads into any Android project with minimal setup.
Supports:
- β Banner and Collapsible Banner Ads
- β Interstitial Ads
- β Interstitial Rewarded Ads
- β Rewarded Ads
- β Native Ads
- β App Open Ads
Requirements:
- β Dagger/Hilt
- β Google Ads services (play_services_ads_)
- β Internet π€ͺ
This library uses Dagger Hilt for dependency injection. Please complete the following setup before using the Ads SDK.
Add following depedencies and plugins to libs.versions.toml
Dependencies:
dagger-hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "daggerHilt_version" }
dagger-ksp = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "daggerHilt_version" }
Plugin:
hilt = { id = "com.google.dagger.hilt.android", version.ref = "daggerHilt_version" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp_version" }
Versions:
daggerHilt_version = "2.55"
ksp_version = "2.1.0-1.0.29"
Plugins:
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
Dependencies:
implementation(libs.dagger.hilt)
ksp(libs.dagger.ksp)
Plugin:
alias(libs.plugins.hilt) apply false
alias(libs.plugins.ksp) apply false
@HiltAndroidApp
class MyApp : Application()
<application
android:name=".MyApp"
... >
</application>
To use AdMob (Google Ads), your project must include the Google Services plugin and the Google Mobile Ads SDK.
Dependency:
google-ads = {group = "com.google.android.gms", name = "play-services-ads", version.ref = "google_services_ads_version"}
Versions:
google_services_ads_version = "24.4.0"
implementation(libs.google.ads)
@Inject lateinit var bannerAdManager: BannerAdManager
Use this function to dynamically load and display a Banner Ad inside a container (e.g., FrameLayout), with optional loading text and collapsibility settings.
bannerAdManager.loadAndShowAd(
activity = requireActivity(),
adUnitId = "Your Ad Unit ID",
adViewContainer = yourViewContainer,
loadingView = yourLoadingTextView,
isCollapsible = false,
isCollapsibleAtBottom = true
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | Your current Activity (or requireActivity() inside Fragment). Used for context. |
adUnitId |
String |
β Yes | Your Banner Ad Unit ID from AdMob (test or production). |
adViewContainer |
ViewGroup |
β Yes | The layout container where the banner ad will be injected. Usually a FrameLayout . |
loadingView |
TextView? |
β No | Optional loading text (e.g., "Loading Ad..."). It will be shown until the ad is loaded. |
isCollapsible |
Boolean |
β No | If true, the banner will be collapsible (expand/collapse animation). Default is false . |
isCollapsibleAtBottom |
Boolean |
β No | If isCollapsible is true , setting this to true will collapse from the bottom. Default is true . |
This function preloads multiple banner ads into memory so theyβre ready to display when needed (useful for ViewPagers, RecyclerViews, or dynamic sections in your app).
bannerAdManager.getPreBannerAds(
count = your required ads coun
activity = requireActivity(),
adUnitId = "Your Ad Unit ID"
)
bannerAdManager.showBannerAd(
requireActivity(),
binding.bannerAdViewContainer
)
Parameter | Type | Required | Description |
---|---|---|---|
count |
Int |
β Yes | Number of banner ads to preload. |
activity |
Activity |
β Yes | Activity context used by AdMob SDK. Use requireActivity() in fragments. |
adUnitId |
String |
β Yes | Your Banner Ad Unit ID (from AdMob) used to request ads. |
@Inject lateinit var interstitialADManager: InterstitialAdManager
This function is used to load and show an Interstitial Ad on demand β all in one call. It is best used when you're okay with a short delay while the ad loads and shows immediately afterward.
fun showAdOnDemand(
activity: Activity,
adUnitId: String,
callback: (InterstitialAdCallback) -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | The current activity used to present the ad. |
adUnitId |
String |
β Yes | Your Interstitial Ad Unit ID from AdMob. |
callback |
(InterstitialAdCallback) -> Unit |
β Yes | Lambda that returns ad status results via a sealed class. |
interstitialAdManager.showAdOnDemand(
activity = this,
adUnitId = getString(R.string.interstitial_ad_unit_id)
) { adState ->
when (adState) {
is InterstitialAdCallback.Loaded -> {
Log.d("Ad", "Interstitial Ad Shown")
}
is InterstitialAdCallback.Closed -> {
Log.d("Ad", "Interstitial Ad Closed by user")
}
is InterstitialAdCallback.Failed -> {
Log.e("Ad", "Interstitial Ad Failed to load/show")
}
}
}
This function is used to load an interstitial ad in advance, so it can be shown instantly later using showAdIfAvailable(). This improves user experience by eliminating loading delays when showing the ad.
fun preloadAd(adUnitId: String)
Parameter | Type | Required | Description |
---|---|---|---|
adUnitId |
String |
β Yes | Your Interstitial Ad Unit ID from AdMob. Use the same ID when calling showAdIfAvailable() . |
interstitialAdManager.preloadAd(
adUnitId = getString(R.string.interstitial_ad_unit_id)
)
fun showAdIfAvailable(
activity: Activity,
adUnitId: String,
callback: (InterstitialAdCallback) -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | The current activity context used to display the ad. |
adUnitId |
String |
β Yes | Your Interstitial Ad Unit ID. Must match the one used in preloadAd() . |
callback |
(InterstitialAdCallback) -> Unit |
β Yes | Callback for handling the ad's load/show state and events. |
interstitialAdManager.showAdIfAvailable(
activity = this,
adUnitId = getString(R.string.interstitial_ad_unit_id)
) { adState ->
when (adState) {
is InterstitialAdCallback.Loaded -> {
Log.d("Ad", "Interstitial ad shown.")
}
is InterstitialAdCallback.Closed -> {
Log.d("Ad", "User closed the interstitial ad.")
}
is InterstitialAdCallback.Failed -> {
Log.e("Ad", "No ad available or failed to show.")
}
}
}
@Inject lateinit var appOpenAdManager: AppOpenAdManager
You can set these in your Application class or in Activty class.
Sets whether the interstitial ad should be shown on demand or only if preloaded.
setShowAdOnDemand(isShowAdOnDemand: Boolean)
Name | Type | Description |
---|---|---|
isShowAdOnDemand |
Boolean |
If true , the ad will be loaded and shown immediately using showAdOnDemand() . If false , only preloaded ads will be used (showAdIfAvailable() ). |
Sets a global interstitial ad unit ID, so you donβt need to pass it every time to loadAd() or showAdOnDemand().
setAdUnitId(adUnitId: String)
Name | Type | Description |
---|---|---|
adUnitId |
String |
Your AdMob interstitial ad unit ID. |
Sets a controller to hide or restore your appβs UI elements when the ad appears or disappears. Helpful for removing sensitive or interactive UI during ads.
setAdVisibilityController(adVisibilityController: AdVisibilityController)
Name | Type | Description |
---|---|---|
adVisibilityController |
AdVisibilityController |
Interface instance to control your app's UI visibility during ads. |
If on demand is set true
than Ad will be loaded and than showd otherwise ad will be preloaded and will be shown instantly
@Inject lateinit var nativeAdmanager: NativeAdManager
Copy the layouts and drawables in your projects resource folder so that it will be avaialble to the NativeAdManager
class.
This function loads and immediately displays a native ad inside the given container without requiring any preload setup. Itβs perfect for screens where you need to show a native ad on-the-fly with minimal code.
fun showNativeAdOnDemand(
adUnitId: String,
nativeAdContainer: FrameLayout,
adSize: NativeTemplate = NativeTemplate.MEDIUM
)
Parameter | Type | Required | Description |
---|---|---|---|
adUnitId |
String |
β Yes | Your Native Ad Unit ID from AdMob (test or production). |
nativeAdContainer |
FrameLayout |
β Yes | The container where the native ad view will be displayed. |
adSize |
NativeTemplate |
β No | Enum defining the layout size/style of the native ad. Default is MEDIUM . |
@Inject lateinit var rewardedAdManager: RewardedAdManager
This function loads and immediately shows a rewarded ad. Itβs a one-liner solution for when you want to give users a reward after watching a video ad, without preloading.
private fun loadAndShowAd(
activity: Activity,
adUnitId: String,
callback: (adState: RewardAdCallback) -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | The activity used to show the rewarded ad. |
adUnitId |
String |
β Yes | Your Rewarded Ad Unit ID from AdMob. |
callback |
(RewardAdCallback) -> Unit |
β Yes | Lambda function that returns the ad status, including reward info and failure state. |
This function allows you to load an interstitial ad in advance without showing it immediately. You can later display it using showAdIfAvailable().
fun preLoadAd(
adUnitId: String,
onLoaded: () -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
adUnitId |
String |
β Yes | Your Interstitial Ad Unit ID from AdMob. |
onLoaded |
() -> Unit |
β Yes | Callback triggered only when the ad is successfully loaded. |
rewardInterstitialManager.preLoadAd(
adUnitId = getString(R.string.reward_interstitial_ad_unit_id)
){
// on ad loaded
}
fun showAdIfAvailable(
activity: Activity,
adUnitId: String,
callback: (adState: RewardAdCallback) -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | The current activity used to display the ad. |
adUnitId |
String |
β Yes | Your Rewarded Ad Unit ID from AdMob. |
callback |
(RewardAdCallback) -> Unit |
β Yes | Lambda callback for handling ad states and reward events. |
rewardedAdManager.showAdIfAvailable(
activity = this,
adUnitId = getString(R.string.rewarded_ad_unit_id)
) { adState ->
when (adState) {
is RewardAdCallback.Loaded -> {
Log.d("Ad", "Rewarded ad shown.")
}
is RewardAdCallback.Closed -> {
Log.d("Ad", "User closed the ad.")
}
is RewardAdCallback.Failed -> {
Log.e("Ad", "No ad available to show.")
}
is RewardAdCallback.RewardEarned -> {
Log.d("Ad", "User earned ${adState.amount} ${adState.type}")
// Grant reward here
}
}
}
@Inject lateinit var rewardedInterstitialAdManager: RewardedInterstitialAdManager
This function loads and immediately shows a Rewarded Interstitial Ad, which is a type of rewarded ad that doesnβt require explicit user opt-in (e.g., clicking a button). It combines the reach of interstitial ads with the reward mechanics of rewarded ads.
fun loadAndShowAd(
activity: Activity,
adUnitId: String,
callback: (adState: RewardInterstitialAdCallback) -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | The current activity where the ad should be displayed. |
adUnitId |
String |
β Yes | Your Rewarded Interstitial Ad Unit ID from AdMob. |
callback |
(RewardInterstitialAdCallback) -> Unit |
β Yes | Callback to handle ad events and reward state. |
This function allows you to load a Rewarded Interstitial Ad in advance. Once preloaded, you can later display it using showAdIfAvailable() for a smooth, delay-free user experience.
fun preLoadAd(
adUnitId: String,
onLoaded: () -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
adUnitId |
String |
β Yes | Your Rewarded Interstitial Ad Unit ID from AdMob. |
onLoaded |
() -> Unit |
β Yes | Lambda called only when the ad is successfully loaded and cached. |
rewardInterstitialManager.preLoadAd(
adUnitId = getString(R.string.reward_interstitial_ad_unit_id)
){
// on ad loaded
}
fun showAdIfAvailable(
activity: Activity,
adUnitId: String,
callback: (adState: RewardInterstitialAdCallback) -> Unit
)
Parameter | Type | Required | Description |
---|---|---|---|
activity |
Activity |
β Yes | The current activity used to show the ad. |
adUnitId |
String |
β Yes | The same Rewarded Interstitial Ad Unit ID used during preloading. |
callback |
(RewardInterstitialAdCallback) -> Unit |
β Yes | Callback to listen for ad status and rewards. |
rewardInterstitialManager.showAdIfAvailable(
activity = this,
adUnitId = getString(R.string.reward_interstitial_ad_unit_id)
) { adState ->
when (adState) {
is RewardInterstitialAdCallback.Loaded -> {
Log.d("Ad", "Ad shown.")
}
is RewardInterstitialAdCallback.Closed -> {
Log.d("Ad", "User closed the ad.")
}
is RewardInterstitialAdCallback.Failed -> {
Log.e("Ad", "No ad available.")
}
is RewardInterstitialAdCallback.RewardEarned -> {
Log.d("Ad", "User earned: ${adState.amount} ${adState.type}")
// Grant the reward here
}
}
}