盒子
盒子
Posts List
  1. 一.接入环境
    1. 1.工具版本
    2. 2.环境配置说明
    3. cocos2dx配置
    4. eclipse Adt 配置
    5. 配置CDT编译
  2. 二.接入步骤
    1. 1.修改androidmanifest.xml文件
    2. 2.目录资源拷贝
    3. 3.接口接入
    4. 账号接口
    5. SDK初始化接口
    6. Application处理
  3. 三.接入接口说明
    1. 品宣初始化
    2. 游戏统计接口
    3. 支付接口
  4. 四.常见问题汇总
    1. 1. target-13 XXX 版本对应问题
    2. 2. 找不到MyGame SO库问题
    3. 3. 报告错误
    4. 4. is missing required library: ‘XXXX\CMBilling.jar’

百度单机SDK cocos2dx接入文档

一.接入环境

1.工具版本

接入环境名称 版本号
cocos2dx 2.13.1
androidSDK 3.2 LEVEL=13
JDK 1.8.0_51
NDK R10E

2.环境配置说明

cocos2dx配置

  • 下载cocos2dx_2.13.1,解压并运行根目录下的setup.py配置cocos2dx基本环境,包括:

    • cocos2dx运行根目录
    • cocos2dx template 目录
    • NDK根目录
  • 生成android 工程 ,运行命令

cocos new DemoForCocos2dx -p com.duoku.cocosx.demo -l cpp -d .

eclipse Adt 配置

  • 导入cocos2dx 生成的android包,位于生成的项目根目录下的proj.android目录下,如图:

image

  • 导入cocos2dx lib库,位于
    cocos2d/cocos/platform/android/java/下,如图:

image

  • 同理,导入单机SDK_Lib库,位于DKSingleSDK_LibProject

  • 设置 DemoForCocos2dx依赖于上述两个Lib库,在项目上右键->属性->android->add , 如图:

image

配置CDT编译

如果在开发过程中修改了相关cpp代码,则需要重编译libMyGame.so库,具体编译方法为:

项目右键 -> 属性 -> builders -> CDT builder勾上
选择c/c++ builder ->build command 填入内容

python ${ProjDirPath}/build_native.py -b release

==注意:==

如果没有修改CPP内容,则不需要增加CDT编译

二.接入步骤

1.修改androidmanifest.xml文件

标签前添加权限

<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.webkit.permission.PLUGIN" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<!-- ================= migu 基地配置 start ================ -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>
<!-- ================= migu 基地配置 end ================ -->

标签内添加以下内容

