Android apps

radlang build --target android turns a radlang program into an Android app for arm64-v8a devices (every current phone and the Apple Silicon emulator):

radlang build --target android
# -> output/release/android/<name>.aab              upload to Google Play
# -> output/release/android/<name>.apk              adb install, for testing
# -> output/release/android/lib/arm64-v8a/libmain.so   kept for crash symbolization

It builds without Gradle or Android Studio projects, using an installed Android SDK and a JDK.

  • syn.webview (and gui.window) apps run in a full-screen WebView. This is how NeuronLab ships.
  • Console and server programs run with their output shown in a scrolling console view.
  • Native syn.window apps render with OpenGL ES 3 in a NativeActivity (see Native syn apps).

Requirements

Piece Where radlang looks
Android SDK ANDROID_HOME, then ANDROID_SDK_ROOT, then ~/Library/Android/sdk (macOS) or ~/Android/Sdk
NDK (r26 or newer) ANDROID_NDK_HOME / ANDROID_NDK_ROOT, else the newest <sdk>/ndk/<version>
build-tools (aapt2, d8, apksigner) the newest <sdk>/build-tools/<version> that has all three
Platform 35 or 36 <sdk>/platforms/android-35 or android-36 (the newest is the targetSdk)
bundletool RADLANG_BUNDLETOOL (a bundletool-all jar or launcher), else the jars Android Gradle builds leave in ~/.gradle, else bundletool on PATH
JDK 17+ RADLANG_JAVA_HOME, JAVA_HOME, then (macOS) /usr/libexec/java_home -v 17, then javac on PATH

Install the SDK pieces with Android Studio’s SDK Manager or the command-line tools:

sdkmanager "platforms;android-36" "build-tools;36.0.0" "ndk;28.2.13676358"

The first Android build of a checkout compiles the Android runtime archive (compiler/runtime/android-arm64/runtime.a, about 20 seconds). It also builds the app shell’s classes.dex with javac and d8. Namespace libraries are compiled per target into lib/android-arm64/.

Configuration

App identity comes from project:. Android settings go in the build: section of config.yaml (see the README’s “Build and signing settings”).

project:
  name: "NeuronLab"                 # launcher label and log tag
  bundleId: "com.radark.neuronlab"  # applicationId, unless build.android.package is set
  version: "1.8.3"                  # versionName; versionCode is derived from it
  icon: "public/icon.png"           # launcher icon (mipmaps are generated)

build:
  android:
    package: "com.radark.neuronlab" # optional applicationId override
    minSdk: 26                      # default 26 (Android 8.0); may be raised, never lowered
    targetSdk: 36                   # default: the newest installed of 36 and 35
    versionCode: 1008003            # default: derived from project.version
    permissions: [CAMERA]           # extra <uses-permission>; bare names get android.permission.
    cleartextTraffic: false         # true allows plain http:// (blocked by default)
    manifest: "android/manifest-extra.xml"   # optional extra manifest XML
    sign:
      keystore: "~/.android-keys/upload.jks"
      alias: "upload"
      storePassword: "keychain:radlang-android-upload/upload"
      keyPassword: "env:UPLOAD_KEY_PASS"   # optional; defaults to storePassword
