Unity Ads SDK

This commit is contained in:
litingting 2024-07-05 16:43:01 +08:00
parent cf711dd4b3
commit 261f5fa16c
9 changed files with 285 additions and 39 deletions

View File

@ -19,8 +19,8 @@ android {
applicationId = "com.wallart.art.wallpapers.hd" applicationId = "com.wallart.art.wallpapers.hd"
minSdk = 23 minSdk = 23
targetSdk = 34 targetSdk = 34
versionCode = 2 versionCode = 3
versionName = "1.0.1" versionName = "1.0.2"
setProperty("archivesBaseName", "Art Wallpaper_V" + versionName + "(${versionCode})_$timestamp") setProperty("archivesBaseName", "Art Wallpaper_V" + versionName + "(${versionCode})_$timestamp")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }
@ -81,4 +81,7 @@ dependencies {
implementation(platform("com.google.firebase:firebase-bom:32.3.1")) implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
implementation("com.google.firebase:firebase-analytics-ktx") implementation("com.google.firebase:firebase-analytics-ktx")
implementation ("com.google.firebase:firebase-crashlytics-ktx") implementation ("com.google.firebase:firebase-crashlytics-ktx")
//-----------------------------Unity Ads SDK
implementation("com.unity3d.ads:unity-ads:4.9.1")
} }

View File

@ -31,4 +31,40 @@
-dontwarn org.conscrypt.ConscryptHostnameVerifier -dontwarn org.conscrypt.ConscryptHostnameVerifier
-dontwarn org.openjsse.javax.net.ssl.SSLParameters -dontwarn org.openjsse.javax.net.ssl.SSLParameters
-dontwarn org.openjsse.javax.net.ssl.SSLSocket -dontwarn org.openjsse.javax.net.ssl.SSLSocket
-dontwarn org.openjsse.net.ssl.OpenJSSE -dontwarn org.openjsse.net.ssl.OpenJSSE
#=======================================UNity SDK
# Keep filenames and line numbers for stack traces
-keepattributes SourceFile,LineNumberTable
# Keep JavascriptInterface for WebView bridge
-keepattributes JavascriptInterface
-keep class android.webkit.JavascriptInterface {
*;
}
# Keep all classes in Unity Ads package
-keep class com.unity3d.ads.** {
*;
}
# Keep all classes in Unity Services package
-keep class com.unity3d.services.** {
*;
}
-keep class com.google.android.gms.ads.initialization.** {
*;
}
-keep class com.google.android.gms.ads.MobileAds {
*;
}
-dontwarn com.google.ads.mediation.admob.*
-dontwarn com.google.android.gms.ads.**
#==================================UNity SDK

View File

@ -27,11 +27,11 @@
android:exported="true" android:exported="true"
android:screenOrientation="fullSensor" android:screenOrientation="fullSensor"
tools:ignore="DiscouragedApi"> tools:ignore="DiscouragedApi">
<!-- <intent-filter>--> <intent-filter>
<!-- <action android:name="android.intent.action.MAIN" />--> <action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.LAUNCHER" />--> <category android:name="android.intent.category.LAUNCHER" />
<!-- </intent-filter>--> </intent-filter>
</activity> </activity>
<activity <activity
@ -48,14 +48,10 @@
<activity <activity
android:name=".page.MainAbility" android:name=".page.MainAbility"
android:exported="true" android:exported="false"
android:screenOrientation="fullSensor" android:screenOrientation="fullSensor"
tools:ignore="DiscouragedApi" > tools:ignore="DiscouragedApi" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </activity>
<activity <activity

View File

@ -1,9 +1,15 @@
package com.cute.girl.hd.pink.img.wallpaper package com.cute.girl.hd.pink.img.wallpaper
import android.app.Application import android.app.Application
import android.content.Intent
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.cute.girl.hd.pink.img.wallpaper.entity.CategoryData import com.cute.girl.hd.pink.img.wallpaper.entity.CategoryData
import com.cute.girl.hd.pink.img.wallpaper.entity.Data import com.cute.girl.hd.pink.img.wallpaper.entity.Data
import com.cute.girl.hd.pink.img.wallpaper.unityad.UnityAdManager
import com.liulishuo.filedownloader.FileDownloader import com.liulishuo.filedownloader.FileDownloader
import com.unity3d.ads.IUnityAdsInitializationListener
import com.unity3d.ads.UnityAds
import org.json.JSONArray import org.json.JSONArray
import java.io.BufferedReader import java.io.BufferedReader
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -19,8 +25,6 @@ class MyApp : Application() {
companion object { companion object {
lateinit var app: MyApp lateinit var app: MyApp
lateinit var myData: List<CategoryData> lateinit var myData: List<CategoryData>
const val MAX_SDK = "3cUMfTcsZKzlJevxK4IkNysgDAeQA4B5w332p3g8B9ZAgC54WQNZLVxuxnCx4sCHA5StLJnDTAFa68mFTi8rd8"
const val AD_INIT_ACTION = "on_success_action" const val AD_INIT_ACTION = "on_success_action"
var initSDK = false var initSDK = false
} }
@ -107,7 +111,24 @@ class MyApp : Application() {
private fun initSDK() { private fun initSDK() {
Log.d(UnityAdManager.TAG, "----------------------application init")
UnityAds.initialize(this, UnityAdManager.unityGameID, UnityAdManager.testMode, object :
IUnityAdsInitializationListener {
override fun onInitializationComplete() {
LocalBroadcastManager.getInstance(this@MyApp).sendBroadcast(Intent(AD_INIT_ACTION))
initSDK = true
UnityAdManager.loadAllAdNew()
}
override fun onInitializationFailed(
error: UnityAds.UnityAdsInitializationError?,
message: String?
) {
initSDK = false
}
});
} }
} }