<!-- ================================baidu single sdk meta data start================================ -->
<meta-data
android:name="dksdk_appid"
android:value="XXXXXXX" />
<meta-data
android:name="dksdk_appkey"
android:value="XXXXXX" />
<meta-data
android:name="dksdk_appsecret"
android:value="XXXXXX" />
<!-- dksdk_channel表示渠道,请开发者向我方运营索要需要推广的渠道号 -->
<meta-data
android:name="dksdk_channel"
android:value="XXXXX" />
<!-- dksdk_ver表示sdk版本 -->
<meta-data
android:name="dksdk_ver"
android:value="XXXXX" />
<!-- ================================baidu single sdk meta data end================================ -->
<!-- ===============!!!!!!!!!替换此处LauncherActivity为自己项目的入口Activity!!!!!!!!!!!!======== -->
<activity
android:name="com.duoku.cocosx.demo.LauncherActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="portrait">
</activity>
<!-- <activity -->
<!-- android:name="com.duoku.cocosx.demo.LauncherActivity" -->
<!-- android:configChanges="orientation|keyboardHidden|screenSize" -->
<!-- android:screenOrientation="portrait" -->
<!-- android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > -->
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
<!-- </activity> -->
<activity
android:name="org.cocos2dx.cpp.AppActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:screenOrientation="portrait"
android:theme="@style/DK.Theme.NoBackGround.NoAnimation" >
</activity>
<!-- ================================baidu single sdk 基础配置 start ================================ -->
<!-- ================= migu 基地配置 start ================ -->
<activity
android:name="cn.cmgame.billing.api.GameOpenActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboard|screenSize"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CHINAMOBILE_OMS_GAME" />
<category android:name="android.intent.category.CHINAMOBILE_GAMES" />
</intent-filter>
</activity>
<activity android:name="cn.cmgame2_0.launch_model.shortcut.main.MiguHomeActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="cn.cmgame2_0.category.migu_home"/>
</intent-filter>
</activity>
<service
android:name="cn.cmgame.billing.service.GameService">
</service>
<!-- ================= migu 基地配置 end ================ -->
<!-- Activity屏幕方向要同代码中设置一致 -->
<activity
android:name="com.duoku.platform.single.ui.DKRecommendActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:screenOrientation="portrait"
android:theme="@style/DK.Theme.NoBackGround.NoAnimation" >
</activity>
<activity
android:name="com.duoku.platform.single.ui.DKStartDownloadActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:theme="@style/dksdk_theme_dialog" />
<activity
android:name="com.duoku.platform.single.ui.DKLotteryDrawActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:screenOrientation="portrait"
android:theme="@style/DK.Theme.NoBackGround.NoAnimation" >
</activity>
<receiver android:name="com.duoku.platform.single.receiver.PushMessageReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="com.baidu.android.pushservice.action.MESSAGE" />
<action android:name="com.baidu.android.pushservice.action.RECEIVE" />
<action android:name="com.baidu.android.pushservice.action.notification.CLICK" />
</intent-filter>
</receiver>
<receiver
android:name="com.baidu.android.pushservice.PushServiceReceiver"
android:process=":bdservice_v1" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="com.baidu.android.pushservice.action.notification.SHOW" />
<action android:name="com.baidu.android.pushservice.action.media.CLICK" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="com.baidu.android.pushservice.action.frontia.user" />
</intent-filter>
</receiver>
<receiver
android:name="com.baidu.android.pushservice.RegistrationReceiver"
android:process=":bdservice_v1" >
<intent-filter>
<action android:name="com.baidu.android.pushservice.action.METHOD" />
<action android:name="com.baidu.android.pushservice.action.BIND_SYNC" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service
android:name="com.baidu.android.pushservice.PushService"
android:exported="true"
android:process=":bdservice_v1" >
<intent-filter>
<action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" />
</intent-filter>
</service>
<service
android:name="com.baidu.android.pushservice.CommandService"
android:exported="true" />
<service
android:name="com.duoku.platform.single.gameplus.service.GPDownloadService"
android:enabled="true"
android:exported="true"
android:process="com.baidu.platform.gameplus.service" >
<intent-filter>
<action android:name="com.baidu.platform.gameplus.service" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
<service android:name="com.duoku.platform.single.gameplus.install.GPSilentInstallService" />
<receiver
android:name="com.duoku.platform.single.gameplus.broadcast.GPNotificaionReceiver" >
<intent-filter>
<action android:name="baidu.platform.gameplus.intent.action.CANCLE_NOTIFICATION" />
<action android:name="baidu.platform.gameplus.intent.action.DO_NOTIFICATION_CLICK" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<!-- 支付模块配置 -->
<!-- 注意:支付模块的Activity屏幕方向要同代码中设置一致 -->
<activity
android:name="com.duoku.platform.single.ui.DKContainerActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:screenOrientation="portrait"
android:theme="@style/DK.Theme.NoBackGround.NoAnimation" >
</activity>
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:screenOrientation="portrait" >
</activity>
<!-- 聚合微信支付 start -->
<activity
android:name="com.ipaynow.wechatpay.plugin.inner_plugin.wechat_plugin.activity.WeChatNotifyActivity"
android:configChanges="orientation|keyboardHidden|keyboard|smallestScreenSize|locale|screenLayout"
android:screenOrientation="behind"
android:theme="@android:style/Theme.Dialog" >
</activity>
<!-- 聚合微信支付 end -->
<!-- for weixin plugin -->
<activity
android:name="com.baidu.platformsdk.wxpay.PayActivity"
android:configChanges="orientation|navigation"
android:exported="false"
android:windowSoftInputMode="adjustPan"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<!-- for weixin plugin end -->
<!-- 联通沃商店支付 Start -->
<activity android:name="com.unicom.wostore.unipay.paysecurity.SecurityActivity"
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
android:configChanges="screenSize|orientation"
android:screenOrientation="landscape">
</activity>
<service android:name="com.unicom.wostore.unipay.paysecurity.SecurityServiceFramework"/>
<!-- 联通沃商店支付 End-->
<!-- 登陆模块配置 -->
<activity
android:name="com.duoku.platform.single.bdpass.DKAccountContainerActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:screenOrientation="portrait"
android:theme="@style/DK.Theme.NoBackGround.NoAnimation" >
</activity>
<!-- ================================baidu single sdk 基础配置 end ================================ -->

