Or, if you prefer “Creating your second cocos2d-x (v3.x) project in Eclipse”.

Abstract

I can save you having to read this by summarising immediately. All my problems were solved when I copied the “.metadata” folder to my new workspace. So “workspace\.metadata” -> “workspace2\.metadata”. Eclipse is huge and I don’t begrudge having to duplicate 100MB for every workspace anymore. You might ask what I put in there that’s 100MB, the answer will have to wait until another day. On with ironing out the wrinkles.

I’ve actually created several projects in the course of getting my first game off on the right foot. I always kept just one workspace, which seemed a big achievement back then and by deleting all my projects from it would work (quite, IIRC) well with whatever I chose to replenish it with.

I decided to write this because a lot of projects don’t work nicely with each other. I find it necessary to see a project working before I risk my working workspace with it. Plus WordPress gave me a gravatar.

I only recently discovered the ability to “switch workspace” in Eclipse. I had such a large workspace I didn’t like to risk this while my first project was incomplete. Now it’s safely tucked up I’m trying to build other projects. Let’s reuse the old workspace with previous projects removed. This is equivalent to starting a new workspace and copying the “.metadata” folder, which I’d been scared to touch while my project was unstable for a couple of months. Do beware that copying the .metadata folder to a new workspace folder did have the unexpected consequence of importing all my old projects- which I had to simply remove again.

1) empty-cpp-tests

Tried following the readme.md

