更多笔记和源码请关注:【微信公众号】 CocosCreator笔记
tips:ts与java交互
请参考官方文档:
https://docs.cocos.com/creator/2.2/manual/zh/advanced-topics/java-reflection.html
初始化相关变量:
static public AppActivity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
}
ts调用java:
必须是static函数,如果有任何UI操作,一定要在UI线程中执行
static public void showAd(String codeId) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
java调用ts:
一定要在GL线程中执行
public void sendReward() {
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
String js = "cc.NativeBridge.rewardedAdCallback(";
String params = "'"+param0+"','"+param1+"'";
js = js + params + ");";
Cocos2dxJavascriptJavaBridge.evalString(js);
}
});
}
首先构建编译安卓工程
在proj.android-studio/app目录下新建libs目录
将已下两个aar文件拷贝到该目录
配置gradle:
在项目的build.gradle中添加依赖:
implementation('com.android.support:support-v4:24.2.0')
官方文档中让添加open_ad_sdk.aar和android-gif-drawable-1.2.6.aar
但cc项目本身就添加了aar格式文件,不需要再手动依赖,否则会报错:
More than one file was found with OS independent path XXX
最终正确的配置如下:
然后在defaultConfig中添加abiFilters,后边根据需要配置平台
ndk {
// 设置支持的 SO 库构架,注意这里要根据你的实际情况来设置 abiFilters ‘armeabi-v7a’, ‘arm64-v8a’, ‘x86’, ‘x86_64’, ‘arm64-v8a’
abiFilters 'armeabi-v7a'
}
位置如下图:
如果将demo中的LayoutManagerGroup.aar也拷贝进项目会编译报错:
Manifest merger failed with multiple errors, see logs
点击查看错误详情:
提示引用的包LayoutManagerGroup.aar中minSdkVersion最小为19,而此项目里minSdkVersion为16,造成冲突。
将项目的minSdkVersion改为19即可
配置AndroidManifest:
添加权限:
添加provider:
android:name="com.bytedance.sdk.openadsdk.TTFileProvider"
android:authorities="${applicationId}.TTFileProvider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
android:name="com.bytedance.sdk.openadsdk.multipro.TTMultiProvider"
android:authorities="${applicationId}.TTMultiProvider"
android:exported="false" />
在res目录下新建xml目录,新建file_paths.xml文件,添加代码:
配置代码混淆:
在proguard-rules.pro文件中添加代码:
-keep class com.bytedance.sdk.openadsdk.** { *; }
-keep public interface com.bytedance.sdk.openadsdk.downloadnew.** {*;}
配置完成后,开始撸代码了~
将demo中的TTAdManagerHolder.java文件拷贝到项目中
文件顶部的包名改成
package org.cocos2dx.javascript;
buildConfig函数中填写正确的参数
新建MyApplication.java文件,代码如下:
public class MyApplication extends Application {
@Override
public void onCreate(){
super.onCreate();
//穿山甲SDK初始化
//强烈建议在应用对应的Application#onCreate()方法中调用,避免出现content为null的异常
TTAdManagerHolder.init(this);
}
}
在AndroidManifest中添加application
android:name="org.cocos2dx.javascript.MyApplication"
就可以成功初始化SDK了~
在AppActivity中添加广告代码:
static public AppActivity activity;
private String TAG = "cocos";
private TTAdNative mTTAdNative;
private TTRewardVideoAd mttRewardVideoAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
// Don't need to finish it again since it's finished in super.onCreate .
return;
}
// DO OTHER INITIALIZATION BELOW
SDKWrapper.getInstance().init(this);
activity = this;
//step1:初始化sdk
TTAdManager ttAdManager = TTAdManagerHolder.get();
//step2:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
TTAdManagerHolder.get().requestPermissionIfNecessary(this);
//step3:创建TTAdNative对象,用于调用广告请求接口
mTTAdNative = ttAdManager.createAdNative(getApplicationContext());
loadAd("938493801", TTAdConstant.VERTICAL);
}
static public void showAd(String codeId) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (activity.mttRewardVideoAd != null) {
//step6:在获取到广告后展示
//该方法直接展示广告
activity.mttRewardVideoAd.showRewardVideoAd(activity);
//展示广告,并传入广告展示的场景
activity.mttRewardVideoAd = null;
} else {
Log.e(activity.TAG, "请先加载广告");
}
}
});
}
public void sendReward() {
activity.runOnGLThread(new Runnable() {
@Override
public void run() {
String js = "cc.NativeBridge.rewardedAdCallback();";
Cocos2dxJavascriptJavaBridge.evalString(js);
Log.e(TAG,"rewardVideoAd evalString");
}
});
}
private void loadAd(final String codeId, int orientation) {
//step4:创建广告请求参数AdSlot,具体参数含义参考文档
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(codeId)
.setSupportDeepLink(true)
.setImageAcceptedSize(1080, 1920)
.setRewardAmount(1) //奖励的数量
.setUserID("")//用户id,必传参数
.setOrientation(orientation) //必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
.build();
//step5:请求广告
mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
@Override
public void onError(int code, String message) {
Log.e(TAG, message);
}
//视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
@Override
public void onRewardVideoCached() {
Log.e(TAG, "rewardVideoAd video cached");
}
//视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
@Override
public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
Log.e(TAG, "rewardVideoAd loaded");
mttRewardVideoAd = ad;
mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
@Override
public void onAdShow() {
Log.e(TAG, "rewardVideoAd show");
}
@Override
public void onAdVideoBarClick() {
Log.e(TAG, "rewardVideoAd bar click");
}
@Override
public void onAdClose() {
Log.e(TAG, "rewardVideoAd close");
//关闭后再次加载广告
loadAd(codeId, TTAdConstant.VERTICAL);
}
//视频播放完成回调
@Override
public void onVideoComplete() {
Log.e(TAG,"rewardVideoAd complete");
}
@Override
public void onVideoError() {
Log.e(TAG,"rewardVideoAd error");
}
//视频播放完成后,奖励验证回调,rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称
@Override
public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName) {
Log.e(TAG,"rewardVideoAd rewardVerify");
sendReward();
}
@Override
public void onSkippedVideo() {
}
});
}
});
}