Skip to content

Commit 04177b6

Browse files
authored
Migrate to Applovin (#83)
* Migrate to Applovin :: Patch 1 * Migrate to Applovin :: Patch 2 * Migrate to Applovin :: Patch 3 * Migrate to Applovin :: Patch 4
1 parent d8c6cb7 commit 04177b6

File tree

22 files changed

+182
-325
lines changed

22 files changed

+182
-325
lines changed

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ dependencies {
9191
playstoreImplementation platform(proprietary.fbBom)
9292
playstoreImplementation proprietary.fbA
9393
playstoreImplementation proprietary.fbC
94-
playstoreImplementation proprietary.plA
9594
}
9695

9796
kapt {

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@
5555
android:name="com.google.android.gms.ads.APPLICATION_ID"
5656
android:value="ca-app-pub-5994265132349884~9645142847" />
5757

58+
<meta-data
59+
android:name="applovin.sdk.key"
60+
android:value="HIShVpilJ0_bWZ4mFNgLTkzJBXDODBScGQa478ynIUQut8zdD8QkhpWnWi5cdvFWNIT8gwL_TzvXzCNCTuDGm8"/>
61+
5862
</application>
5963
</manifest>

app/src/main/java/com/shifthackz/aisdv1/app/di/FeatureModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.shifthackz.aisdv1.app.di
22

3-
import com.shifthackz.aisdv1.feature.ads.di.adFeatureModule
3+
import com.shifthackz.aisdv1.feature.ads.di.adFeatureOldModule
44
import com.shifthackz.aisdv1.feature.analytics.di.analyticsModule
55
import com.shifthackz.aisdv1.feature.auth.di.authModule
66
import com.shifthackz.aisdv1.feature.diffusion.di.diffusionModule
77

88
val featureModule = (
9-
adFeatureModule
9+
adFeatureOldModule
1010
+ analyticsModule
1111
+ authModule
1212
+ diffusionModule

dependencies.gradle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ ext {
2020
pagingVersion = '3.1.1'
2121
pagingComposeVersion = '1.0.0-alpha18'
2222
imagePickerVersion = 'v2.0.3'
23-
constraintVersion = '2.1.4'
2423
timberVersion = '5.0.1'
2524
gsonVersion = '2.10.1'
2625
googleMaterialVersion = '1.9.0'
@@ -29,16 +28,13 @@ ext {
2928
onnxruntimeVersion = 'latest.release'
3029

3130
firebaseBomVersion = '32.2.0'
32-
playServicesAdsVersion = '22.2.0'
33-
umpVerson = '2.1.0'
3431

3532
testJunitVersion = '4.13.2'
3633

3734
androidx = [
3835
core : "androidx.core:core-ktx:$coreKtxVersion",
3936
appcompat : "androidx.appcompat:appcompat:$appCompatVersion",
4037
activity : "androidx.activity:activity-ktx:$activityVersion",
41-
constraintLayout : "androidx.constraintlayout:constraintlayout:$constraintVersion",
4238
composeBom : "androidx.compose:compose-bom:$composeBomVersion",
4339
composeRuntime : "androidx.compose.runtime:runtime",
4440
composeMaterial3 : "androidx.compose.material3:material3:$composeMaterialVersion",
@@ -99,8 +95,7 @@ ext {
9995
fbBom: "com.google.firebase:firebase-bom:$firebaseBomVersion",
10096
fbA : "com.google.firebase:firebase-analytics-ktx",
10197
fbC : "com.google.firebase:firebase-crashlytics-ktx",
102-
plA : "com.google.android.gms:play-services-ads:$playServicesAdsVersion",
103-
ump : "com.google.android.ump:user-messaging-platform:$umpVerson",
98+
appL : "com.applovin:applovin-sdk:+"
10499
]
105100
test = [
106101
junit: "junit:junit:$testJunitVersion",

domain/src/main/java/com/shifthackz/aisdv1/domain/feature/ad/AdFeature.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ interface AdFeature {
88
fun initialize(activity: Activity)
99
fun getHomeScreenBannerAd(context: Context): Ad
1010
fun getGalleryDetailBannerAd(context: Context): Ad
11-
fun loadAd(ad: Ad)
1211
fun showRewardedCoinsAd(activity: Activity, rewardCallback: (Int) -> Unit)
1312

1413
data class Ad(
@@ -22,9 +21,8 @@ interface AdFeature {
2221
companion object {
2322
val empty = object : AdFeature {
2423
override fun initialize(activity: Activity) = Unit
25-
override fun getHomeScreenBannerAd(context: Context) = Ad()
26-
override fun getGalleryDetailBannerAd(context: Context) = Ad()
27-
override fun loadAd(ad: Ad) = Unit
24+
override fun getHomeScreenBannerAd(context: Context): Ad = Ad()
25+
override fun getGalleryDetailBannerAd(context: Context): Ad = Ad()
2826
override fun showRewardedCoinsAd(activity: Activity, rewardCallback: (Int) -> Unit) = Unit
2927
}
3028
}

feature/ads/build.gradle

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,10 @@ apply from: "$project.rootDir/gradle/common.gradle"
77

88
android {
99
namespace 'com.shifthackz.aisdv1.feature.ads'
10-
buildTypes {
11-
release {
12-
buildConfigField "String", "BANNER_HOMESCREEN_AD_UNIT_ID", "\"ca-app-pub-5994265132349884/9338284416\""
13-
buildConfigField "String", "BANNER_GALLERY_AD_UNIT_ID", "\"ca-app-pub-5994265132349884/8450925763\""
14-
buildConfigField "String", "COIN_REWARDED_AD_UNIT_ID", "\"ca-app-pub-5994265132349884/2254182493\""
15-
}
16-
debug {
17-
buildConfigField "String", "BANNER_HOMESCREEN_AD_UNIT_ID", "\"ca-app-pub-3940256099942544/2247696110\""
18-
buildConfigField "String", "BANNER_GALLERY_AD_UNIT_ID", "\"ca-app-pub-3940256099942544/2247696110\""
19-
buildConfigField "String", "COIN_REWARDED_AD_UNIT_ID", "\"ca-app-pub-3940256099942544/5224354917\""
20-
}
10+
buildTypes.each {
11+
it.buildConfigField("String", "BANNER_HOME_ID", "\"c66032471f8ba759\"")
12+
it.buildConfigField("String", "BANNER_GALLERY_ID", "\"6522e4b9d477e41f\"")
13+
it.buildConfigField("String", "COIN_REWARDED_ID", "\"d27e36a05fa45186\"")
2114
}
2215
flavorDimensions "type"
2316
productFlavors {
@@ -34,7 +27,5 @@ dependencies {
3427
implementation project(":core:common")
3528
implementation project(":domain")
3629
implementation di.koinCore
37-
playstoreImplementation androidx.constraintLayout
38-
playstoreImplementation proprietary.plA
39-
playstoreImplementation proprietary.ump
30+
playstoreImplementation proprietary.appL
4031
}

feature/ads/src/foss/java/com/shifthackz/aisdv1/feature/ads/AdFeatureImpl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ internal class AdFeatureImpl : AdFeature {
88
override fun initialize(activity: Activity) = Unit
99
override fun getHomeScreenBannerAd(context: Context) = AdFeature.Ad()
1010
override fun getGalleryDetailBannerAd(context: Context) = AdFeature.Ad()
11-
override fun loadAd(ad: AdFeature.Ad) = Unit
1211
override fun showRewardedCoinsAd(activity: Activity, rewardCallback: (Int) -> Unit) = Unit
1312
}

feature/ads/src/foss/java/com/shifthackz/aisdv1/feature/ads/Ump.kt

Lines changed: 0 additions & 3 deletions
This file was deleted.

feature/ads/src/main/java/com/shifthackz/aisdv1/feature/ads/di/AdFeatureModule.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package com.shifthackz.aisdv1.feature.ads.di
22

33
import com.shifthackz.aisdv1.domain.feature.ad.AdFeature
44
import com.shifthackz.aisdv1.feature.ads.AdFeatureImpl
5-
import com.shifthackz.aisdv1.feature.ads.Ump
6-
import org.koin.core.module.dsl.factoryOf
5+
import org.koin.core.module.dsl.singleOf
76
import org.koin.dsl.bind
87
import org.koin.dsl.module
98

10-
val adFeatureModule = module {
11-
factoryOf(::Ump)
12-
factoryOf(::AdFeatureImpl) bind AdFeature::class
9+
val adFeatureOldModule = module {
10+
singleOf(::AdFeatureImpl) bind AdFeature::class
1311
}

feature/ads/src/playstore/java/com/shifthackz/aisdv1/feature/ads/AdFeatureImpl.kt

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,77 @@ package com.shifthackz.aisdv1.feature.ads
22

33
import android.app.Activity
44
import android.content.Context
5-
import android.view.LayoutInflater
6-
import androidx.annotation.LayoutRes
7-
import com.google.android.gms.ads.*
8-
import com.google.android.gms.ads.nativead.NativeAdView
9-
import com.google.android.gms.ads.rewarded.RewardItem
10-
import com.google.android.gms.ads.rewarded.RewardedAd
11-
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
12-
import com.shifthackz.aisdv1.core.common.log.errorLog
5+
import android.view.ViewGroup
6+
import android.widget.FrameLayout
7+
import com.applovin.mediation.MaxAd
8+
import com.applovin.mediation.MaxError
9+
import com.applovin.mediation.MaxReward
10+
import com.applovin.mediation.ads.MaxAdView
11+
import com.applovin.mediation.ads.MaxRewardedAd
12+
import com.applovin.sdk.AppLovinSdk
1313
import com.shifthackz.aisdv1.domain.feature.ad.AdFeature
14-
import org.koin.core.component.KoinComponent
15-
import org.koin.core.component.inject
1614

17-
internal class AdFeatureImpl : AdFeature, KoinComponent {
15+
internal class AdFeatureImpl : AdFeature, LoggableMaxRewardedAdListener {
1816

19-
private val ump: Ump by inject()
20-
21-
private var rewardedAd: RewardedAd? = null
17+
private var rewardedAd: MaxRewardedAd? = null
18+
private var rewardCallback: (Int) -> Unit = {}
2219

2320
override fun initialize(activity: Activity) {
24-
ump.request(activity)
25-
MobileAds.initialize(activity) {
26-
if (BuildConfig.DEBUG) MobileAds.setRequestConfiguration(
27-
RequestConfiguration.Builder()
28-
.setTestDeviceIds(activity.resources.getStringArray(R.array.test_device_ids).asList())
29-
.build()
30-
)
21+
AppLovinSdk.getInstance(activity).mediationProvider = "max"
22+
AppLovinSdk.initializeSdk(activity) {
3123
loadRewardedCoinsAd(activity)
3224
}
3325
}
3426

35-
override fun getHomeScreenBannerAd(context: Context) = AdFeature.Ad(
36-
view = inflateNativeAdView(context, R.layout.native_small_ad_view),
37-
id = BuildConfig.BANNER_HOMESCREEN_AD_UNIT_ID,
38-
)
39-
40-
override fun getGalleryDetailBannerAd(context: Context) = AdFeature.Ad(
41-
view = inflateNativeAdView(context, R.layout.native_small_ad_view),
42-
id = BuildConfig.BANNER_GALLERY_AD_UNIT_ID,
43-
)
27+
override fun getHomeScreenBannerAd(context: Context): AdFeature.Ad {
28+
return loadBannerAd(MaxAdView(BuildConfig.BANNER_HOME_ID, context))
29+
}
4430

45-
override fun loadAd(ad: AdFeature.Ad) {
46-
inflateAdLoader(ad)?.loadAd(AdRequest.Builder().build())
31+
override fun getGalleryDetailBannerAd(context: Context): AdFeature.Ad {
32+
return loadBannerAd(MaxAdView(BuildConfig.BANNER_GALLERY_ID, context))
4733
}
4834

4935
override fun showRewardedCoinsAd(activity: Activity, rewardCallback: (Int) -> Unit) {
50-
val rewardReducer: (RewardItem) -> Unit = { item ->
51-
rewardCallback(item.amount)
52-
loadRewardedCoinsAd(activity)
53-
}
54-
val show: (RewardedAd) -> Unit = { ad ->
55-
ad.show(activity, rewardReducer)
56-
}
36+
this.rewardCallback = rewardCallback
5737
rewardedAd
58-
?.let { ad -> show(ad) }
59-
?: run { loadRewardedCoinsAd(activity) { ad -> show(ad) } }
38+
?.takeIf { it.isReady }
39+
?.showAd()
6040
}
6141

62-
private fun inflateNativeAdView(context: Context, @LayoutRes layoutId: Int): NativeAdView {
63-
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
64-
return inflater.inflate(layoutId, null) as NativeAdView
42+
override fun onAdLoadFailed(p0: String?, p1: MaxError?) {
43+
super.onAdLoadFailed(p0, p1)
44+
rewardedAd?.loadAd()
6545
}
6646

67-
private fun inflateAdLoader(ad: AdFeature.Ad) = ad.view?.context?.let { ctx ->
68-
AdLoader.Builder(ctx, ad.id)
69-
.forNativeAd { nativeAd -> AdMobXmlRenderer().invoke(ad, nativeAd) }
70-
.applyLoggableAdListener(ad.id)
71-
.build()
47+
override fun onAdDisplayFailed(p0: MaxAd?, p1: MaxError?) {
48+
super.onAdDisplayFailed(p0, p1)
49+
rewardedAd?.loadAd()
7250
}
7351

74-
private fun loadRewardedCoinsAd(context: Context, onAdReady: (RewardedAd) -> Unit = {}) {
75-
RewardedAd.load(
76-
context,
77-
BuildConfig.COIN_REWARDED_AD_UNIT_ID,
78-
AdRequest.Builder().build(),
79-
object : RewardedAdLoadCallback() {
80-
override fun onAdLoaded(p0: RewardedAd) {
81-
super.onAdLoaded(p0)
82-
rewardedAd = p0
83-
onAdReady(p0)
84-
}
52+
override fun onUserRewarded(p0: MaxAd?, p1: MaxReward?) {
53+
super.onUserRewarded(p0, p1)
54+
val amount = p1?.amount?.takeIf { it > 0 } ?: 1
55+
rewardCallback(amount)
56+
rewardCallback = {}
57+
}
8558

86-
override fun onAdFailedToLoad(p0: LoadAdError) {
87-
super.onAdFailedToLoad(p0)
88-
errorLog(Exception(p0.message), "${p0.code} - ${p0.cause}")
89-
}
90-
},
59+
private fun loadRewardedCoinsAd(activity: Activity) {
60+
rewardedAd = MaxRewardedAd
61+
.getInstance(BuildConfig.COIN_REWARDED_ID, activity)
62+
.also { ad -> ad.setListener(this) }
63+
.also { ad -> ad.loadAd() }
64+
}
65+
66+
private fun loadBannerAd(adView: MaxAdView): AdFeature.Ad {
67+
adView.loadAd()
68+
adView.setListener(LoggableMaxAdViewListener.factory())
69+
adView.layoutParams = FrameLayout.LayoutParams(
70+
ViewGroup.LayoutParams.MATCH_PARENT,
71+
adView.context.resources.getDimensionPixelSize(R.dimen.ad_banner_height),
72+
)
73+
return AdFeature.Ad(
74+
id = adView.adUnitId,
75+
view = adView,
9176
)
9277
}
9378
}

0 commit comments

Comments
 (0)