/JetAds

😱 An easy way to implements ads on jetpack compose apps

Primary LanguageKotlinApache License 2.0Apache-2.0

JetAds JetAds

Maven Central License Android API

Easy Ads for Jetpack Compose

Easily integrate and manage ads in your Jetpack Compose apps with a library that simplifies AdMob implementation. It’s like 'Plug and Earn!'

Caution

If you're not using test IDs, remember to add your device/emulator as a test device.

Installation

The easiest way to start using JetAds is to add it as a Gradle dependency in your app module's build.gradle file.

implementation("io.github.pen-drive:jet-ads:<version>")

Tip

There's no need to add the AdMob/Google Ads dependency, as it's already included in your project through transitive dependency.

Add AdMob meta-data

After adding the library, add the following meta-data to your AndroidManifest.xml file:

<manifest>
  <application>
    <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
    <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
  </application>
</manifest>

Tip

Change to your app's APPLICATION_ID! The ID above is a test ID provided by AdMob.

Initialize the ads

class MainActivity : ComponentActivity(),
    AdsInitializer by AdsInitializeFactory.admobInitializer() // <- use delegation ,
{

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initializeAds() // Initialize the ads

        setContent {
            //Compose content
        }
    }
}

Banner AdBanner

To add an adaptive banner, simply place this composable where you want to show it:

AdaptiveBanner("YOUR_BANNER_ID")

To position the banner at the bottom of the screen:

Tip

Banners are typically positioned at the bottom of the screen. To do this, you can:

  Scaffold(
    modifier = Modifier.fillMaxSize(),
    bottomBar = { AdaptiveBanner(AdMobTestIds.ADAPTIVE_BANNER) }) { innerPadding ->
        //Compose content
    }

Interstitial AdInterstitials

Call this composable at some point before showing the Interstitial:

LoadInterstitial("YOUR_INTERSTITIAL_ID")

To show the Interstitial:

val interstitialsController: InterstitialsController = InterstitialsControllerFactory.admobController()
interstitialsController.show("PREVIOUSLY_LOADED_INTERSTITIAL_ID", activityContext)

Rewarded AdRewardeds

Call this composable at some point before showing the Rewarded:

LoadRewarded("YOUR_REWARDED_ID")

To show the Rewarded:

val rewardsController: RewardsController = RewardedControllerFactory.admobController()
rewardsController.show("PREVIOUSLY_LOADED_REWARDED_ID", activity) { rewardedItem ->
    // Logic to handle the reward
}

App Open AdOpen Ads

Add OpenAdSetup to your activity and use the corresponding delegation!

Method to show ads whenever the app enters the foreground:

class MainActivity : ComponentActivity(),
    AdsInitializer by AdsInitializeFactory.admobInitializer(),
    AppOpenAdManager by AppOpenAdManagerFactory.admobAppOpenAdManager() // <-- for app open ads
{
    private var keepSplashScreen = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initializeAds()

        registerAppOpenAd("YOUR_APP_OPEN_ID")  // <-- for app open ads

        setContent {
            //Compose content
        }
    }
}

Method to show ads whenever the app enters the foreground, and on cold start, i.e., the first time the app starts. This is the recommended method:

class MainActivity : ComponentActivity(),
    AdsInitializer by AdsInitializeFactory.admobInitializer(),
    AppOpenAdManager by AppOpenAdManagerFactory.admobAppOpenAdManager()) // <-- for app open ads
{
    private var keepSplashScreen = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initializeAds()

        val splash = installSplashScreen()
        splash.setKeepOnScreenCondition {
            keepSplashScreen
        }

        registerAppOpenAdForColdStart(AdMobTestIds.APP_OPEN, onCloseSplashScreen = {
            keepSplashScreen = false
        }) // <-- for app open ads

        setContent {
            //Compose content
        }
    }
}

Caution

The Show on cold start feature was specifically designed to work only with the SplashScreen API. Using a splash screen as a regular activity may not function as expected.

Note: This behavior may change in future versions.

Test IDs

The library provides the AdMobTestIds object to access all AdMob test IDs:

object AdMobTestIds {
  const val APP_OPEN = "ca-app-pub-3940256099942544/9257395921"
  const val ADAPTIVE_BANNER = "ca-app-pub-3940256099942544/9214589741"
  const val INTERSTITIAL = "ca-app-pub-3940256099942544/1033173712"
  const val REWARDED = "ca-app-pub-3940256099942544/5224354917"
  const val NATIVE = "ca-app-pub-3940256099942544/2247696110"
  const val FIXED_SIZE_BANNER = "ca-app-pub-3940256099942544/6300978111"
  const val REWARDED_INTERSTITIAL = "ca-app-pub-3940256099942544/5354046379"
  const val NATIVE_VIDEO = "ca-app-pub-3940256099942544/1044960115"
}

Tip

Take a look at the app module in this repository; there you can see more advanced ways to use this library.

Logs

This library provides logs only in debug mode. It logs your ad IDs as tags, such as: 'ca-app-pub-3940256099942544/9257395921', allowing you to filter Logcat to view logs specific to each ad.

Upcoming features (possibly)

  • Native ads
  • Mediation

Contributing

Contributions of new features or bug fixes are always welcome. Please submit a new issue or pull request at any time.

When contributing, keep in mind:

  • The library's philosophy is to be easy to use, always hiding complex implementations from users.
  • Contributors must follow the 'Plug and Earn' principle, ensuring that this library remains simple and easy to use for developers.