Add the Ad SDK Dependency
Add the following dependency to your app-levelbuild.gradle file.
implementation("com.twinalyze:ads:1+")
implementation 'com.twinalyze:ads:1+'
📝 Note:
If you’re using the Ad Analytics SDK, you do not need to add the Analytics SDK dependency separately. The Ad SDK already includes all analytics features, events, and insights. However, you must initialize the SDK in yourApplicationclass.
Initializing Analytics SDK in Android
Initialize the SDK in yourApplication class to start tracking sessions, events, and insights.
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
MyApp.init(
this,
"YOUR_API_KEY",
"YOUR_ORG_ID",
false
)
}
}
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
MyApp.init(
this,
"YOUR_API_KEY",
"YOUR_ORG_ID",
false
);
}
}
✓Best Practice
Always call initialize() inside your
Application class — not inside an Activity — to ensure
complete session tracking.
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
MyApp.init(
this,
"YOUR_API_KEY",
"YOUR_ORG_ID"
)
}
}
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
MyApp.Companion.init(
this,
"YOUR_API_KEY",
"YOUR_ORG_ID"
);
}
}
Initialize Ads in the Launch Activity
RuninitAds() in the launch activity, typically SplashActivity, StartActivity, or MainActivity.
initAds()
fun initAds() {
Ads.init(this, object : AdsInitListner {
override fun onSuccess() {
try {
val data = JSONObject(AppUtil.GetOtherData())
val showAppOpen =
data.optBoolean("isShowAppOpenOnSplash", false)
if (showAppOpen) {
AdsManagerAppOpen.initAd(
this@SplashActivity,
object : AdsInitListner {
override fun onSuccess() {
val intent = Intent(
this@SplashActivity,
StartActivity::class.java
)
startActivity(intent)
}
override fun failedOnAdsInit(
error: ErrorType?,
msg: String?
) {
val intent = Intent(
this@SplashActivity,
StartActivity::class.java
)
startActivity(intent)
}
}
)
} else {
val intent = Intent(
this@SplashActivity,
StartActivity::class.java
)
startActivity(intent)
}
} catch (e: JSONException) {
e.printStackTrace()
val intent = Intent(
this@SplashActivity,
StartActivity::class.java
)
startActivity(intent)
}
}
override fun failedOnAdsInit(
error: ErrorType?,
msg: String?
) {
if (error == ErrorType.NO_INTERNET) {
runOnUiThread {
Toast.makeText(
applicationContext,
"checkInternet",
Toast.LENGTH_SHORT
).show()
}
}
val intent = Intent(
this@SplashActivity,
StartActivity::class.java
)
startActivity(intent)
}
})
}
private void initAds() {
Ads.init(this, new AdsInitListner() {
@Override
public void onSuccess() {
try {
JSONObject data =
new JSONObject(AppUtil.GetOtherData());
boolean showAppOpen =
data.optBoolean(
"isShowAppOpenOnSplash",
false
);
if (showAppOpen) {
AdsManagerAppOpen.initAd(
SplashActivity.this,
new AdsInitListner() {
@Override
public void onSuccess() {
Intent intent = new Intent(
SplashActivity.this,
StartActivity.class
);
startActivity(intent);
}
@Override
public void failedOnAdsInit(
ErrorType error,
String msg
) {
Intent intent = new Intent(
SplashActivity.this,
StartActivity.class
);
startActivity(intent);
}
}
);
} else {
Intent intent = new Intent(
SplashActivity.this,
StartActivity.class
);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
Intent intent = new Intent(
SplashActivity.this,
StartActivity.class
);
startActivity(intent);
}
}
@Override
public void failedOnAdsInit(
ErrorType error,
String msg
) {
if (error == ErrorType.NO_INTERNET) {
runOnUiThread(() ->
Toast.makeText(
getApplicationContext(),
"checkInternet",
Toast.LENGTH_SHORT
).show()
);
}
Intent intent = new Intent(
SplashActivity.this,
StartActivity.class
);
startActivity(intent);
}
});
}
Add Ad View in XML
Place two ads in one Activity usingapp:position="top" and app:position="bottom".
<com.twinalyze.ads.View.NativeAdView
android:id="@+id/nativeBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:layout_alignParentBottom="true"
android:orientation="vertical"
app:position="bottom" />