AppSeedのアプリ開発ブログ

アプリ開発会社AppSeed(アップシード)開発担当のブログです。iOS、Android、Unity、Cocos2d-xなどアプリ開発関連のTipsや備忘録、アプリ開発に役立つ情報を発信します。

【Cocos2d-x】MacのターミナルでCocosのコマンドオプション一覧を表示する方法

https://nzigen.com/ysawa/wp-content/uploads/2016/05/800x480_2dx_landscape.png


最近Cocos2d-xの仕事が入ったので、Cocos2d-x関連の備忘録も残していこうと思います。

今回は、Cocos2d-xでコマンドオプションの一覧を表示する方法について。


Cocos2d-xでコマンドオプションの一覧を表示する方法



Cocosのコマンドオプションは、以下のコマンドをMacのターミナルで入力すると確認できます。

cocos compile --help


上のコマンドを入力すると、以下のようなコマンドオプションの一覧が表示されます。

usage: cocos compile [-h] [-s SRC_DIR] [-q] [-p PLATFORM] [--list-platforms]
                     [--proj-dir PROJ_DIR] [-m MODE] [-j JOBS] [-o OUTPUT_DIR]
                     [--ap ANDROID_PLATFORM] [--build-type BUILD_TYPE]
                     [--app-abi APP_ABI] [--ndk-toolchain TOOLCHAIN]
                     [--ndk-cppflags CPPFLAGS] [--no-apk] [--no-sign]
                     [--vs VS_VERSION] [--source-map] [--advanced]
                     [-t TARGET_NAME] [--sign-identity SIGN_ID] [--no-res]
                     [--compile-script {0,1}] [--lua-encrypt]
                     [--lua-encrypt-key LUA_ENCRYPT_KEY]
                     [--lua-encrypt-sign LUA_ENCRYPT_SIGN]

Compile projects to binary.

optional arguments:
  -h, --help            show this help message and exit
  -s SRC_DIR, --src SRC_DIR
                        Specify the path of the project.
  -q, --quiet           Less output
  -p PLATFORM, --platform PLATFORM
                        Specify the target platform.
  --list-platforms      List available platforms
  --proj-dir PROJ_DIR   Specify the directory for target platform.
  -m MODE, --mode MODE  Set the compiling mode, should be debug|release,
                        default is debug.
  -j JOBS, --jobs JOBS  Allow N jobs at once.
  -o OUTPUT_DIR, --output-dir OUTPUT_DIR
                        Specify the output directory.

Android Options:
  --ap ANDROID_PLATFORM
                        Specify the Android platform used for building Android
                        apk.
  --build-type BUILD_TYPE
                        Set the compiling type of native code, should be ndk-
                        build|none, default is ndk-build. Native code will not
                        be compiled when the value is none.
  --app-abi APP_ABI     Set the APP_ABI of ndk-build.Can be multi value
                        separated with ':'. Sample : --app-abi armeabi-
                        v7a:x86. Default value is 'armeabi-v7a'.
  --ndk-toolchain TOOLCHAIN
                        Specify the NDK_TOOLCHAIN of ndk-build.
  --ndk-cppflags CPPFLAGS
                        Specify the APP_CPPFLAGS of ndk-build.
  --no-apk              Not generate the apk file.
  --no-sign             Generate unsigned apk even when release mode.

Windows Options:
  --vs VS_VERSION       Specify the Visual Studio version will be used. Such
                        as: 2015. Default find available version
                        automatically.

Web Options:
  --source-map          Enable source-map
  --advanced            Compile all source js files using Closure Compiler's
                        advanced mode, bigger compression ratio bug more risk.

iOS/Mac Options:
  -t TARGET_NAME, --target TARGET_NAME
                        Specify the target name to be compiled.

iOS Options:
  --sign-identity SIGN_ID
                        The code sign identity for iOS.

lua/js project Options:
  --no-res              Package without project resources.
  --compile-script {0,1}
                        Diable/Enable the compiling of lua/js script files.

lua project Options:
  --lua-encrypt         Enable the encrypting of lua scripts.
  --lua-encrypt-key LUA_ENCRYPT_KEY
                        Specify the encrypt key for the encrypting of lua
                        scripts.
  --lua-encrypt-sign LUA_ENCRYPT_SIGN
                        Specify the encrypt sign for the encrypting of lua
                        scripts.


Cocos2d-xのコマンドオプションが役に立った例


Cocos2d-xって結構環境依存が激しいので、既にあるプロジェクトファイルや、人の環境から自分の環境に持ってきて改修する時とかが厄介です。(特にAndroid


僕の場合、Android StudioにCocos2d-xで作られたプロジェクト(他の人が作ったもの)をインポートしようとしたんですが、エラー多発で改修が進まないことがありました。

なので、Android Studioにインポートするのは諦めて、Macのターミナルでコマンドを叩いてビルドすることにしました。


cocos run -s "プロジェクトソースパス" -p android


普通のプロジェクトだったら、上記のようなコマンドで普通にビルドできるはずなんですが、

いざコマンドを叩いてみると、以下のエラーが出てしまいます。

Python 2.7.10
Building mode: debug
Using Android Studio project : "パス"/proj.android
Building apk...

File "/Applications/cocos2d-x-3.17/tools/cocos2d-console/plugins/plugin_compile/build_android.py", line 361, in gradle_build_apk
    f = open(check_file)
IOError: [Errno 2] No such file or directory: '/Users/App-Android/proj.android/app/build.gradle'


最近のCocos2d-xだと、Android向けのプロジェクトファイルが2つ生成(Eclipse用?とAndroid Studio用)されますが、エラー内容をみる限り、Eclipse用のプロジェクトファイルでbuild.gradleを探して見つからないという変なエラーが出てました。


そこで役にやったのが、Cocosのプロジェクトディレクトリを指定できる「--proj-dir」っていうコマンドオプション。

cocos run -s /Users/App-Android --proj-dir /Users/App-Android/proj.android-studio -p android

上のような感じで、Android Studioのプロジェクトディレクトリを指定してビルドしたところ、無事ビルドに成功しました。


2時間くらい粘っても解決できず、諦めようかとも思いましたが、コマンドオプションのおかげでなんとかなりました。


アプリ開発って、結構粘り強くやると解決できる場合が結構あるなーと感じました。


ちなみに、Cocosの他のコマンドは、以下のサイトがよくまとまっていたのでオススメです↓


westplain.sakuraweb.com