A puzzle game of bent arrows sent off a board of dots, written in Zig.
pfeilstorch docs adr 0003-android-without-java.md
2.2 kB · 44 lines
Markdown
at main

Android runs as a NativeActivity with Zig glue, and the APK is built by Google's build-tools without Gradle #

The Android shell is a shared library loaded by the platform's own android.app.NativeActivity, so the application declares hasCode="false" and the repository holds no Java or Kotlin. The callbacks, the looper and the input queue that the NDK's android_native_app_glue would provide in C are written in Zig instead, and what can only be had from Java — the system accent color, the window insets — is asked for through JNI, which is a table of functions handed to native code rather than a library to link.

The APK is put together by zig build apk running aapt2, zipalign and apksigner from the Android build-tools in nixpkgs. There is no Gradle and no Android Gradle Plugin; apksigner needs a JDK at build time and nothing at run time.

Considered options #

  • GameActivity or a hand-written Activity: the documented route, with better support for text input and insets, but it brings Java or Kotlin into the repository and a Gradle build to compile them.
  • A packer written in Zig: binary XML, the resources table, the zip and the APK signature scheme, with no Google tool involved; possible later, but most of that work is unrelated to the game.

Consequences #

Back is left to the system, which sends the game to the background rather than quitting it: from Android 16 the system no longer passes back to an app as a key, and the callback it calls instead can only be registered from Java. That is also what Android does with back for every other app.

The Android SDK and NDK in nixpkgs are licensed by Google and unfree, so the flake accepts the Android SDK license, and those packages reach the binary cache through CI.

The shaders are compiled for Vulkan 1.2, which is the only Vulkan version Zig's SPIR-V backend has a CPU model for, while Android 10 promises only 1.1. A device with 1.1 alone cannot install the game, since the manifest asks for 1.2; those are mostly from before 2020.