allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.qq326646683:cocos-creator-android:2.0.1'
}
- 文件读写、网络权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
- 下载游戏zip并解压
- 继承CocosActivity,并将解压后的路径赋值给filePath
class CocosGameActivity: CocosActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun filePath() = intent.getStringExtra("path")
}
- 清单文件
<application>
<meta-data
android:name="android.app.lib_name"
android:value="cocos" />
<activity android:name=".CocosGameActivity" android:process=":cocos"/>
- 本篇的module和事例app代码放在gitlab
1.实现弹出Android的Dialog,选择后把结果传给cocos显示(不需要主进程的数据,可以直接1->4) android/CocosGameActivity.kt
private val showArray = arrayOf("刘德华", "周华健")
private val cocosListenerInCocos: CocosDataListener = CocosDataListener { action, argument, callbackId ->
CocosBridgeHelper.log("接收InCocos", action)
if (action == "action_showStarDialog") {
runOnUiThread {
AlertDialog.Builder(this)
.setTitle("选择")
.setItems(showArray) { _, index ->
CocosBridgeHelper.getInstance().nativeCallCocos(action, showArray[index], callbackId)
}
.create()
.show()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
CocosBridgeHelper.getInstance().addCocosListener(cocosListenerInCocos)
}
override fun onDestroy() {
super.onDestroy()
CocosBridgeHelper.getInstance().removeCocosListener(cocosListenerInCocos)
}
2.实现从主进程取数据给cocos显示 android/MainActivity.kt
private val cocosListenerInMain: CocosDataListener = CocosDataListener { action, argument, callbackId ->
CocosBridgeHelper.log("接收InMain", action)
if (action == "action_appVersion") {
CocosBridgeHelper.getInstance().main2Cocos(action, packageManager.getPackageInfo(packageName, 0).versionName, callbackId)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
CocosBridgeHelper.getInstance().addMainListener(cocosListenerInMain)
}
override fun onDestroy() {
super.onDestroy()
CocosBridgeHelper.getInstance().removeMainListener(cocosListenerInMain)
}