Key Meaning
project.name The app label on the launcher and the logcat tag of the program’s output.
project.bundleId The applicationId (Play’s package name). Without one radlang uses dev.radlang.<name>.
project.version versionName.
project.icon A square PNG. radlang resizes it to the five launcher densities (with sips on macOS; elsewhere the image is used as-is at the highest density). Without one, Android’s default icon is used.
build.android.package Overrides the applicationId. It needs two or more dot-separated segments, each starting with a letter.
build.android.minSdk minSdkVersion. The default is 26, the release the runtime is compiled for.
build.android.targetSdk targetSdkVersion and compile SDK. That platform must be installed.
build.android.versionCode versionCode. The default is major*1000000 + minor*1000 + patch from project.version (1.8.3 → 1008003). That always increases with the version, so Play accepts each upload.
build.android.permissions Extra permissions. INTERNET is added automatically when the program uses sockets (an HTTP server or client, including streamSync and libsql:// databases, TCP, WebSocket, SSE).
build.android.cleartextTraffic true sets android:usesCleartextTraffic, allowing plain http:// from the HTTP client and the WebView. Android blocks it by default; an HTTP client request that hits the block fails with a message naming this key.
build.android.manifest Extra XML for the generated AndroidManifest.xml. See below.
build.android.sign The upload key. See Signing.

Unknown keys under build.android (or its sign:) print a warning that names the key.

build.android.manifest names a file of manifest elements radlang splices into the manifest it generates. Wrap them by where they go:

<radlang-manifest>
  <in-manifest>      <!-- children of <manifest>: permissions, <queries>, features -->
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  </in-manifest>
  <in-application>   <!-- children of <application>: providers, services, meta-data -->
  </in-application>
  <in-activity>      <!-- children of the launcher <activity>: intent filters -->
    <intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="myapp"/>
    </intent-filter>
  </in-activity>
</radlang-manifest>

A file without these wrappers is taken as children of <manifest>. The android: namespace is declared by the generated manifest, so fragments use it without declaring it. aapt2 validates the result.

Signing

Google Play needs the .aab signed with your upload key. Play then re-signs installs with the app signing key it holds. When build.android.sign is present, every Android build is signed:

  • the .aab with jarsigner, then verified with jarsigner -verify;
  • the universal .apk with apksigner (APK Signature Scheme v2/v3), then verified with apksigner verify.

storePassword and keyPassword take a reference, never a password. A literal value is a build error.

Reference Reads
keychain:<service>/<account> A macOS keychain generic password (security find-generic-password -s <service> -a <account> -w). Store one with security add-generic-password -s <service> -a <account> -w.
env:<VARIABLE> An environment variable.

The resolved password reaches jarsigner and apksigner only through their environment (-storepass:env, --ks-pass env:). It is never written to disk, passed on a command line or printed. The keystore may be PKCS12 or JKS.

Overrides for CI, in precedence order (a flag beats an environment variable, which beats config.yaml):

Override Meaning
--sign Require signing. It fails when no keystore is configured.
--unsigned Skip signing even when build.android.sign is set.
RADLANG_ANDROID_KEYSTORE The keystore path.
RADLANG_ANDROID_KEY_ALIAS The key alias.
RADLANG_ANDROID_KEYSTORE_PASSWORD The store password itself (for CI secret stores).
RADLANG_ANDROID_KEY_PASSWORD The key password itself.

Without any signing configuration the build still succeeds, with a warning:

  • the .aab is unsigned;
  • the universal .apk is signed with the debug key when ~/.android/debug.keystore exists, so adb install still works.

--notarize is macOS-only and is rejected.

Running it

adb install -r output/release/android/<name>.apk
adb shell am start -n <applicationId>/dev.radlang.app.RadActivity        # native syn apps: …/dev.radlang.app.RadNativeActivity
adb logcat --pid=$(adb shell pidof <applicationId>)

To test the bundle as Play would deliver it, use bundletool build-apks --bundle <name>.aab --output app.apks --connected-device and then bundletool install-apks --apks app.apks.

How it works

  • The program is a JNI library. radlang compiles it for aarch64-unknown-linux-android26 and links it with the NDK’s clang into libmain.so. Only JNI_OnLoad is exported, and the segments are 16 KB aligned, as Google Play requires for apps targeting Android 15 and later. Native libraries are stored uncompressed in the APK.
  • A small Java shell hosts it. RadActivity and RadHost (in compiler/runtime/android/java) load libmain.so and run main on its own native thread with an 8 MB stack. main runs once per process; a recreated activity reattaches to the running program.
  • Windows are WebViews. Calls such as win.loadFile, win.eval and win.bind are forwarded to the UI thread in call order.
  • syn.run(win) never returns. It turns radlang’s main thread into the message loop: JavaScript calls arrive from WebView, are queued, and run one at a time on that thread.
    • Each callback runs in an arena checkpoint. Its temporaries are reclaimed once the reply has been copied out, so long-running apps stay flat.
    • Bindings pin their captures for the app’s lifetime.
    • win.dispatch works from any thread, including parallel workers.
  • Bundled files. paths.static (default public/) is packaged as APK assets and served to the WebView from https://appassets.androidplatform.net/. That is a real origin, so fetch, modules and storage work. win.loadFile("public/index.html") resolves there, then falls back to the working directory. The window.rad bridge is injected at the top of every served page, before the page’s own scripts.
  • Storage. The working directory is the app’s private files directory, so relative SQLite and fs paths persist there. HOME, TMPDIR and SQLITE_TMPDIR point at the app’s directories. Bundled assets are also extracted (once per install or update) to a directory that __rad_gui_resource_path and server.useStatic() read. .radenv is never packaged.
  • Output. stdout and stderr go to logcat under project.name (stderr at warning level).
  • Back and lifecycle. The back gesture goes to the page’s rad.onBack handlers, then the program’s win.onBack, then walks the WebView history, then moves the app to the background; it does not finish it, because the program keeps running (see gui.md). Rotation and other configuration changes keep the same activity. The system bars take the page’s theme-color (or body background), and content is kept clear of system bars, cutouts and the keyboard.

Differences from desktop platforms

  • sys.platform() returns "android" and sys.arch() returns "arm64".
  • HTTP client. It runs over Android’s HttpURLConnection with the same API and libcurl’s error codes and messages. Plain http:// needs build.android.cleartextTraffic: true; see http.md for the other differences.
  • HTTP server. It works and always runs in-process: project.cores worker processes are not used. Sockets add the INTERNET permission automatically.
  • SQLite, BearSSL TLS, parallel .map(cb, n) and async all work.
  • sys.cli, spawn, exec and start need Android 9 (API 28) or later; on older devices they throw a catchable error. Android apps can run only a few system tools.
  • Process diagnostics.
    • sys.loadavg needs API 29 and may be denied by the app sandbox; it returns -1 when unavailable.
    • Uncaught-exception native backtraces need API 33.
    • Fatal signals print radlang’s diagnostic, then hand off to Android’s crash reporter, so a tombstone is still written.
  • Memory reservations follow the iOS layout (2 GiB managed heap, smaller per-thread arenas): see ios.md. build.android.heapReserveMB sets the heap reservation.
  • Thread-locals use emulated TLS. This is required for a library built for API 26 and loaded with dlopen.
  • sys.cores() counts the online CPUs, as adb shell nproc does. An emulator has as many as its AVD’s hw.cpu.ncore (1 for some device profiles; emulator -cores N overrides it).
  • Rejected at build time:
    • gpu.* (needs Metal; gpu.available() is allowed and returns false);
    • --mlir, --notarize and --output.

Native syn apps

A program that opens a syn.window (or uses server.useSyn()) is linked with the portable syn renderer (compiler/runtime/syn_portable.c, OpenGL ES 3 through sokol, with -landroid -lEGL -lGLESv3) and launched by dev.radlang.app.RadNativeActivity: Android’s NativeActivity plus a text input view, from the same classes.dex as the WebView shell. main runs on its own thread and becomes the render and event loop at syn.run.

  • Text input. Input methods are attached to the focused input, textarea or number through a real InputConnection, so suggestions, autocorrect, gesture typing, voice input and accented or CJK characters arrive as whole strings, and composing text is shown underlined until it is committed. Hardware keys and the keys an input method sends still arrive as key events.
  • Back. It closes syn’s own menu, dropdown or focus, then asks the app’s onBack handlers, then leaves the app (see syn.md). The activity opts out of predictive back, so Android delivers every Back press to the app.
  • Lifecycle. Rotation and other configuration changes keep the activity and resize the window; the app resumes drawing after it was in the background. Leaving the app ends the process.