cocos2dx 命令创建各平台工程,并自动打包Android apk的方法

本文介绍如何通过脚本一键在命令行环境中为Cocos2d-x创建多平台项目,包括iOS和Android,只需指定项目名称、包名和语言,即可自动生成项目并提供快速构建Android APK的方法。

转自:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_create_a_multi-platform_project_in_one_command_line


How to create a multi-platform project in one command line

Many developers complained that creating new projects for cocos2d-x besides iOS is too difficult.
Even Ricardo Quesada complained this, and here's a tutorial that how to create an cocos2d-x hybrid project on iOS and Android
But today, we can do these jobs in a script automatically.

So here it is, issue #1733. Since v2.1.2, cocos2d-x/tools/project-creator.py can do the magic for you.

Environment Requirement

Only python environment is required, which can be downloaded from http://python.org/download/Make sure you're using Python 2.7.3. Download, install and set python's path into your command-line environment.
YOU DON'T NEED ANY PLATFORM'S DEVELOPMENT ENVIRONMENT AS A PRECONDITION! It means that, you can create win32 project from osx, and create ios project from win32.

The Magic

For example, your company is SuperSell.com, and your game is named Crash of Clams, and you would like to write the game logic codes in cpp, then you should use the script like this

cd /cocos2d-x/tools/project-createor/
python create_project.py -project Clams -package com.supersell.crash -language cpp


Then create_project.py will do the magic

After the jobs done, you can find the new project is created in cocos2d-x/projects/Clams/ directory. Visit each Clams/proj.*/ folder to find the corresponding makefile or project file for IDE.

For example, if you would like to run the empty android project immediately, 

cd /workspace/cocos2d-x/project/Clams/proj.android/
./build_native.sh
android update project -p ./
android update project -p ../../../cocos2dx/platform/android/java
ant debug
cd bin
adb install Clams-debug.apk


Run these additional commands above to build android apk requires that you have:

  • successfully installed android development environment, including SDK and NDK.

  • set valid NDK_ROOT, ANDROID_SDK_ROOT and COCOS2DX_ROOT parameters in your environment.

  • installed ant tool.

  • edit $PATH environment variable, config ANDROID_SDK_ROOT's tools and platform-tools directory to be searchable.

Troubleshooting:update project failed.

If you get

Error: The project either has no target set or the target is invalid.

you can add a "--target android-10" option after "android update project -p ./".

The final command is :

android update project -p ./ --target android-10

More details

if you run this script without parameters like this, 

python create_project.py


It will dump the usage for you


The script have been tested on Mac OS X 10.8.2 and Windows 7. If you find any bug, please report it on http://forum.cocos2d-x.org



总结一下就是:

解压官方cocos2dx压缩包到任意目录,然后在压缩包目录下执行

cd ./cocos2d-x/tools/project-createor/
python create_project.py -project Clams -package com.supersell.crash -language cpp

这时候会在cocos2d-x同级目录生成一个projects文件夹,这里存放着刚刚生成的各个平台的项目文件


然后就可以对应打开,因为Android没有C++的IDE所以这里单独列出了Android apk编译方法主要是使用ant,

cd ../../projects/....../pro.android/
./build_native.sh
android update project -p ./
android update project -p ../../../cocos2dx/platform/android/java
ant debug(release)
cd bin
adb install Clams-debug.apk
android update project -p ./ 这一行如果有问题可以用

android update project -p ./ --target android-10(具体可以用android list targets来查看本机安装的android版本)

下一句也一样。

注:这时候生成的这个版本是没有经过签名的版本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值