Requirements
- Min SDK version 26
- Kotlin 1.9+
- kotlinx-coroutines 1.8.1+
Installation
The SDK is available onmavenCentral()
. Ensure mavenCentral()
is listed in your project’s repository configuration.
build.gradle.kts
file:
Usage
1. Initialization
First, create an instance ofAdsProvider
using the Builder
. This object should be scoped to a single conversation and ideally tied to a lifecycle-aware component, like a ViewModel
.
2. Message Representation
To provide context for ad targeting, your app’s chat messages must be represented in a way the SDK understands. You have two options: Option 1: Conform toMessageRepresentable
You can make your existing message data class conform to the MessageRepresentable
interface. This involves overriding the required properties to map to your class’s fields.
AdsMessage
Data Class
If you cannot or prefer not to modify your existing data class, you can map your message objects to the AdsMessage
type provided by the SDK. AdsMessage
already conforms to MessageRepresentable
.
3. Updating Messages and Collecting Ads
Whenever your list of messages changes, pass the new list to theAdsProvider
.
The adsProvider.ads
property is a kotlinx.coroutines.flow.Flow
that emits a Map<String, List<AdConfig>>
. The map’s key is the message ID, and the value is a list of ads to be displayed for that message.
Collect this flow from a CoroutineScope
to receive and display ads.
4. Displaying Ads
Once you collected the ads Map in your ViewModel, you can use it in your Composable UI to display the ads. TheInlineAd
composable is provided for this purpose. It takes an AdConfig
object and handles the ad rendering.
For View support use InlineAdView
.
5. Lifecycle Management
It is crucial to release the resources used by the SDK. Call theclose()
method when the AdsProvider
is no longer needed.
Example ViewModel Setup
Here is a simplified example of how to integrate theAdsProvider
into an Android ViewModel
. For a complete, working implementation, please see the example
module in this repository.