cocos2d-js 3.0 android平台编译打包

前端之家收集整理的这篇文章主要介绍了cocos2d-js 3.0 android平台编译打包前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址: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版本,方便测试调试。一般开发阶段这样已经就可以了。
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文件
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

android编译环境换到mac系统,因为cocos在使用release模式编译的时候,自动把js脚本编译成jsc,而jsbcc只支持mac和win32平台:
https://github.com/faint2death/cocos2d-js/blob/master/build-apk.md

1. 环境配置
cocos2d-js 3.0 rc0的changelog提到cocos在使用release模式编译的时候,自动把js脚本编译成jsc,但是看cocos2d-js-v3.0-rc0/tools/cocos2d-console/plugins/plugin_jscompile/bin/下的jsbcc只支持mac和win32平台,所以把原来linux下编译环境换到mac平台。

MacBook Air的机器,操作系统版本是OS X 10.9.3,Xcode版本是5.1.1,java开发环境已经装好。下载如下必要的开发包:
cocos2d-js-v3.0-rc0.zip
android-ndk-r9d-darwin-x86.tar.bz2
adt-bundle-mac-x86_64-20140624.zip
apache-ant-1.9.4-bin.tar.bz2
有新的可以下载更新的版本。
配置cocos2d-js:
xtekiMacBook-Air:cocos2d-js-v3.0-rc0 x$ ./setup.py
Setting up cocos2d-x...
->Check environment variable COCOS_CONSOLE_ROOT
->Find environment variable COCOS_CONSOLE_ROOT...
->COCOS_CONSOLE_ROOT is found : /Users/x/cocos2d/cocos2d-js-v3.0-rc0/tools/cocos2d-console/bin

->Check environment variable NDK_ROOT
->Find environment variable NDK_ROOT...
->NDK_ROOT is found : /Users/x/cocos2d/android-ndk-r9d
->Check environment variable ANDROID_SDK_ROOT
->Find environment variable ANDROID_SDK_ROOT...
->ANDROID_SDK_ROOT is found : /Users/x/cocos2d/adt-bundle-mac-x86_64-20140624/sdk
->Check environment variable ANT_ROOT
->Find environment variable ANT_ROOT...
->ANT_ROOT is found : /Users/x/cocos2d/apache-ant-1.9.4/bin
Please execute command: "source /Users/x/.bash_profile" to make added system variables take effect
xtekiMacBook-Air:cocos2d-js-v3.0-rc0 x$ source ~/.bash_profile
进入android sdk的目录下载更新android平台和第三方库文件
xtekiMacBook-Air:cocos2d-js-v3.0-rc0 x$ cd ~/cocos2d/adt-bundle-mac-x86_64-20140624/sdk/
xtekiMacBook-Air:sdk x$ tools/android
这时会弹出Android SDK Manager的窗口,勾选Android 2.3.3(API 10),然后安装。安装完后,在sdk的platforms下出现一个android-10的目录。
2. 编译测试
生成一个测试项目:
xtekiMacBook-Air:cocos2d-js-v3.0-rc0 x$ cocos new MyGame -l js -d ~/project/
修改frameworks/runtime-src/proj.android/AndroidManifest.xml可以设置游戏启动是横屏还是竖屏,默认是landscape:
android:screenOrientation="portrait"
编译这个测试项目:
xtekiMacBook-Air:cocos2d-js-v3.0-rc0 x$ cd ~/project/MyGame/
xtekiMacBook-Air:MyGame x$ cocos compile -p android
Runing command: compile
Building mode: debug
building native
NDK build mode: debug
The Selected NDK toolchain version was 4.8 !
...
debug:
BUILD SUCCESSFUL
Total time: 14 seconds
Move apk to /Users/x/project/MyGame/runtime/android
build succeeded.
xtekiMacBook-Air:MyGame x$ ls -l /Users/x/project/MyGame/runtime/android
total 15416
-rw-r--r--1 xstaff7889244 Jul 10 15:55 MyGame-debug.apk
这样编译的是debug版本,方便测试调试。一般开发阶段,这样就可以了。
3. 发布release版本
如果要编译release版本,那么首先要创建keystore:
xtekiMacBook-Air:MyGame x$ 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文件
xtekiMacBook-Air:MyGame x$ ls -l demo.keystore
-rw-r--r--1 xstaff1334 Jul 10 14:52 demo.keystore
然后用如下命令编译发布:
xtekiMacBook-Air:MyGame x$ cocos compile -p android -m release
编译中间可以看到js被转成jsc文件。编译完后,会提示你输入keystore文件
Total time: 12 seconds
Move apk to /Users/x/project/MyGame/publish/android
Please input the absolute/relative path of ".keystore" file:
输入刚才生成的demo.keystore完整路径,比如这里是/Users/x/project/MyGame/demo.keystore,然后会提示输入这个keystore的密码,alias以及alias的密码,然后脚本会自动做签名,生成做了签名的apk文件
xtekiMacBook-Air:MyGame x$ ls -l /Users/x/project/MyGame/publish/android
total 24504
-rw-r--r--1 xstaff6271887 Jul 10 16:01 MyGame-release-signed.apk
-rw-r--r--1 xstaff6268000 Jul 10 16:01 MyGame-release-unsigned.apk
release模式编译的apk包比debug模式要小不少,解开apk包,查看assets目录下的js文件都编译成jsc文件,不用担心源码泄漏了。

猜你在找的Cocos2d-x相关文章