beetlebug

Develop for Android

This page is the loop of a developer of an Android app: try the script on the computer, run it on a phone or the emulator, see its log, and make an APK for other people.

Try the script on the computer

The same script runs in a window of the computer:

zig-out/bin/ztk main.tcl

tk touchscreen gives 0 on a desktop and 1 on a phone, so a script can choose a layout. The debug tools of Develop on Linux and the tests with --headless work on the computer.

Run a script on a phone or the emulator

ztk --android bundles the script for the device, installs the app, starts it, and prints the log of the app until the app ends:

zig-out/bin/ztk --android main.tcl images/
ztk: org.ztk.main runs on Android 16 (x86_64); its log follows. Ctrl-C stops the log, and the app goes on.
renderer vulkan: llvmpipe (LLVM 21.1.8, 256 bits)
ztk: org.ztk.main ended
Option or variable What it does
--device SERIAL Selects the device. With no --device: ANDROID_SERIAL, else the one device of adb devices.
ADB The path of adb. Else platform-tools/adb of ANDROID_HOME or ANDROID_SDK_ROOT, else the PATH.
The words after the script Files and folders of the app, as for ztk bundle.
--allow, --tcl Add capabilities to file, channels, and clock, and select the Tcl release.

The target comes from the ABI of the device (arm64-v8a or x86_64), and its runtime must be next to the program. With no device, or with a phone that shows the prompt of USB debugging, the command prints the cause and ends with the status 1. The app stays on the device, and adb uninstall org.ztk.main removes it.

See the log

puts and the errors of the script go to the log of the system with the tag ztk. ztk --android shows it. For an app that you installed with adb install:

adb logcat -s ztk

Make the APK

zig-out/bin/ztk bundle -o Hello.apk --target aarch64-linux-android \
    --id org.example.hello main.tcl images/
Option What it does
-o Name.apk Writes an APK.
--target T aarch64-linux-android for a phone or a tablet, and x86_64-linux-android for the emulator on an x86-64 computer. An APK has the runtime of one target.
--id ID The package name. The default is org.ztk. and the letters of the name.
--name NAME The label under the icon.
--icon FILE.png The icon of the app, from a square PNG of 192 pixels or more: the legacy icon of each density, and the adaptive icon of Android 8 and later, with the image at 72 dp of the layer of 108 dp on white. The launcher gives it the shape of the device, such as a circle, so the corners of the image can go. With no --icon, the app has the default icon of Android.
--version V The version name. The default is 1.0.
--version-code N The version code. The default is 1.
--allow CAPS The capabilities. The default is file,channels,clock.
--sign-cert FILE Signs the APK with your certificate: PEM, the certificate of the signer first, then its chain.
--sign-key FILE The private key of that certificate: PEM with no password, a P-256, P-384, or RSA key.

Sign the APK

With no --sign-cert, ztk bundle signs the APK with the debug key of this computer. It makes the key at the first use:

Host The debug key
Windows %APPDATA%\ztk\android-debug.pem
macOS and Linux ztk/android-debug.pem in XDG_CONFIG_HOME, or in ~/.config

An APK with the debug key is debuggable. Sign an APK that you give to other people with your own key.

Install the APK

adb install Hello.apk

A person can also install the APK from a file, after they allow apps from that source in the settings of the phone.

The next page, Widgets, shows the widgets on an Android phone.