==注意==:

  • dksdk_appid,dksdk_appkey,dksdk_appsecret,dksdk_channel需要修改为CP在百度移动游戏开发者平台上申请后的分配的值。
  • 找到AndroidManifest.xml文件中launcherActivity,替换为自己项目的入口activity,且launchMode段注释掉。

2.目录资源拷贝

  • assets
    将demo项目下的除img/和fonts/两目录以外的其他资源,全部拷贝到自己项目的assets目录下。

    ==注意:cocos2dx自带的build_native.py会在编译完成后,默认删除assets下的内容,并从根目录下Resources中重新拷贝资源==

  • res
    将res/values/g_strings拷贝到自己项目对应的目录,并修改LauncherActivity为自己项目的启动activity (与androidManifest.xml中对应)如图所示:

image

3.接口接入

账号接口

接口:

public void invokeBDInitApplication(Application context)
public void invokeBDInit(Context context,IDKSDKCallBack callBack)

例子:

@Override
public void onCreate() {
super.onCreate();
activities = new ArrayList<Activity>();
try {
//baidu pass
DKPlatform.getInstance().invokeBDInitApplication(this);
} catch (Exception e) {
// TODO: handle exception
}
//移动基地so文件加载
DKPlatform.getInstance().invokeGBInit(this);
app = this;
}
// 登陆初始化
private void initLogin() {
// 回调函数
loginlistener = new IDKSDKCallBack() {
@Override
public void onResponse(String paramString) {
try {
JSONObject jsonObject = new JSONObject(paramString);
// 返回的操作状态码
int mFunctionCode = jsonObject
.getInt(DkProtocolKeys.FUNCTION_CODE);
// 登陆成功
if (mFunctionCode == DkErrorCode.DK_ACCOUNT_LOGIN_SUCCESS) {
Log.d(TAG, "login success");
// 返回的百度uid,供cp绑定使用
String bduid = jsonObject
.getString(DkProtocolKeys.BD_UID);
Log.d(TAG, "bduid =" + bduid);
Var.bduid = bduid;
// 登陆失败
} else if (mFunctionCode == DkErrorCode.DK_ACCOUNT_LOGIN_FAIL) {
// 显示登陆按钮,隐藏修改密码和切换账号按钮
Log.d(TAG, "login failed");
Var.bduid = "";
// 快速注册成功
} else if (mFunctionCode == DkErrorCode.DK_ACCOUNT_QUICK_REG_SUCCESS) {
// 快速试玩登陆成功,都需要隐藏
Log.d(TAG, "quick reg success");
Var.bduid = "";
}
} catch (Exception e) {
e.printStackTrace();
Var.bduid = "";
}finally{
enterGame();
}
}
};
DKPlatform.getInstance().invokeBDInit(this, loginlistener);
}

==注意:==

方法invokeBDInitApplication 在 Application的onCreate方式中调用,invokeBDInit 所在的activity生命周期必须跟app一致,不能提前销毁