View File

@ -17,6 +17,8 @@ import com.cute.girl.hd.pink.img.wallpaper.adapter.ImagePagerAdapter
import com.cute.girl.hd.pink.img.wallpaper.databinding.AbilityPreviewBinding import com.cute.girl.hd.pink.img.wallpaper.databinding.AbilityPreviewBinding
import com.cute.girl.hd.pink.img.wallpaper.entity.Data import com.cute.girl.hd.pink.img.wallpaper.entity.Data
import com.cute.girl.hd.pink.img.wallpaper.entity.IntentConstants import com.cute.girl.hd.pink.img.wallpaper.entity.IntentConstants
import com.cute.girl.hd.pink.img.wallpaper.unityad.UnityAdManager
import com.cute.girl.hd.pink.img.wallpaper.unityad.onShowCallBack
import com.cute.girl.hd.pink.img.wallpaper.utils.DownloadUtil import com.cute.girl.hd.pink.img.wallpaper.utils.DownloadUtil
import com.cute.girl.hd.pink.img.wallpaper.utils.GlobalExt.getString import com.cute.girl.hd.pink.img.wallpaper.utils.GlobalExt.getString
import com.cute.girl.hd.pink.img.wallpaper.utils.GlobalExt.hide import com.cute.girl.hd.pink.img.wallpaper.utils.GlobalExt.hide
@ -41,7 +43,6 @@ class PreviewAbility : AppCompatActivity(), EasyPermissions.PermissionCallbacks
private var mAction = 0//0设置壁纸 1下载壁纸 private var mAction = 0//0设置壁纸 1下载壁纸
companion object { companion object {
fun start(context: Context, bean: Data) { fun start(context: Context, bean: Data) {
val intent = Intent(context, PreviewAbility::class.java) val intent = Intent(context, PreviewAbility::class.java)
@ -60,7 +61,25 @@ class PreviewAbility : AppCompatActivity(), EasyPermissions.PermissionCallbacks
} }
private fun showMyMAx(action: () -> Unit) { private fun showMyMAx(action: () -> Unit) {
action.invoke() val readyUnitAd = UnityAdManager.getReadyUnitAd()
if (readyUnitAd == null) {
action.invoke()
} else {
UnityAdManager.showUnityAd(this, readyUnitAd, object : onShowCallBack {
override fun onShowFail() {
action.invoke()
}
override fun onShowClose() {
action.invoke()
}
override fun onShowSuccess() {
}
})
}
} }

View File

@ -11,14 +11,16 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.cute.girl.hd.pink.img.wallpaper.MyApp import com.cute.girl.hd.pink.img.wallpaper.MyApp
import com.cute.girl.hd.pink.img.wallpaper.databinding.AbilityStartBinding import com.cute.girl.hd.pink.img.wallpaper.databinding.AbilityStartBinding
import com.cute.girl.hd.pink.img.wallpaper.unityad.UnityAdManager
import com.cute.girl.hd.pink.img.wallpaper.unityad.onShowCallBack
class StartAbility : AppCompatActivity() { class StartAbility : AppCompatActivity() {
private lateinit var binding: AbilityStartBinding private lateinit var binding: AbilityStartBinding
private var time =10000L private var time = 13000L
private var needShow = true private var needShow = true
private lateinit var countDownTimer: CountDownTimer private lateinit var countDownTimer: CountDownTimer
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -26,41 +28,42 @@ class StartAbility : AppCompatActivity() {
binding = AbilityStartBinding.inflate(layoutInflater) binding = AbilityStartBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
countDownTimer = object : CountDownTimer(time,100){ countDownTimer = object : CountDownTimer(time, 100) {
override fun onTick(millisUntilFinished: Long) { override fun onTick(millisUntilFinished: Long) {
Log.d(
UnityAdManager.TAG,
"--------millisUntilFinished=${millisUntilFinished} $needShow"
)
if (needShow) { if (needShow) {
ShowAd {} onShowAd({
setupViews()
}, false)
} }
} }
override fun onFinish() { override fun onFinish() {
Log.d(UnityAdManager.TAG, "--------onFinish needShow=${needShow}")
if (needShow) { if (needShow) {
ShowAd { onShowAd({
setupViews() setupViews()
} }, true)
} }
} }
} }
startAd() if (MyApp.initSDK) {
} Log.d(UnityAdManager.TAG, "--------initSDK true")
private fun startAd() { countDownTimer.start()
if (!MyApp.initSDK) { } else {
LocalBroadcastManager.getInstance(this).registerReceiver(object : BroadcastReceiver() { LocalBroadcastManager.getInstance(this).registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
loadAdGo() Log.d(UnityAdManager.TAG, "--------onReceive")
Log.d("------------","------------1sucess") countDownTimer.start()
} }
}, IntentFilter(MyApp.AD_INIT_ACTION))
} else {
loadAdGo()
Log.d("------------","------------2sucess")
}
}
private fun loadAdGo() { }, IntentFilter(MyApp.AD_INIT_ACTION))
// lists = MaxUtils.getAllAd() }
countDownTimer.start()
} }
private fun setupViews() { private fun setupViews() {
@ -68,9 +71,31 @@ class StartAbility : AppCompatActivity() {
finish() finish()
} }
private fun ShowAd(action: () -> Unit) { private fun onShowAd(action: () -> Unit, isFinish: Boolean) {
setupViews() val readyUnitAd = UnityAdManager.getReadyUnitAd()
if (readyUnitAd != null) {
needShow = false
UnityAdManager.showUnityAd(this@StartAbility, readyUnitAd, object :
onShowCallBack {
override fun onShowFail() {
action.invoke()
}
override fun onShowClose() {
action.invoke()
}
override fun onShowSuccess() {
}
})
} else {
if (isFinish) {
action.invoke()
}
}
} }
} }

