2026年天津统招专升本全日制培训来袭,封闭集训助你上岸!
2026/7/7 10:06:35
1.首先构建Android 项目,构建成功后打开Android视图
在build.gradle(Project:项目名)这个脚本修添加一些依赖的仓库,整体如下,就填加了allprojectts
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() // jcenter() // keeped as anchor, will be removed soon } dependencies { classpath 'com.android.tools.build:gradle:8.5.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenCentral() maven { url 'https://cboost.jfrog.io/artifactory/chartboost-ads/' } } } apply from: NATIVE_DIR +"/build.gradle"接下来打开build.gradle(Module:CocosGame)
在如下地方添加,chartboost最新正式的sdk
dependencies { implementation fileTree(dir: '../libs', include: ['*.jar','*.aar']) implementation fileTree(dir: 'libs', include: ['*.jar','*.aar']) implementation fileTree(dir: "${COCOS_ENGINE_PATH}/cocos/platform/android/java/libs", include: ['*.jar']) implementation project(':libservice') implementation project(':libcocos') //chartboost implementation 'com.chartboost:chartboost-sdk:9.12.0' if (Boolean.parseBoolean(PROP_ENABLE_INPUTSDK)) { implementation 'com.google.android.libraries.play.games:inputmapping:1.1.0-beta' implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.10" } }再打开proguard-rules.pro项目级别的,添加防混淆代码
-keep class com.chartboost.plugin.** { *; } -keep class com.chartboost.sdk.** { *; } -keepattributes *Annotation*接下来同步一下,下载这些依赖
下载完成后,创建AdChartboost类和appActivity同级
package com.cocos.game; import android.app.Activity; import android.util.Log; import android.widget.Toast; import com.chartboost.sdk.Chartboost; import com.chartboost.sdk.ads.Interstitial; import com.chartboost.sdk.ads.Rewarded; import com.chartboost.sdk.callbacks.InterstitialCallback; import com.chartboost.sdk.callbacks.RewardedCallback; import com.chartboost.sdk.events.CacheError; import com.chartboost.sdk.events.CacheEvent; import com.chartboost.sdk.events.ClickError; import com.chartboost.sdk.events.ClickEvent; import com.chartboost.sdk.events.DismissEvent; import com.chartboost.sdk.events.ExpirationEvent; import com.chartboost.sdk.events.ImpressionEvent; import com.chartboost.sdk.events.RewardEvent; import com.chartboost.sdk.events.ShowError; import com.chartboost.sdk.events.ShowEvent; import com.cocos.lib.JsbBridge; public class AdChartboost { public String TAG = "ChartBoost_sdk"; public Rewarded chartboostRewarded = null; public Interstitial chartboostInterstitial = null; private Activity activity; //重试次数 private int InterstitialCount = 5; private int RewardedCount = 5; private boolean onRewardPD=false; public AdChartboost(Activity activity) { this.activity = activity; } public void initChartboostSdk(String appId, String appSignature) { // 确保在 UI 线程中执行初始化和 Toast 提示 activity.runOnUiThread(() -> { Chartboost.startWithAppId(activity.getApplicationContext(), appId, appSignature, startError -> { if (startError == null) { Log.d(TAG, "Chartboost SDK initialized successfully!"); // Toast.makeText(activity, "SDK is initialized", Toast.LENGTH_SHORT).show(); JsbBridge.sendToScript("chartboost_succeed"); // 在这里调用你的隐私合规检查方法 InitAdType(); } else { String errorMsg = "SDK initialized with error: " + startError.getCode().name(); Log.e(TAG, errorMsg); //Toast.makeText(activity, errorMsg, Toast.LENGTH_SHORT).show(); JsbBridge.sendToScript("chartboost_error", startError.getCode().name()); } }); }); } public void InitAdType() { chartboostInterstitial = new Interstitial("location", new InterstitialCallback() { @Override public void onAdExpired(ExpirationEvent expirationEvent) {//过期 chartboostInterstitial.clearCache(); chartboostInterstitial.cache(); } @Override public void onImpressionRecorded(ImpressionEvent impressionEvent) {//记录 } @Override public void onAdClicked(ClickEvent clickEvent, ClickError clickError) {//点击 } @Override public void onAdShown(ShowEvent showEvent, ShowError showError) {//观看 } @Override public void onAdRequestedToShow(ShowEvent showEvent) {//展示请求 } @Override public void onAdLoaded(CacheEvent cacheEvent, CacheError cacheError) {//加载 if (cacheError != null) { Log.w(TAG, "loading error:" + cacheError.getCode()); JsbBridge.sendToScript("Interstitial", "TypeErrory"); if (InterstitialCount > 0) { chartboostInterstitial.cache(); InterstitialCount--; } return; } JsbBridge.sendToScript("Interstitial", "TypeSucceed"); InterstitialCount = 5; } @Override public void onAdDismiss(DismissEvent dismissEvent) {//关闭 chartboostInterstitial.cache(); } }, null); chartboostInterstitial.cache(); chartboostRewarded = new Rewarded("location", new RewardedCallback() { @Override public void onAdExpired(ExpirationEvent expirationEvent) { chartboostRewarded.clearCache(); chartboostRewarded.cache(); } @Override public void onImpressionRecorded(ImpressionEvent impressionEvent) {//记录印象 } @Override public void onAdClicked(ClickEvent clickEvent, ClickError clickError) { } @Override public void onAdShown(ShowEvent showEvent, ShowError showError) { if (showError != null) { Log.w(TAG, "onAdShown: 播放失败" + showError.getCode()); return; } onRewardPD=false; } @Override public void onAdRequestedToShow(ShowEvent showEvent) {//展示请求 } @Override public void onAdLoaded(CacheEvent cacheEvent, CacheError cacheError) { if (cacheError != null) { Log.w(TAG, "onAdLoaded: 缓存失败" + cacheError.getCode()); JsbBridge.sendToScript("Rewarded", "TypeErrory"); if (RewardedCount > 0) { chartboostRewarded.cache(); RewardedCount--; } return; } JsbBridge.sendToScript("Rewarded", "TypeSucceed"); RewardedCount= 5; } @Override public void onAdDismiss(DismissEvent dismissEvent) { if(onRewardPD){ JsbBridge.sendToScript("Rewarded","CloseSucced_Reward"); } chartboostRewarded.cache(); } @Override public void onRewardEarned(RewardEvent rewardEvent) {//获奖 onRewardPD=true; } }, null); chartboostRewarded.cache(); } }initChartboostSdk方法是初始化,成功或者失败会向Cocos层发送chartboost_succeed/chartboost_error,在Cocos接收到再进行判断是否继续发送检测广告是否有缓存有就展示广告,没有就不展示,下面是是Cocos的回调事件
messageInfo() { if (native.bridge) { native.bridge.onNative = (arg0, arg1) => { if (arg0 == "chartboost_succeed") { GameConfig.chartboostInit = true; console.log("chartboost_succeed"); } if (arg0 == "Interstitial") { if (arg1 == "TypeSucceed") { //如果插页有缓存 就是true,判断如果是true,就可以播放插页广告 GameConfig.adInInterstitial = true; } } if (arg0 == "Rewarded") { if (arg1 == "TypeSucceed") { //有广告,可以展示广告按钮 GameConfig.adRewarded = true; director.getScene()?.emit(EVENT_REWARDED_AD_LOADED); } //激励广告观看成功,发放奖励 if (arg1 == "CloseSucced_Reward") { director.getScene()?.emit(EVENT_REWARDED_AD_SUCCESS); } } } native.bridge.sendToNative("INITSDK"); } }emit是获取节点的事件并执行,最后在appactive.java的写入2个方法并且在oncreate调用Jsb_connection()就行,appid,app singnature替换成自己的就行
protected void Jsb_connection() { JsbBridge.setCallback(new JsbBridge.ICallback() { @Override public void onScript(String arg0, String arg1) { final String a0 = arg0; final String a1 = arg1; CocosHelper.runOnGameThread(new Runnable() { @Override public void run() { dispatchScriptMessage(a0, a1); } }); } }); } //message private void dispatchScriptMessage(String arg0, String arg1) { switch (arg0) { case "showInInterstitia": adChartboost.chartboostInterstitial.show(); break; case "showRewarded": adChartboost.chartboostRewarded.show(); break; case "INITSDK": // 实例化并初始化 Chartboost; adChartboost = new AdChartboost(this); adChartboost.initChartboostSdk("APP ID", "APP SIGNATURE"); break; } }如下广告已经正确展示,如果加载失败,看一下时间是不是和所在时间是一致的,不一致就会加载失败