Android SDK Initialization
Initialization Timing
The UjuAd SDK must be initialized in your Application class's onCreate() method to ensure it's ready when the app starts and prepared for subsequent ad loading.
Basic Initialization
1. Create Application Class with Initialization
If your app doesn't have a custom Application class, create one:
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
initializeSdk()
}
private fun initializeSdk() {
// Create config object
val config = UjuAdInitConfig(
appId = "YOUR_APP_ID", // Required, app ID
appKey = "YOUR_APP_KEY", // Required, RSA public key from dashboard, pass as-is
channel = "test_channel", // Required, channel ID for stats and revenue
subChannel = "test_sub_channel", // Optional, sub-channel
isDebug = false, // Debug mode, recommended during development
wxAppId = "wx_xxxxx", // WeChat AppId for WeChat ad channel
// Preset strategy
presetStrategyFileName = "placement_config.json",
// Privacy config: developer can pass AndroidId, SDK won't auto-collect
privacyConfig = UjuPrivacyConfig(
androidId = getAndroidId(this)
)
)
// Step 1: Initialize
UjuAdSdk.init(this, config)
// Step 2: Start SDK with callback listener
UjuAdSdk.start(object : BaseInitListener {
override fun onInitSuccess() {
// SDK started successfully, ready to load ads
}
override fun onInitFailed(error: UjuException) {
// Handle initialization failure
Log.e("UjuAd", "SDK init failed: ${error.message}")
}
})
}
}2. Configuration Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
appId | String | Yes | App ID from UjuAd dashboard |
appKey | String | Yes | RSA public key from UjuAd dashboard, pass as-is |
channel | String | Yes | Channel ID for stats and revenue allocation, no default |
subChannel | String | No | Sub-channel for finer segmentation, default "" |
isDebug | Boolean | No | Debug mode toggle, recommended during development, default false |
wxAppId | String | No | WeChat AppId for WeChat ad channel |
presetStrategyFileName | String | No | Preset strategy filename, e.g. "placement_config.json", for local preset ad strategy |
privacyConfig | UjuPrivacyConfig | No | Privacy config, developer can pass AndroidId to prevent SDK auto-collection |
personalization | UjuPersonalizedConfig | No | Personalization config, requires user consent |
3. Personalization Config
UjuPersonalizedConfig provides user data for personalized ad strategies. Must only be passed after obtaining user consent:
| Field | Type | Required | Description |
|---|---|---|---|
userId | String | Yes | Unique user identifier |
userAge | Int | Yes | User age, e.g. 25 |
userGender | Int | No | Gender: 0 unknown (default) / 1 male / 2 female |
userKeywords | List<String> | No | User tag keywords, e.g. ["game","sports"], default empty |
// Example: Pass personalization config after user consent
val personalization = UjuPersonalizedConfig(
userId = "user_123",
userAge = 25,
userGender = 1, // 1=male, 2=female, 0=unknown
userKeywords = listOf("game", "sports")
)
val config = UjuAdInitConfig(
appId = "YOUR_APP_ID",
appKey = "YOUR_APP_KEY", // RSA public key
channel = "YOUR_CHANNEL",
personalization = personalization
)Update Channel Info
After initialization, you can dynamically update channel and sub-channel via updateChannel:
// Update channel info
UjuAdSdk.updateChannel("new_channel", "new_sub_channel")Check Initialization State
You can check the SDK initialization state at any time:
// Check if SDK has completed init (phase 1)
if (UjuAdSdk.isSdkInitialized()) {
// SDK initialized, ready to load ads
}Other Public Methods
Besides init/start/updateChannel, UjuAdSdk provides:
| Method | Return | Description |
|---|---|---|
isSdkInitialized() | Boolean | Whether SDK completed init (phase 1) |
getVersion() | String | Get SDK version |
getOAID() | String | Get device OAID |
getGoogleAdId() | String | Get Google Advertising ID |
getInitializeState() | UjuAdInitStatus | Init state enum (IDLE/INITIALIZING/INITIALIZED/INITIALIZATION_FAILED) |
getAppId() | String | Get current app ID |
destroy() | void | Release SDK resources |
// Get SDK version
val version = UjuAdSdk.getVersion()
Log.d("UjuAd", "SDK version: $version")
// Get initialization state enum
val state = UjuAdSdk.getInitializeState()
when (state) {
UjuAdInitStatus.IDLE -> Log.d("UjuAd", "Not initialized")
UjuAdInitStatus.INITIALIZING -> Log.d("UjuAd", "Initializing")
UjuAdInitStatus.INITIALIZED -> Log.d("UjuAd", "Initialized")
UjuAdInitStatus.INITIALIZATION_FAILED -> Log.d("UjuAd", "Init failed")
}ProGuard Configuration
If your app uses code obfuscation, add the following rules to proguard-rules.pro.
UJU AD-SDK Core Rules (Required)
# UJU AD-SDK ProGuard
-keep class com.ujusdk.**. *AdapterFactory {
public <init>();
*;
}
-keep class com.ujusdk.adsdk.public.base.BaseAdapterFactory { *; }OAID Rules (Required)
# OAID ProGuard
-keep class repeackage.com.uodis.opendevice.aidl.** { *; }
-keep interface repeackage.com.uodis.opendevice.aidl.** { *; }
-keep class repeackage.com.asus.msa.SupplementaryDID.** { *; }
-keep interface repeackage.com.asus.msa.SupplementaryDID.** { *; }
-keep class repeackage.com.bun.lib.** { *; }
-keep interface repeackage.com.bun.lib.** { *; }
-keep class repeackage.com.heytap.openid.** { *; }
-keep interface repeackage.com.heytap.openid.** { *; }
-keep class repeackage.com.samsung.android.deviceidservice.** { *; }
-keep interface repeackage.com.samsung.android.deviceidservice.** { *; }
-keep class repeackage.com.zui.deviceidservice.** { *; }
-keep interface repeackage.com.zui.deviceidservice.** { *; }
-keep class repeackage.com.coolpad.deviceidsupport.** { *; }
-keep interface repeackage.com.coolpad.deviceidsupport.** { *; }
-keep class repeackage.com.android.creator.** { *; }
-keep interface repeackage.com.android.creator.** { *; }
-keep class repeackage.com.google.android.gms.ads.identifier.internal.** { *; }
-keep interface repeackage.com.google.android.gms.ads.identifier.internal.* { *; }
-keep class repeackage.com.oplus.stdid.** {*; }
-keep interface repeackage.com.oplus.stdid.** {*; }
-keep class com.huawei.hms.ads.** {*; }
-keep interface com.huawei.hms.ads.** {*; }
-keep class com.hihonor.ads.** {*; }
-keep interface com.hihonor.ads.** {*; }
-keep class repeackage.com.qiku.id.** { *; }
-keep interface repeackage.com.qiku.id.** { *; }Gromore Aggregation Rules (Optional — skip if not using Gromore)
# Gromore aggregation, skip if not using gromore
-keep class bykvm*.**
-keep class com.bytedance.msdk.adapter.**{ public *; }
-keep class com.bytedance.msdk.api.** {
public *;
}Baidu SDK Rules (Optional — skip if not using Baidu)
# baidu sdk, skip if not using baidu sdk
-ignorewarnings
-dontwarn com.baidu.mobads.sdk.api.**
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class com.baidu.mobads.** { *; }
-keep class com.style.widget.** {*;}
-keep class com.component.** {*;}
-keep class com.baidu.ad.magic.flute.** {*;}
-keep class com.baidu.mobstat.forbes.** {*;}Kuaishou SDK Rules (Optional — skip if not using Kuaishou)
# ks, skip if not using ks sdk
-keep class org.chromium.** {*;}
-keep class org.chromium.** { *; }
-keep class aegon.chrome.** { *; }
-keep class com.kwai.**{ *; }
-keep class com.yxcorp.kuaishou.addfp.android.Orange {*;}
-dontwarn com.kwai.**
-dontwarn com.kwad.**
-dontwarn com.ksad.**
-dontwarn aegon.chrome.**WeChat SDK Rules (Optional — skip if not using WeChat mini-game)
# If integrating WeChat mini-game, add these keep rules
-keep class com.tencent.mm.opensdk.** {
*;
}
-keep class com.tencent.wxop.** {
*;
}
-keep class com.tencent.mm.sdk.** {
*;
}FAQ
Q: What to do if initialization fails?
A: Possible causes:
- Network issues: Check network connection
- Wrong App ID/Key: Verify App ID and App Key
- Device restrictions: Some devices may be restricted
- Version issues: Check SDK version compatibility
Q: How to improve initialization success rate?
A: Recommendations:
- Initialize as early as possible in
Application.onCreate() - Ensure stable network connection
- Avoid heavy operations during initialization
- Handle initialization failure properly
Q: How to handle AndroidId in privacyConfig?
A: Developers should obtain AndroidId and pass it via UjuPrivacyConfig. When provided, the SDK uses the developer-supplied ID instead of auto-collecting device IDs, which helps meet privacy compliance requirements.
Best Practices
- Initialize early: In
Application.onCreate()to ensure ads are ready - Handle failures: Add failure callback to ensure app runs normally on init failure
- Test mode: Use test devices and test ads during development, set
isDebug = true - Monitor init: Monitor initialization success rate in production
- Version management: Keep SDK version up to date
- Privacy compliance: Pass AndroidId via
privacyConfigper privacy policy - ProGuard: Add corresponding rules based on actual ad channels integrated
Next Steps
After SDK initialization, start integrating ad types:
- Banner Ad (Chinese)
- Rewarded Video (Chinese)
- Native Ad (Chinese)
- Interstitial Ad (Chinese)
- Splash Ad (Chinese)
Related Docs
- API Reference (Chinese) — Complete API signatures
