I will write which stones I fell over during the process of getting Box2D to compile as a ShiVa-Android-plugin. This could also be useful for anybody who wants to use STL on Android.
Disclaimer: I am far no C++, build or make file guru. If you spot anything, which could be done better, please tell me (I am willing to learn).
First, modify the MyPlugin.makefile (located in “/ShiVaEditor/Data/PluginTemplate/Make/Android/”) such that CPPFLAGS looks like this:
CPPFLAGS = -fno-rtti -I"$(ANDROID_NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/include" -I"$(ANDROID_NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include"
Then in ShiVa, regenerate your Android plugin makefile and click on recompile (RE-compile, not compile). If everything works out right, you should see the red circle next to the android plugin turned green:
Now you’re ready to export!
Export your game with the Android profile selected. This is important, because when using plugins, only the selected profile version of the plugin is packed into the STK. So if you export with the DefaultProfile, the Android plugin will probably be missing! Note: After exporting, you can always look at the yourgame.xml to see what files are packed into the STK. Just open it with your browser.
With the freshly exported STK, now create an Android project with UAT. Unzip it’s contents and make the following changes to these two files:
Add the line
APP_STL := gnustl_static
to where the Application.mk is defined (2 spots in the build.xml: for release and debug).
Add the line
LOCAL_LDLIBS += -lgnustl_static
after the last LOCAL_LDLIBS assignments.
Now compile the build.xml and hope that everything works I have come accross some weird build errors, they are handled in the next section.
Credits to this whole procedure go to supernat on this thread in the ShiVa forum! In case you want to use STLPORT instead of GNUSTL, you can see what changes need to be made over at the forum. I stuck with GNUSTL. Also big thanks to Dominik from StarkApps.
Box2D Specific Modifications
I don’t really have a clue, what precompiled headers do (nice rhyme eh?), but I managed to get them to compile. In the end, I had to add a
#include "PrecompiledHeader.h" to nearly every Box2D source file (the compiler gave some tips/warnings that I must do so). As I had done this, the plugin started to compile for Windows. For Android, I got some weird error like “error in algorithm” or “error in stdlib.h”, which were quite misleading.
In the end, I simply MOVED the failing include statements to the precompiled header file (meaning removing them from the Box2D sources and adding them to the precompiled header file), leading to these additional include statements in the precompiled header file (~ stands for < or > …had some trouble with html editor):
#include ~stdlib.h~ #include ~cstdlib~ #include ~cstdio~ #include ~cstdarg~ #include ~climits~ #include ~cstring~ #include ~memory~ #include ~algorithm~
The funny thing now is, there’s a precompiled header file for each platform and I only added these includes to the windows one. I actually don’t know why this is working on Android, but sometimes you just have to stop asking questions If anybody out there has an idea, please tell me.
b2BroadPhase And std::sort
The file b2BroadPhase.h has a call to std::sort, which the Android compiler complained that he can’t find. Looking at the GNUSTL headers/templates, it read like that for GNUSTL, this isn’t in the std namespace … but I’m not sure about that one. Reading headers and templates in notepad++ isn’t a good idea with all those macros and defines. For the first version, I just commented the line out, as it’s just a performance optimization anyway.
Don’t use backslashes in include statements, use slashes! Meaning don’t write
#include "Box2D\Box2D.h", instead write
#include "Box2D/Box2D.h". Not sure if this would have also failed on Android, but on iOS it raised an error.
That was it for now. I’ll keep updating the issues list as I keep working on it and maybe I’ll have some insights on the unclear points.