Sunday, 13 April 2014

Run Cocos2dx Android Project on Windows


I assume you have already created Android project using template provided with Cocos2dx and imported it in your Eclipse workspace using from existing Android project option and converted it to C/C++ nature.

Also I assume you have installed Android NDK and set up correct path in Eclipse and your workspace location is Cocos2dx default directory.

Note:This method does not require either cygwin or python to run Android project on Windows.

Software's: Cocos2d-x-2.2.2, Windows 8.1 64-Bit and Eclipse 4.4

Step 1: Add Environment Variables

 
Go to Eclipse -> Window -> Preferences -> C/C++ -> Build -> Environment

And add NDK_ROOT and NDK_MODULE_PATH in Environment variables:

1) NDK_ROOT
            F:\Android\NDK\android-ndk-r9
2) NDK_MODULE_PATH :
            F:\Coco2dx\cocos2d-x-2.2.2;F:\Coco2dx\cocos2d-x-2.2.2\cocos2dx\platform\third_party\android\prebuilt

* Note:
If you are using version Cocos2dx-2.2.3 then you don't need to add path for prebuilt modules in NDK_MODULE_PATH. You can only use path F:\Coco2dx\cocos2d-x-2.2.3;




Step 2: Link Resources


Now in your project you have to link your Resources and Classes folders to your eclipse project:

You have to create two folder link named classes and assets If assets folder already exist then delete and recreate it.

To create folder link :
Select Project -> Right Click -> New -> Folder -> Advanced

Linking Classes :
Type classes in folder name and select Link to alternate location (Linked Folder) and type PARENT-1-PROJECT_LOC/Classes

Linking Resources :
Type assets in folder name and select Link to alternate location (Linked Folder) and type PARENT-1-PROJECT_LOC/Resources




Step 3: Setting  C/C++ Builder

Select Project -> Right-Click -> Properties -> C/C++ Build -> Builder-Settings
Uncheck Use default build command option and enter ndk-build.cmd in Build-Command and then click Apply and then click Ok. 

Now eclipse will start building your whole Cocos2dx Android project.





This method build projects faster than Cygwin because Cygwin runs under emulated environment and usually it takes longer time to copy resources to assets directory when you build and run your projects.