View File

@ -0,0 +1,132 @@
package com.cute.girl.hd.pink.img.wallpaper.unityad;
import android.app.Activity;
import android.util.Log;
import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAds;
import com.unity3d.ads.UnityAdsShowOptions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
public class UnityAdManager {
public static String unityGameID = "5649501";
public static Boolean testMode = true;
public static String unitAd1 = "Art_Wallpaper_ad_unity_inst_open";
public static String unitAd2 = "Art_Wallpaper_ad_unity_inst_set";
public static String unitAd3 = "Art_Wallpaper_ad_unity_inst_theme";
public static Boolean AD1Ready = false;
public static Boolean AD2Ready = false;
public static Boolean AD3Ready = false;
public static String TAG = "-------TAG";
public static void loadAllAdNew() {
if (!AD1Ready)
loadAd(unitAd1);
if (!AD2Ready)
loadAd(unitAd2);
if (!AD3Ready)
loadAd(unitAd3);
}
public static String getReadyUnitAd() {
ArrayList<String> unitList = new ArrayList<>();
unitList.add(unitAd1);
unitList.add(unitAd2);
unitList.add(unitAd3);
Collections.shuffle(unitList);
for (String unit : unitList) {
if (Objects.equals(unit, unitAd1)) {
if (AD1Ready) {
return unit;
}
} else if (Objects.equals(unit, unitAd2)) {
if (AD2Ready) {
return unit;
}
} else if (Objects.equals(unit, unitAd3)) {
if (AD3Ready) {
return unit;
}
}
}
return null;
}
public static void loadAd(String adUnitId) {
UnityAds.load(adUnitId, new IUnityAdsLoadListener() {
@Override
public void onUnityAdsAdLoaded(String placementId) {
Log.d(TAG, "------loaded-unit" + placementId);
if (Objects.equals(placementId, unitAd1)) {
AD1Ready = true;
} else if (Objects.equals(placementId, unitAd2)) {
AD2Ready = true;
} else if (Objects.equals(placementId, unitAd3)) {
AD3Ready = true;
}
}
@Override
public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
Log.d(TAG, "------load fail-unit" + placementId + "-----" + message);
}
});
}
public static void showUnityAd(Activity activity, String adUnitId, onShowCallBack onShowCallBack) {
UnityAds.show(activity, adUnitId, new UnityAdsShowOptions(), new IUnityAdsShowListener() {
@Override
public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
onShowCallBack.onShowFail();
Log.d(TAG, "--------onUnityAdsShowFailure---message=" + message);
}
@Override
public void onUnityAdsShowStart(String placementId) {
Log.d(TAG, "--------onUnityAdsShowStart---=" + placementId);
if (Objects.equals(placementId, unitAd1)) {
AD1Ready = false;
} else if (Objects.equals(placementId, unitAd2)) {
AD2Ready = false;
} else if (Objects.equals(placementId, unitAd3)) {
AD3Ready = false;
}
onShowCallBack.onShowSuccess();
}
@Override
public void onUnityAdsShowClick(String placementId) {
}
@Override
public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
onShowCallBack.onShowClose();
Log.d(TAG, "--------onUnityAdsShowComplete---");
loadAd(placementId);
}
});
}
}

View File

@ -0,0 +1,6 @@
package com.cute.girl.hd.pink.img.wallpaper.unityad;
public interface onLoadCallBack {
void onIsLoad(boolean loaded);
}

View File

@ -0,0 +1,8 @@
package com.cute.girl.hd.pink.img.wallpaper.unityad;
public interface onShowCallBack {
void onShowFail( );
void onShowClose( );
void onShowSuccess( );
}