cover_small

Running && Logging Cocos2d-JS Game on Android Emulator

Target

Compiler && Install && Run && Debug Logging cocos2d-js game on Android Emulator without using any heavy IDE such as Eclipse Android Studio and simplify && minimize the installation requirements && process.

Enviroment

  • cocos2d-js v3.10
  • Mac OS X
  • android-ndk-r9d
  • Android 4.4.2 (API 19)

Installation

jdk

Used for compiler .java files in game.Link

Android-SDK

Used for signing and generating .apk etc.Link

Android-NDK

Used for compiler c++ and generating .so .a etc.Link

android-ndk-r9d is recommended to match the command line tool cocos in cocos2d-js v3.10.

Setup

After installing Android-SDK and Android-NDK we need to make some modifications to .bash_profile file:

# NDK
export NDK_ROOT=/Users/android/android-ndk-r9d
export PATH=$PATH:$ANDROID_NDK_ROOT

# SDK
export ANDROID_SDK_ROOT=/Users/android/android-sdk-macosx
export PATH=$PATH:$ANDROID_SDK_ROOT

# add the two folders for convenience
export PATH=$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$PATH
export PATH=$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/build-tools/23.0.3:$PATH

Save && Exit && source ~/.bash_profile

AVD (Android Virtual Deice)

Install SDK Image

There are lots of command line tool under android-sdk-macosx/tools that actually are used by Eclipse to provide convenience operations.

root: android

This command will open the visual tool — Android-SDK-Manager which we can use to install the modules we need for creating AVD.

The modules we need here are shown as below:

+ Tools
    - Android SDK Tools
    - Android SDK Platform-tools
    - Android SDK Build-tools
+ Android 4.4.2 (API 19)
    - SDK Platform
    - Google APIs Intel x86 Atom System Image
+ Extras
    - Intel x86 Emulator Accelerator (HAXM Installer)
  • The Android 4.4.2 (API 19) is recommended to math the auto generated Android project by cocos2d-js v3.10

  • Google APIs Intel x86 Atom System Image us the image file for virtual device and with Intel x86 Emulator Accelerator the emulator will run much faster

Create && Run AVD

After downloading the Image we are ready to create our AVD:

root: android avd

This command will open the visual tool — Android-Vitural-Device-Manager.

avd

We can select the Image we just downloaded to create a new AVD.

So, a AVD is consist of Image + SDK + Device + Skin.

avd_create_new

After creating AVD we can start to run it with the following command:

root: emulator avd "device name"

avd_emulator

Generate .apk

With the command line tool cocos in cocos2d-js we can generate the .apk at ease but still we need to make some modifications to some files first.

Modifications

The folder structure of the Android project — proj.android generated by cocos is (take a project — CocosProtobuf for example):

+ CocosProtobuf
    + frameworks
        + runtime-src
            + Classes
            + proj.android

First we need to change the jni/Android.mk :

LOCAL_SRC_FILES := hellojavascript/main.cpp \
                   ../../Classes/AppDelegate.cpp 

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

As you can see the origin Android.mk listed all the source files in our project and that is pretty troublesome.

So we change this part to:

MY_FILES_PATH :=  $(LOCAL_PATH)
MY_FILES_PATH +=  $(LOCAL_PATH)/../../Classes
                   
MY_FILES_SUFFIX := %.cpp %.c %.cc

rwildcard = $(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

MY_ALL_FILES := $(foreach src_path,$(MY_FILES_PATH), $(call rwildcard,$(src_path),*.*) ) 
MY_ALL_FILES := $(MY_ALL_FILES:$(MY_CPP_PATH)/./%=$(MY_CPP_PATH)%)
MY_SRC_LIST  := $(filter $(MY_FILES_SUFFIX),$(MY_ALL_FILES)) 
MY_SRC_LIST  := $(MY_SRC_LIST:$(LOCAL_PATH)/%=%)

define uniq =
    $(eval seen :=)
    $(foreach _,$1,$(if $(filter $_,${seen}),,$(eval seen += $_)))
    ${seen}
endef
MY_ALL_DIRS := $(dir $(foreach src_path,$(MY_FILES_PATH), $(call rwildcard,$(src_path),*/)))
MY_ALL_DIRS := $(call uniq,$(MY_ALL_DIRS))

LOCAL_SRC_FILES  := $(MY_SRC_LIST)
LOCAL_C_INCLUDES := $(MY_ALL_DIRS)

And now all the cpp .c .cc will be compiled without having to list all of them in the Android.mk.

Then we need to change the Application.mk:

# APP_ABI := armeabi-v7a

We need to restore the commented out line and then change the APP_ABI to x86 so that we can install the generated .apk on our AVD

APP_ABI := x86

Now we can compile and generate the .apk using cocos command :

cocos compile -p android

Running .apk && Logging

Running

The generated .apk can be found in both proj.android/bin and CocosProtobuf/simulator/android.

First we need to start our AVD created before :

root: emulator avd "device name"

And then we can install the .apk :

root: adb install CocosProtobuf-debug.apk

After this we can see the icon of the .apk on our AVD.

Logging

With another command line tool in Android-SDK :

root: monitor

we can easily start the visual logging tool just like in Eclipse.

log_monitor

Now we only need to add some filters to see our game log.

log_monitor_filter

log_monitor_filter_result