怎么将cocos2d-js 3.x 成功在android平台编译打包 (测试ok)

转载:http://www.cocoachina.com/bbs/read.php?tid=209353

1. 环境配置


使用的操作系统是64位的Ubuntu Server 14.04,首先下载cocos2d-js开发包和android必须的NDK、SDK等开发库,有更新的版本请下载新版本,全部解压到/home/magic(本文测试用户目录)下的目录。

cocos2d-js-v3.0-beta.zip
android-ndk-r9d-linux-x86_64.tar.bz2
android-sdk_r22.6.2-linux.tgz
apache-ant-1.9.4-bin.tar.bz2

配置cocos2d-js:

magic@magic:~$ cd cocos2d-js-v3.0-beta
magic@magic:~/cocos2d-js-v3.0-beta$ ./setup.py

Setting up cocos2d-x...
->Check environment variable COCOS_CONSOLE_ROOT
  ->Find environment variable COCOS_CONSOLE_ROOT...
    ->COCOS_CONSOLE_ROOT is found : /home/magic/cocos2d-js-v3.0-beta/tools/cocos2d-console/bin

->Configuration for Android platform only, you can also skip and manually edit "/home/magic/.bashrc"

->Check environment variable NDK_ROOT
  ->Find environment variable NDK_ROOT...
    ->NDK_ROOT is found : /home/magic/android-ndk-r9d

->Check environment variable ANDROID_SDK_ROOT
  ->Find environment variable ANDROID_SDK_ROOT...
    ->ANDROID_SDK_ROOT is found : /home/magic/android-sdk-linux

->Check environment variable ANT_ROOT
  ->Find environment variable ANT_ROOT...
    ->ANT_ROOT is found : /home/magic/apache-ant-1.9.4/bin/


Please execute command: "source /home/magic/.bashrc" to make added system variables take effect

magic@magic:~/cocos2d-js-v3.0-beta$ source /home/magic/.bashrc

安装java的运行和开发环境:

magic@magic:~$ sudo apt-get install default-jre
magic@magic:~$ sudo apt-get install default-jdk

进入android sdk的目录下载更新android平台和第三方库文件:

magic@magic:~$ cd android-sdk-linux
magic@magic:~/android-sdk-linux$ tools/android update sdk --no-ui
如果不更新,那么用cocos没法编译。这个更新下载时间非常长,会把所有android版本的sdk文件都下载下来,建议下班前执行,第二天上班再来看。

如果你的操作系统是64位的,那么还需要安装32位的编译器:

magic@magic:~$ sudo apt-get install libstdc++6:i386 libgcc1:i386 zlib1g:i386 libncurses5:i386 --no-install-recommends

否则后面cocos编译的时候会出现类似如下错误:

BUILD FAILED
/home/magic/android-sdk-linux/tools/ant/build.xml:601: The following error occurred while executing this line:
/home/magic/android-sdk-linux/tools/ant/build.xml:653: The following error occurred while executing this line:
/home/magic/android-sdk-linux/tools/ant/build.xml:698: Execute failed: java.io.IOException: Cannot run program "/home/magic/android-sdk-linux/build-tools/19.1.0/aapt" (in directory "/home/magic/project/MyGame/frameworks/js-bindings/cocos2d-x/cocos/platform/android/java"): error=2, No such file or directory

因为aapt是个32的程序,它要调用32位编译器。

2. 编译测试

生成一个测试项目:

magic@magic:~$ cocos new MyGame -l js -d /home/magic/project/

修改frameworks/runtime-src/proj.android/AndroidManifest.xml可以设置游戏启动是横屏还是竖屏,默认是landscape:

android:screenOrientation="portrait"

编译这个测试项目:

magic@magic:~$ cd project/MyGame/
magic@magic:~/project/MyGame$ cocos compile -p android
...
BUILD SUCCESSFUL
Total time: 12 seconds
Move apk to /home/magic/project/MyGame/runtime/android
build succeeded.
magic@magic:~/project/MyGame$ ls -l /home/magic/project/MyGame/runtime/android
total 6120
-rw-rw-r-- 1 magic magic 6263119 Jun 16 16:08 MyGame-debug.apk

这样编译的是debug版本,方便测试调试。一般开发阶段这样已经就可以了。

注意: 如果 cocos compile -p android 出现如下问题:

如题,cocos2dx3.13.1项目编译android时报错:

Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 9 in ./AndroidManifest.xml    
Android NDK:
ERROR:/Users/Evan/Documents/android-ndk/sources/cxx-stl/gnu-libstdc++/Android.mk:gnustl_static: LOCAL_SRC_FILES points to a missing file   
Android NDK: Check that /Users/Evan/Documents/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a exists  or that its path is correct   
make: Entering directory `/Users/Evan/Documents/work-fight/JdysrFight/fightGame/frameworks/runtime-src/proj.android'
/Users/Evan/Documents/android-ndk/build/core/prebuilt-library.mk:45: *** Android NDK: Aborting    .  Stop.
make: Leaving directory `/Users/Evan/Documents/work-fight/JdysrFight/fightGame/frameworks/runtime-src/proj.android'

解决方法:我之前用的ndk版本为r9,换成r10c就好了。

备注:/Users/Evan/Documents/android-ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/libgnustl_static.a exists  or that its path is correct

注意这里有个4.9/,r9d里只有4.6,4.8没有4.9的,所以暂时是换了ndk版本解决的。

ndk 下载地址:https://developer.android.google.cn/ndk/downloads/revision_history.html




3. 发布release版本

如果要编译release版本,那么首先要创建keystore:

magic@magic:~/project/MyGame$ keytool -genkey -alias demo.keystore -keyalg RSA -validity 3650 -keystore demo.keystore

说明:
genkey 产生密钥
-alias demo.keystore 别名 demo.keystore
-keyalg RSA 使用RSA算法对签名加密
-validity 3650 有效期限10年
-keystore demo.keystore

会要求输入keystore的密码,以及组织名字等相关信息,填好后在当前目录生成keystore文件:

magic@magic:~/project/MyGame$ ls -l demo.keystore 
-rw-rw-r-- 1 magic magic 2195 Jun 17 09:04 demo.keystore

然后用如下命令编译发布:

magic@magic:~/project/MyGame$ cocos compile -p android -m release

编译完后,会提示你输入keystore文件:

BUILD SUCCESSFUL
Total time: 12 seconds
Move apk to /home/magic/project/MyGame/publish/android
Please input the absolute/relative path of ".keystore" file:

输入刚才生成的demo.keystore完整路径,比如这里是/home/magic/project/MyGame/demo.keystore,然后会提示输入这个keystore的密码,alias以及alias的密码,然后脚本会自动做签名,生成做了签名的apk文件:

magic@magic:~/project/MyGame$ ls -l /home/magic/project/MyGame/publish/android/MyGame-release-signed.apk
-rw-rw-r-- 1 magic magic 6145648 Jun 17 09:07 /home/magic/project/MyGame/publish/android/MyGame-release-signed.apk
展开阅读全文

没有更多推荐了,返回首页