On Tuesday, 7 April 2020 at 14:51:15 UTC, H. S. Teoh wrote:
On Tue, Apr 07, 2020 at 12:43:20PM +0000, Adam D. Ruppe via
Digitalmars-d-learn wrote:
On Monday, 6 April 2020 at 08:38:03 UTC, Jan Hönig wrote:
> Is there some "Hello World!" example for D on Android?
[...]
> However there is just so much to know.
> It is really overwhelming.
no kidding, I spent several weekends just trying to understand
the setup and build process. And I still basically don't
really know, which is why my thing there builds the D code
externally and copies the files where gradle can find them
instead of actually changing the gradle config.
I managed to build APKs without Gradle (yes, I'm crazy like
that). There are several steps, and you do need some tools from
the Android SDK/NDK, namely aapt, apksigner, dx (called
dalvik-exchange on some distros), zipalign, and a Java 1.7
compiler (the last time I checked; maybe they support 1.8 now,
I don't know).
I haven't tried a native app so far, so the way I set it up is
to create a dummy Java wrapper that contains main() that calls
native methods that are implemented in D. You can use Adam's
jni.d to generate implementations for your native methods.
Steps:
1) Follow LDC wiki to build an Android cross-compiler and
cross-compiled
LDC libraries (this may already be prepackaged with the
latest LDC
releases). Most important thing you need is the path to the
droid32
or droid64 directories containing the libraries
libdruntime-ldc.a,
libphobos2-ldc.a, libdruntime-ldc-debug.a,
libphobos2-ldc-debug.a.
DROID32_PATH=/usr/src/d/android/droid32/lib # for example
2) Generate R.java:
TARGET_API_LEVEL=23 # for example
/usr/bin/aapt package -f -m -M AndroidManifest.xml -S res -I
${PATH_TO_ANDROID_SDK}/platforms/android-${TARGET_API_LEVEL}/android.jar -J ${PATH_TO_JAVA_SOURCE_CODE}
3) Compile Java sources:
/usr/bin/javac -source 1.7 -target 1.7 -bootclasspath
${PATH_TO_ANDROID_SDK}/platforms/android-${TARGET_API_LEVEL}/android.jar -d obj -sourcepath src ${JAVA_SOURCE_FILES (including R.java)}
4) Generate classes.dex (on some systems dalvik-exchange might
be called
simply 'dx'):
/usr/bin/dalvik-exchange --dex --output=classes.dex
${PATH_TO_OBJ_DIR}
5) Setup linker config file (for optimizing apk size):
LINKER_VERSION_FILE=lib${YOUR_APP_NAME}.version
cat > $LINKER_VERSION_FILE
LIBGAME_1.0 {
global:
Java_*;
local:
*;
};
6) Cross-compile D code with LDC:
${PATH_TO_LDC}/bin/ldmd2 -c
-mtriple=armv7-none-linux-androideabi -mcpu=cortex-a8
-L-L${DROID32_PATH}/lib -Xcc=-fpie -Xcc=-pie
-Xcc=--sysroot=${PATH_TO_ANDROID_NDK}/platforms/android-${TARGET_API_LEVEL}/arch-arm -Xcc=-fuse-ld=bfd -Xcc=-gcc-toolchain -Xcc=${PATH_TO_ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -Xcc=-target -Xcc=armv7-none-linux-androideabi -O4 -inline -Isrc/d -od${PATH_TO_OBJ_DIR} ${D_SOURCE_FILES}
${PATH_TO_ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-Wl,-soname,lib${YOUR_APP_NAME}.so -shared -Wl,--gc-sections
-Wl,--version-script=${LINKER_VERSION_FILE} -llog -landroid -lEGL -lGLESv2
--sysroot=${PATH_TO_ANDROID_NDK}/platforms/android-${TARGET_API_LEVEL}/arch-arm
-fuse-ld=bfd -gcc-toolchain
${PATH_TO_ANDROID_NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
-target armv7-none-linux-androideabi ${D_OBJECT_FILES}
${DROID32_PATH}/lib/libphobos2-ldc.a ${DROID32_PATH}/lib/libdruntime-ldc.a -o
lib/armeabi-v7a/lib${YOUR_APP_NAME}.so
7) Create unaligned APK:
/usr/bin/aapt package -f -m -M AndroidManifest.xml -S res -I
${PATH_TO_ANDROID_SDK}/platforms/android-${TARGET_API_LEVEL}/android.jar -F bin/${YOUR_APP_NAME}.unaligned.apk
/usr/bin/aapt add bin/${YOUR_APP_NAME}.unaligned.apk
classes.dex
/usr/bin/aapt add bin/${YOUR_APP_NAME}.unaligned.apk
lib/armeabi-v7a/lib${YOUR_APP_NAME}.so
8) Sign the APK:
/usr/bin/apksigner sign --ks-pass
file:${YOUR_SIGNING_KEY_PASSWORD_FILE} --ks
${YOUR_SIGNING_KEY_STORE_FILE}
bin/${YOUR_APP_NAME}.unaligned.apk
9) Align the APK:
/usr/bin/zipalign -f 4 bin/${YOUR_APP_NAME}.unaligned.apk
bin/${YOUR_APP_NAME}.apk
10) Copy bin/${YOUR_APP_NAME}.apk to your device and install it.
T
Thanks for the effort. I will try the "easy" way frist :D