19 comments :

  1. After Run on emulator, I have error.

    DEBUG-LOG:
    04-14 19:16:57.892: I/ActivityManager(68): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.cocos2dx.hellocpp/.HelloCpp } from pid 131
    04-14 19:16:58.052: I/ActivityManager(68): Start proc org.cocos2dx.hellocpp for activity org.cocos2dx.hellocpp/.HelloCpp: pid=420 uid=10035 gids={}
    04-14 19:16:58.612: I/WindowManager(68): Setting rotation to 1, animFlags=1
    04-14 19:16:58.632: I/ActivityManager(68): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=1/1 orien=2 layout=34 uiMode=17 seq=13}
    04-14 19:16:59.382: D/dalvikvm(420): Trying to load lib /data/data/org.cocos2dx.hellocpp/lib/libhellocpp.so 0x40512df0
    04-14 19:16:59.702: D/dalvikvm(420): Added shared lib /data/data/org.cocos2dx.hellocpp/lib/libhellocpp.so 0x40512df0
    04-14 19:17:00.333: D/Cocos2dxActivity(420): model=sdk
    04-14 19:17:00.362: D/Cocos2dxActivity(420): product=sdk
    04-14 19:17:00.362: D/Cocos2dxActivity(420): isEmulator=true
    04-14 19:17:00.803: I/ActivityManager(68): Displayed org.cocos2dx.hellocpp/.HelloCpp: +2s776ms
    04-14 19:17:01.171: D/libEGL(420): egl.cfg not found, using default config
    04-14 19:17:01.212: D/libEGL(420): loaded /system/lib/egl/libGLES_android.so
    04-14 19:17:01.253: W/dalvikvm(420): threadid=9: thread exiting with uncaught exception (group=0x40015560)
    04-14 19:17:01.253: E/AndroidRuntime(420): FATAL EXCEPTION: GLThread 10
    04-14 19:17:01.253: E/AndroidRuntime(420): java.lang.IllegalArgumentException: No configs match configSpec
    04-14 19:17:01.253: E/AndroidRuntime(420): at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:763)
    04-14 19:17:01.253: E/AndroidRuntime(420): at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:919)
    04-14 19:17:01.253: E/AndroidRuntime(420): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1264)
    04-14 19:17:01.253: E/AndroidRuntime(420): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
    04-14 19:17:01.311: W/ActivityManager(68): Force finishing activity org.cocos2dx.hellocpp/.HelloCpp
    04-14 19:17:01.422: W/InputManagerService(68): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@407ca1c0 (uid=10035 pid=420)
    04-14 19:17:01.422: W/InputManagerService(68): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4069cd10
    04-14 19:17:01.891: D/dalvikvm(68): GC_EXPLICIT freed 499K, 49% free 4346K/8519K, external 7231K/8536K, paused 252ms
    04-14 19:17:01.902: I/WindowManager(68): Setting rotation to 0, animFlags=1
    04-14 19:17:01.921: I/ActivityManager(68): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=1/1 orien=1 layout=34 uiMode=17 seq=14}
    04-14 19:17:03.671: W/WindowManager(68): Window freeze timeout expired.
    04-14 19:17:03.671: W/WindowManager(68): Force clearing orientation change: Window{406ddd00 Sorry! paused=false}
    04-14 19:17:03.671: W/WindowManager(68): Force clearing orientation change: Window{4062dd20 StatusBar paused=false}
    04-14 19:17:03.671: W/WindowManager(68): Force clearing orientation change: Window{4066bb50 com.android.launcher/com.android.launcher2.Launcher paused=false}
    04-14 19:17:06.162: D/dalvikvm(68): GC_EXPLICIT freed 313K, 50% free 4321K/8519K, external 6836K/8536K, paused 446ms
    04-14 19:17:06.602: W/InputManagerService(68): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@405dcbc0
    04-14 19:17:06.982: I/Process(420): Sending signal. PID: 420 SIG: 9
    04-14 19:17:07.052: I/ActivityManager(68): Process org.cocos2dx.hellocpp (pid 420) has died.
    04-14 19:17:07.052: I/WindowManager(68): WIN DEATH: Window{406d3c78 org.cocos2dx.hellocpp/org.cocos2dx.hellocpp.HelloCpp paused=false}
    04-14 19:17:07.102: I/WindowManager(68): WIN DEATH: Window{408545e0 SurfaceView paused=false}
    04-14 19:17:16.412: W/ActivityManager(68): Activity destroy timeout for HistoryRecord{4067e7a0 org.cocos2dx.hellocpp/.HelloCpp}

    ReplyDelete
    Replies
    1. I would recommend you to test coco2dx apps on device only. Still you want to test it on emulator then make sure you have enabled "Use Host GPU" option while creating emulator.

      Delete
    2. Thank you! It will run on the device. But not run on emulator with "Use Host GPU" =(

      Delete
  2. I get this error:
    ndk-build.cmd
    Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml
    Android NDK: WARNING: Ignoring unknown import directory: c:\cocos2d-x-2.2.3
    Android NDK: jni/Android.mk: Cannot find module with tag 'cocos2dx' in import path
    jni/Android.mk:23: *** Android NDK: Aborting. . Stop.
    Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined ?

    ReplyDelete
    Replies
    1. Try set "\" in NDK_MODULE_PATH
      Like "c:\cocos2d-x-2.2.3\"

      Delete
    2. And add to NDK_MODULE_PATH this:
      "c:\cocos2d-x-2.2.3\cocos2dx\platform\third_party\android\prebuilt\"

      Delete
    3. Please take note from Step-1. If you are using Cocos2dx-2.2.3 then you have to set cocos2dx root directory for the NDK_MODULE_PATH variable. If your cocos2dx is in root of your Windows drive then add `\` to path.

      Delete
  3. Hello, I have a question and add native support. I do it right from your eclipse or the cmd command: ndk-build.cmd-C. APP_ABI = "armeabi armeabi-v7a x86"

    sorry for my English and thanks.

    ReplyDelete
    Replies
    1. You can provide it in arguments but i think you should use it in Application.mk file to make it clear to others working on project.

      Delete
  4. @Vikas Patidar
    Where you have said to put ndk-build.cmd in C++ build, I have put bash ${ProjDirPath}/build_native.sh

    So, I wanted to ask one serious question that, while running(in building process) the project, it always compile all the .cpp files




    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlButtonLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCLabelBMFontLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCLabelTTFLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCLayerColorLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCLayerGradientLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCLayerLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCMenuItemImageLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCMenuItemLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCNodeLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCNodeLoaderLibrary.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCParticleSystemQuadLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCScale9SpriteLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCScrollViewLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCSpriteLoader.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCBAnimationManager.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCBKeyframe.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCBSequence.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCBSequenceProperty.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCBValue.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCData.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCNode+CCBRelativePositioning.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControl.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlButton.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlColourPicker.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlHuePicker.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlSaturationBrightnessPicker.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlSlider.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlSwitch.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlUtils.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCInvocation.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCScale9Sprite.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlPotentiometer.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCControlStepper.cpp
    [armeabi] Compile++ thumb: cocos_extension_static <= CCScrollView.cpp
    ..................
    ........................ and so on.

    But shouldn't it just compile the files I've in my Classes folder, which actually belongs to projects.
    How to avoid compiling all the files every time I run them?

    ReplyDelete
    Replies
    1. All static libraries will be built when you clean your project and rebuild it.

      Delete
  5. My cocos2.3 worked all a long, im not able to get 3.0 working
    can u help me out

    ReplyDelete
    Replies
    1. I will test and post it in new blog post.

      Delete
  6. hello, I see this error: make.exe:***[obj/local/armeabi/obj/cocos2cpp_shared/hellocpp/main.o]error2

    does not create the libraries

    ReplyDelete
    Replies
    1. Please post full console error log here.

      Delete
  7. my game freezes when the screen locks on my android. Also, when the I click the the power button to lock screen screen then unlock the game doesnt freeze but there background music stops and ad stops showing.... do you any fix???

    ReplyDelete
    Replies
    1. There was an issue related to sound in coco2dx-2.2.2 when resume apps in android. Please check the version.

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete