1. 程式人生 > >cocos2dx 命令建立各平臺工程,並自動打包Android apk的方法

cocos2dx 命令建立各平臺工程,並自動打包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版本)

下一句也一樣。

注:這時候生成的這個版本是沒有經過簽名的版本。