#### Adding and running from Eclipse
![Example](https://lh3.googleusercontent.com/-SJohnOu6e3QbE/UUOcOXYaGqI/AAAAAAAAsdo/tYBY2SylOSM/s288/cocos2d-x-eclipse-project-from-code.png) ![Import](https://lh5.googleusercontent.com/-XzC9Pn65USc/UUOcOTAwizI/AAAAAAAAsdk/4b6YM-oim9Y/s400/cocos2d-x-eclipse-import-project.png)
1. File->New->Project->Android Project From Existing Code
2. **Browse** to your project directory. eg: `cocos2d-x/cocos2dx/samples/Cpp/CppTests/proj.android/`
3. Add the project 
4. Click **Run** or **Debug** to compile C++ followed by Java and to run on connected device or emulator.

Yeah, that’s how we roll… Or at least it was.

Unfortunately nothing was happening from within eclipse, the disk light was too dormant to indicate build activity and the console window displayed:

[2014-09-15 09:51:05 - CppTests] Unknown Application ABI: 
[2014-09-15 09:51:05 - CppTests] 
[2014-09-15 09:51:05 - CppTests] Unable to detect application ABI's
[2014-09-15 09:51:20 - CppTests] ------------------------------
[2014-09-15 09:51:20 - CppTests] Android Launch!
[2014-09-15 09:51:20 - CppTests] adb is running normally.
[2014-09-15 09:51:20 - CppTests] Performing org.cocos2dx.cpp_tests.AppActivity activity launch
[2014-09-15 09:51:20 - CppTests] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'hvga10'
[2014-09-15 09:51:20 - CppTests] Uploading CppTests.apk onto device 'emulator-5554'
[2014-09-15 09:51:21 - CppTests] Installing CppTests.apk...
[2014-09-15 09:53:21 - CppTests] Failed to install CppTests.apk on device 'emulator-5554!
[2014-09-15 09:53:21 - CppTests] (null)
[2014-09-15 09:53:21 - CppTests] Launch canceled!

Frustrated at this distinct lack of helpful feedback from Eclipse I returned to the command line. The readme.md in cocos2d-x-3.2\tests\cpp-tests\proj.android is completely out of sync with cocos2d-x-v3.2. (or v3.1). It talks about installing cygwin, which I have done, but only because of all the stale documentation I tried to follow to get things up and running the first time. Thankfully it is described as a once only event.

 ### Running project from Command Line
    $ cd cocos2d-x/samples/Cpp/CppTests/proj.android/
    $ export NDK_ROOT=/path/to/ndk
    $ ./build_native.sh
    $ ant debug install

There is no build_native.sh anymore. It was replaced by ndk-build which in turn has been wrapped up in android-build.py in the build directory of the cocos2d-x source tree.

Just typing that command in that directory told me exactly what I needed to do:

C:\Users\John\cocos2d-x-3.2\build>android-build.py -p 10 cpp-empty-test

and sure enough my disk sparked back to life with a long list of cpp file names being built (omitted for clarity), a couple of warnings and one final exclamation that ant is unavailable:

[armeabi] Compile++ thumb: cocos2dxandroid_static <= TouchesJni.cpp
1 warning generated.
[armeabi] Compile thumb : cpufeatures <= cpu-features.c
[armeabi] StaticLibrary : libcocos2d.a
[armeabi] StaticLibrary : libcocosdenshion.a
[armeabi] StaticLibrary : libchipmunk.a
[armeabi] StaticLibrary : libcpufeatures.a
[armeabi] StaticLibrary : libcocos2dandroid.a
[armeabi] SharedLibrary : libcpp_empty_test.so
[armeabi] Install : libcpp_empty_test.so => libs/armeabi/libcpp_empty_tes
t.so
make.exe: Leaving directory `C:/Users/John/cocos2d-x-3.2/tests/cpp-empty-test/proj
.android'
Updated project.properties
Updated local.properties
Updated file C:\Users\John\cocos2d-x-3.2\build\..\cocos\platform\android\java\prog
uard-project.txt
Updated project.properties
Updated local.properties
Updated file C:\Users\John\cocos2d-x-3.2\build\..\tests\cpp-empty-test\proj.androi
d\proguard-project.txt
'ant' is not recognized as an internal or external command,
operable program or batch file.

Naturally I check my build variables at this point:

C:\Users\John\cocos2d-x-3.2\build>set
ANT_ROOT=C:\apache-ant-1.9.4\bin
COCOS_CONSOLE_ROOT=C:\Users\John\cocos2d-x-3.2\tools\cocos2d-console\bin
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_60
NDK_ROOT=C:\Android\android-ndk-r9d\
NDK_WIN_HOST=windows-x86_64
Path=C:\Program Files (x86)\ImageMagick-6.7.8-Q16;C:\Python27\;C:\Windows\system
32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.
0\;C:\cyg64\bin;C:\Program Files\eclipse\plugins\org.apache.ant_1.8.4.v201303080
030\bin;C:\Program Files\doxygen\bin;C:\Program Files\Microsoft SQL Server\110\T
ools\Binn\;C:\Users\John\cocos2d-x-3.2\tools\cocos2d-console\bin;;C:\Android\andro
id-ndk-r9d
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

C:\Users\John\cocos2d-x-3.2\build>ndk-build

But nothing is flagged as a fatal error so for reassurance I check android-build.py:

Note: You should install ant to generate apk while building the andriod tests. But it is optional. You can generate apk with eclipse.

Now everything is as the developers intended and working. It’s worth me noting potential pit falls here:

  1. Ensure your eclipse project matches what you just built, I had cpp_tests as eclipse source and although that now linked against a compiled cocos2d library it failed at run time in the load library stage.
  2. Check that the newly built tests project is linked against the library, it wasn’t for me but I’m used to that.
  3. When you Press F11 to run/debug don’t choose Android Native Application (it has a red box to its left) but instead you must choose to debug as an Android Application.
  4. Don’t try running it on Android 2.3.3 (API Level 10) emulator, I know cocos2d-x is supposed to work on Android 9 (2.3 – 2.3.2) and above but it won’t on the emulator. I think this is to do with OpenGL 2.0 emulation on the old emulator, or maybe they’ve updated API Level 9 since. The error occurs in android.opengl.GLSurfaceView.
  5. Have your emulator already running when you press F11, otherwise Eclipse might make it’s own. This is what happened to me, and although it looked like the right device it evidently wasn’t. It was crashing or not loading properly.

So far so good so not many pictures 🙂

run configs

projrefs

2) cpp-tests

Following the form set out above:

cd C:\Users\John\cocos2d-x-3.2\build

android-build.py -p 10 cpp-tests

That stormed along until CocosGUIScene.cpp where it stuck for 10 minutes and I had to kill it, no offence CocosGUI, I still love ya  ;)

I hit Ctrl+C twice to kill the process then restarted it as before:

[armeabi] Compile++ thumb: cocos2dxandroid_static <= JniHelper.cpp
In file included from C:\Users\John\cocos2d-x-3.2\build\../cocos/platform/android/
jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp:32:
In file included from C:\Users\John\cocos2d-x-3.2\build\../cocos/platform/android/
../..\deprecated/CCString.h:35:
In file included from C:/Android/android-ndk-r9d/sources/cxx-stl/llvm-libc++/lib
cxx/include\sstream:174:
In file included from C:/Android/android-ndk-r9d/sources/cxx-stl/llvm-libc++/lib
cxx/include\ostream:131:
In file included from C:/Android/android-ndk-r9d/sources/cxx-stl/llvm-libc++/lib
cxx/include\ios:216:
In file included from C:/Android/android-ndk-r9d/sources/cxx-stl/llvm-libc++/lib
cxx/include\__locale:21:
In file included from C:/Android/android-ndk-r9d/sources/cxx-stl/llvm-libc++/../
../android/support/include\locale.h:33:
C:/Android/android-ndk-r9d/platforms/android-9/arch-arm/usr/include\locale.h:55:
1: warning:
 empty struct has size 0 in C, size 1 in C++ [-Wextern-c-compat]
struct lconv { };
^
[armeabi] Compile++ thumb: cocos2dxandroid_static <= TouchesJni.cpp
[armeabi] Compile thumb : cpufeatures <= cpu-features.c
[armeabi] StaticLibrary : libcpp_tests_common.a
1 warning generated.
[armeabi] StaticLibrary : libcocosbuilder.a
[armeabi] StaticLibrary : libspine.a
[armeabi] StaticLibrary : libcocostudio.a
[armeabi] StaticLibrary : libnetwork.a
[armeabi] StaticLibrary : libui.a
[armeabi] StaticLibrary : libextension.a
[armeabi] StaticLibrary : libcocos2d.a
[armeabi] StaticLibrary : libcocosdenshion.a
[armeabi] StaticLibrary : libbox2d.a
[armeabi] StaticLibrary : libchipmunk.a
[armeabi] StaticLibrary : libcpufeatures.a
[armeabi] StaticLibrary : libcocos2dandroid.a
[armeabi] SharedLibrary : libcpp_tests.so
[armeabi] Install : libcpp_tests.so => libs/armeabi/libcpp_tests.so
make.exe: Leaving directory `C:/Users/John/cocos2d-x-3.2/tests/cpp-tests/proj.andr
oid'
Updated project.properties
Updated local.properties
Updated file C:\Users\John\cocos2d-x-3.2\build\..\cocos\platform\android\java\prog
uard-project.txt
Updated project.properties
Updated local.properties
Updated file C:\Users\John\cocos2d-x-3.2\build\..\tests\cpp-tests\proj.android\pro
guard-project.txt
Updated and renamed default.properties to project.properties
Updated local.properties
No project name specified, using Activity name 'AppActivity'.
If you wish to change it, edit the first line of build.xml.
Added file C:\Users\John\cocos2d-x-3.2\build\..\tests\cpp-tests\proj.android\bin\b
uild.xml
Added file C:\Users\John\cocos2d-x-3.2\build\..\tests\cpp-tests\proj.android\bin\p
roguard-project.txt
'ant' is not recognized as an internal or external command,
operable program or batch file.

Now I find a big 100Mb apk in the build directory of the cpp-tests project but Eclipse isn’t going to acquiesce to letting me run it on an emulator. I know it’s big but I can’t justify leaving it more than 10 minutes to launch on the emulator. I recall a similar problem before when I first built cpp-tests a couple of months ago and it worked on my phone so I’m just going to copy it over and run it there, where it will be of more use anyway.

No sooner had I plugged my phone in than I got a response from Eclipse’s console (at 11:57):

[2014-09-15 11:49:44 - CppTests] ------------------------------
[2014-09-15 11:49:44 - CppTests] Android Launch!
[2014-09-15 11:49:44 - CppTests] adb is running normally.
[2014-09-15 11:49:44 - CppTests] Performing org.cocos2dx.cpp_tests.AppActivity activity launch
[2014-09-15 11:49:45 - CppTests] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'hvga16'
[2014-09-15 11:49:45 - CppTests] Uploading CppTests.apk onto device 'emulator-5554'
[2014-09-15 11:57:23 - CppTests] Installing CppTests.apk...
[2014-09-15 11:57:26 - CppTests] Installation error: INSTALL_FAILED_MEDIA_UNAVAILABLE
[2014-09-15 11:57:26 - CppTests] Please check logcat output for more details.
[2014-09-15 11:57:26 - CppTests] Launch canceled!

Eclipse was running with my phone in no time but I was fully prepared to have to copy to apk across, with a crowbar.

Sprite3D

Screenshot_2014-09-15-12-18-18

That’s enough for one post. I’ve edited this because the first draught was written from my old workspace anyway. I think there’s something to be said for keeping just one workspace.