Integration with Android
This comprehensive guide will walk you through setting up the X3M SDK for Android, from prerequisites to initialization.
If you're migrating from another ad mediation platform (like AppLovin MAX or IronSource LevelPlay), check out our Migration Guides for step-by-step instructions to make the transition smooth.
Prerequisites
- Min SDK Version
23
or higher - Compile SDK Version
34
or higher - Android Gradle Plugin
8.6.1
or higher
Get Your Configuration
Once the application is registered in the system and its status is set to RUNNING, the configuration details can be obtained. These details will be used when setting up ads in the application.
To obtain this configuration, log in with your credentials to the admin panel. Navigate to Applications click on the name of the application you're integrating
After clicking on the name of the application you'll find the information in the App Details tab.
If the application is not yet in the RUNNING status, the integration can be performed using the test IDs. Once the application is configured, replace the test IDs with the production IDs obtained in this step.
Add X3M SDK and Loomit Adapters
X3M's Maven Repository
In the project level gradle configuration file, include X3M's Maven repository:
- build.gradle
- build.gradle.kts
allprojects {
buildscript {
// [...]
}
repositories {
// [...]
maven { url "https://android-artifact-registry.x3mads.com/maven" }
}
}
allprojects {
buildscript {
// [...]
}
repositories {
// [...]
maven {
url = URI("https://android-artifact-registry.x3mads.com/maven")
}
}
}
XMediator Core Dependency
In the app-level gradle configuration file, add the following dependency:
- app/build.gradle
- app/build.gradle.kts
dependencies {
implementation 'com.x3mads.android.xmediator:core:+'
}
dependencies {
implementation("com.x3mads.android.xmediator:core:+")
}
Add Your Google Ads App ID to the Android Manifest
XMediator utilizes Google Ads services to obtain the GAID (Google Advertising ID) required by some mediation networks to serve ads.
In your app's manifest add the following metadata with your own Google Ads App ID obtained in the previous step:
- app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<!-- [...] -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy" />
</application>
</manifest>
Choose Mediators and Networks
Use the following tool to generate compatible dependencies for the Networks and Mediation SDKs of your choice.
If you're looking to integrate an ad network that isn't currently listed, please contact our team or your account representative to discuss support and integration options.
If you are using AppLovin/MAX adapter v12.4.0 or newer through LevelPlay mediation, please be sure to remove any entry for applovin.sdk.key
from your AndroidManifest.xml
to avoid any compatibility issues with AppLovin's new initialization API. For more information, please visit AppLovin's documentation.
Initialize the SDK
Before requesting any ad, make sure to call XMediatorAds.startWith()
(or platform equivalent). This method configures your application Key, and fetches the required configuration parameters for the ad placements in your app, among other things.
Additionally, you may want to wait for the initialization callback to complete. This will ensure that your placement requests have the necessary prebid configurations.
If you previously used a different mediation platform, remove any related initialization and configuration code, and ensure there is no interaction with any of its code while Loomit is active.
Example Implementation
In addition to the example below, you can see a real implementation in our Android Demo App.
- Kotlin
- Java
XMediatorAds.startWith(
activity = activity,
appKey = "<your-app-key>",
initSettings = InitSettings(
userProperties = UserProperties(
userId = "<your-user-id>",
customProperties = buildCustomProperties {
addString("key1", "initialValue")
addInt("key2", 100)
addBoolean("key3", true)
}
),
),
initCallback = {
Log.d("X3M", "Initialization complete!")
},
)
XMediatorAds.startWith(
activity,
"<your-app-key>",
new InitSettings.Builder()
.setUserProperties(new UserProperties("<your-user-id>", new CustomProperties.Builder()
.addString("key1", "initialValue")
.addInt("key2", 100)
.addBoolean("key3", true)
.build()))
.build(),
initResult -> {
Log.d("X3M", "Initialization complete!")
return Unit.INSTANCE;
}
);
To ensure the SDK is correctly configured before ad mediation begins, any method that performs an ad request will raise an exception if they are called before XMediatorAds.startWith()
is invoked.
It is mandatory to wait for the init callback to complete before making any ad request.
Update User Properties
After the SDK has been initialized, you can update the user properties (and custom properties) at any time. You may modify, add, or remove properties as needed.
To ensure you do not overwrite the entire configuration, always follow these steps:
- Retrieve the current user properties.
- Edit only the properties you want to change using the editor.
- Pass the updated properties to
setUserProperties
to apply your changes.
Below you can see how to do this:
- Kotlin
- Java
XMediatorAds.setUserProperties(
XMediatorAds.getUserProperties()
.editCustomProperties { editor ->
// Modify key1
editor.addString("key1", "updatedValue")
// Remove key2
editor.remove("key2")
// key3 remains unchanged
// Add new key4
editor.addStringSet("key4", setOf("new1", "new2"))
}
)
XMediatorAds.setUserProperties(
UserPropertiesExtensions.editCustomPropertiesOf(
XMediatorAds.getUserProperties(),
editor -> {
// Modify key1
editor.addString("key1", "updatedValue");
// Remove key2
editor.remove("key2");
// key3 remains unchanged
// Add new key4
editor.addStringSet("key4", new HashSet<>(Arrays.asList("new1", "new2")));
}
)
);