Update on Android

Posted 2019-10-14

D on android almost just works out of the box - ldc can build D code, as well as the D runtime, for the android platform and package it into a shared lib. The part I'm fighting with is just integrating it ergonomically.

Core D Development Statistics

In the community

Community announcements

See more at the announce forum.

What Adam is working on

ldc functions as a cross-compiler out of the box. You can simply download the pre-build version for your host platform and pass arguments to it to get it to generate programs for other platforms. The only tricky part is getting the D runtime to work there, and that's actually pretty easy too - you can download a pre-built one for that platform and copy it in, or build it with a helper program that comes with ldc called ldc-build-runtime.

Getting specific with android, starting with the instructions on the d wiki, and using ldc-build-runtime, I ran:

ldc-build-runtime --ninja --targetPreset=Android-aarch64 --buildDir=droid64 CMAKE_C_COMPILER=clang LDC_EXE_FULL=/path/to/ldc2

And got it to build for me. I had to specify CMAKE_C_COMPILER since my system uses gcc by default (I sent a patch to ldc to fix this) and the LDC_EXE_FULL I think is required because I was using a git version.

ldc-build-runtime will try to download the ldc version. Since I was using the git one, I had to put the source inside the build directory - droid64 here - and call the subdirectory ldc-src.

But then it worked. I compiled and linked separately - compile with the appropriate mtriple (see below), then link using the linker provided with the Android ndk. I did NOT do any special steps for building a cross compiler and did NOT use clang to link, like the wiki says. I believe it is easier now than it was when that was written.

On my system, the link command ended up being:

~/Android/ndk/android-ndk-r20/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/./arm-linux-androideabi-ld.bfd -soname,libnative-activity.so -shared --sysroot=$NDK/platforms/android-21/$NDK_ARCH main.o sensor.o android_native_app_glue.o $RTDIR/lib/libphobos2-ldc.a $RTDIR/lib/libdruntime-ldc.a

Note the long starting command is just calling the ld.bfd command from the ndk directory I downloaded from google's website.

My trouble

The problem is I still don't know how to make the apk. I haven't worked with Android before and don't know gradle or the IDE. I'm sure I'll get there eventually though and when I do, I'll let you all know.

Appendix - list of common -mtriple values

The format of these is arch-vendor-os-variant.