Haxe/NME Android on Arch

These are some notes, the tricky parts mostly from tom5760, on building Haxe/NME apps for Android.

First install Ant and Java using the standard tools. Despite various warnings around, I have not had a problem using Java7 (OpenJDK).  Then install the Android tools using the AUR packages android-sdk android-sdk-platform-tools android-ndk. I use packer, so it’s as simple as:

$ sudo packer -S android-sdk \
                 android-sdk-platform-tools android-ndk

After that you need to use the android manager to install support for your API and device targets, like usual.

Next configure NME with the right paths by running nme setup android. Note that you need to do this as your user, not sudo. Opt not to download any of the packages. If you did a standard install you should have JAVA_HOME and ANT_HOME set for it to detect, but these are the standard paths:

  • SDK: /opt/android-sdk
  • NDK: /opt/android-ndk
  • Ant: /usr/share/apache-ant
  • Java: /usr/lib/jvm/java-7-openjdk

After that, projects still won’t compile. You’ll probably get errors like this:

Creating hxcpp.h.gch...
[ huge compile command... ]
/usr/lib/haxe/lib/hxcpp/2,10,2//include/hxcpp.h:13:20: error: typeinfo: No such file or directory
In file included from /usr/lib/haxe/lib/hxcpp/2,10,2//include/hxcpp.h:170: /usr/lib/haxe/lib/hxcpp/2,10,2//include/Array.h:195:21: error: algorithm: No such file or directory
In file included from /usr/lib/haxe/lib/hxcpp/2,10,2//include/hxcpp.h:162: /usr/lib/haxe/lib/hxcpp/2,10,2//include/hx/Object.h: In member function 'void hx::ObjectPtr::CastPtr(hx::Object*)': /usr/lib/haxe/lib/hxcpp/2,10,2//include/hx/Object.h:143: error: must #include before using typeid
In file included from /usr/lib/haxe/lib/hxcpp/2,10,2//include/hxcpp.h:170: /usr/lib/haxe/lib/hxcpp/2,10,2//include/Array.h: In member function 'void Array_obj::sort(Dynamic)': /usr/lib/haxe/lib/hxcpp/2,10,2//include/Array.h:388: error: 'sort' is not a member of 'std'
In file included from /usr/lib/haxe/lib/hxcpp/2,10,2//include/hxcpp.h:171: /usr/lib/haxe/lib/hxcpp/2,10,2//include/Class.h: In function 'bool hx::TCanCast(hx::Object*)': /usr/lib/haxe/lib/hxcpp/2,10,2//include/Class.h:139: error: must #include before using typeid
Called from ? line 1
Called from BuildTool.hx line 1301
Called from BuildTool.hx line 567
Called from BuildTool.hx line 604
Called from BuildTool.hx line 738
Called from BuildTool.hx line 767
Called from BuildTool.hx line 162
Uncaught exception - Error creating pch: 256 - build cancelled
Error: Source path "bin/android/obj/libApplicationMain.so" does not exist

These are caused by Haxe/NME not locating the NDK libraries correctly. Recent versions of Android reorganized the directory structure a bit, so you have to do this:

cd $ANDROID_NDK/sources/cxx-stl/gnu-libstdc++
ln -s 4.4.3/libs
ln -s 4.4.3/include

Apparently version 4.6 (the latest as of this writing) doesn’t work.

At that point, everything should work, but I got an error like this:

Copy bin/android/obj/libApplicationMain.so to bin/android/bin/libs/armeabi/libApplicationMain.so
cd bin/android/bin
/SDKs//ant/bin/ant debug
sh: /SDKs//ant/bin/ant: No such file or directory
Called from ? line 1
Called from InstallTool.hx line 679
Called from InstallTool.hx line 119
Called from installers/InstallerBase.hx line 229
Called from installers/AndroidInstaller.hx line 56
Called from helpers/AndroidHelper.hx line 53
Called from helpers/ProcessHelper.hx line 125
Called from InstallTool.hx line 152
Called from /usr/lib/haxe/std/neko/Lib.hx line 63
Called from helpers/ProcessHelper.hx line 119
Called from helpers/ProcessHelper.hx line 169
Uncaught exception - Error running: /SDKs//ant/bin/ant debug [bin/android/bin]

To fix that, I edited ~/.hxcpp_config.xml to set the various paths:

<section id="vars">
<set name="SDK_ROOT" value="/SDKs/" />
<set name="ANDROID_SDK" value="/opt/android-sdk" />
<set name="ANDROID_SETUP" value="true" />
<set name="ANDROID_NDK_ROOT" value="/opt/android-ndk/" />
<set name="ANT_HOME" value="/usr/share/apache-ant" />
<set name="JAVA_HOME" value="/usr/lib/jvm/java-7-openjdk" />
</section>

I have not done more yet to figure out why I needed to do that.

Note also that you need to include the following NDLLs in your .nmml to compile for Android, though they don’t seem to be needed for other platforms:

<ndll name="std" />
<ndll name="regexp" />
<ndll name="zlib" />
<ndll name="nme" haxelib="nme" />

The latter seems to be needed even if including the nme haxelib. Again, I have not investigated farther.

The Android target also does not like the portrait="*" option in the nmml file, you must specify landscape or portrait.

At this point, you should be able to build and run Android apps, e.g.:

/usr/lib/haxe/lib/nme/3,4,4/samples/02-Text/
nme test Sample.nmml android

Note that the above will only work if you can write to the samples dir, e.g., chown -R joe /usr/lib/haxe/lib/nme/3,4,4/samples/.