SDK初始化接口

接口:

public void init(Activity activity, boolean isLandScape, SdkMode sdkMode,DKCMMMData mmData, DKCMGBData gbData,IDKSDKCallBack callBack)

例子:

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// init sdk
initSDK();
}
private void initSDK() {
IDKSDKCallBack initcompletelistener = new IDKSDKCallBack() {
@Override
public void onResponse(String paramString) {
Log.d(TAG, "SDK INIT RESULT:"+paramString);
try {
JSONObject jsonObject = new JSONObject(paramString);
// 返回的操作状态码
int mFunctionCode = jsonObject
.getInt(DkProtocolKeys.FUNCTION_CODE);
// 初始化完成
if (mFunctionCode == DkErrorCode.BDG_CROSSRECOMMEND_INIT_FINSIH) {
// initLogin();
enterGame();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
// 参数为测试数据,接入时请填入你真实数据
DKCMMMData mmData = new DKCMMMData("000000000000", "0000000000000000");
DKCMGBData gbData = new DKCMGBData();
// 初始化函数
DKPlatform.getInstance().init(this, false,
DKPlatformSettings.SdkMode.SDK_PAY, mmData, gbData,
initcompletelistener);
}

Application处理

==注意:==

如果不接入移动基地和移动MM支付,则不需要建立Application类

public class DemoApplication extends Application {
@Override
protected void attachBaseContext(Context ctx){
super.attachBaseContext(ctx);
DKPlatform.getInstance().invokeMMHelperInstall(this);
}
@Override
public void onCreate() {
super.onCreate();
//baidu pass
DKPlatform.getInstance().invokeBDInitApplication(this);
//移动基地so文件加载
DKPlatform.getInstance().invokeGBInit(this);
}
}

修改androidManifest.xml文件,添加application映射,如图所示

image

三.接入接口说明

品宣初始化

注意:如果游戏存在多个activity,则每个activity都需要接入,如demo中存在launcherActivity和AppActivity

接口:

public void bdgameInit(Activity activity, callBack)

例子:

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initAds();
}
private void initAds() {
DKPlatform.getInstance().bdgameInit(this, new IDKSDKCallBack() {
@Override
public void onResponse(String paramString) {
Log.d(TAG, "bggameInit success");
}
});
}

游戏统计接口

==注意:==

如果游戏存在多个activity,则每个activity都需要接入,如demo中存在launcherActivity和AppActivity

接口:

public void resumeBaiduMobileStatistic(Context context)
public void pauseBaiduMobileStatistic(Context context)

例子:

@Override
protected void onResume(){
super.onResume();
DKPlatform.getInstance().resumeBaiduMobileStatistic(this);
}
@Override
protected void onPause(){
super.onPause();
DKPlatform.getInstance().pauseBaiduMobileStatistic(this);
}

支付接口

==注意:==

调用支付接口必须在Cocos2dxActivity或其继承类中进行。

接口:

public void invokePayCenterActivity(Context context, GamePropsInfo dkPlatformGameProps, DKCMMdoData mdoInfo,DKCMMMData mmInfo,
DKCMGBData gbData,DKCMYBKData ybkData,IDKSDKCallBack callBack)

四.常见问题汇总

1. target-13 XXX 版本对应问题

打开SDK Manager 下载对应的版本

2. 找不到MyGame SO库问题

打开androidManifest.xml文件,查看如图所示位置配置的内容:

image

并查看libs目录下是否有对应的libMyGame.so库

==注意:==

本demo中的libMyGame.so库根据demo需求进行了部分修改,开发者如有修改,需重新编译生成so库

3. 报告错误

unable to resolve virtual method ZipResourceFile

将demo项目中的libcocos2dx用库依赖方式引入,而不是jar包方式引入

4. is missing required library: ‘XXXX\CMBilling.jar’

在cocos2dx项目上右键->属性->Java build path -> library -> add Jars

image

支持一下
扫一扫,支持牛头码农