Run your Android app on a device or emulator¶
kivyforge run -p android builds your app, installs it on a device or an AVD
(Android Virtual Device, an emulator), and launches it. This page covers the
debug development loop and the on-device smoke test.
Before you begin¶
- Configure your Android app and lock it with
kivyforge lock -p android. - Have one of the following:
- A physical device with USB debugging enabled and this computer authorized
for
adb. See Set up a physical device. - An AVD, created in Android Studio's Device Manager or with
avdmanager. It does not need to be running.
- A physical device with USB debugging enabled and this computer authorized
for
You don't need an emulator to build or to run on a phone. The SDK's emulator package is needed only for AVDs.
Set up a physical device¶
- On the phone, open Settings > About phone and tap Build number seven times to turn on Developer options.
- Open Settings > System > Developer options and turn on USB debugging. The menu path varies by phone maker.
- Connect the phone with a USB cable and unlock it.
- When the phone asks Allow USB debugging?, tap Allow. Select Always allow from this computer to skip the prompt next time.
-
Check that
adbsees the phone.adbis in theplatform-toolsfolder of your Android SDK:adb devices
The phone's serial number should be listed with the state device. Any other
result means run can't use the phone yet:
adb devices shows |
Fix |
|---|---|
unauthorized |
Unlock the phone and accept the Allow USB debugging? prompt. If no prompt appears, unplug and reconnect the cable. |
offline |
Unplug and reconnect the cable. If that doesn't help, run adb kill-server and try again. |
no permissions (Linux) |
Add a udev rule for the phone and add your user to the plugdev group. See Android's Run apps on a hardware device. |
| Nothing | Check that USB debugging is still on: some phones turn it off when you revoke USB debugging authorizations. If the phone's USB notification says Charging only, switch it to File transfer. Try another cable or port, because some cables carry only power. On Windows, the phone may also need its maker's USB driver. |
Then run on the phone with --device, so that run never looks for an
emulator:
kivyforge run -p android --device
Run the app¶
kivyforge run -p android
run picks a target, builds a debug APK (Android Package) for that target's ABI
(application binary interface), installs it, and launches it. Debug builds use
the standard Android debug keystore, so you need no signing setup.
Without target flags, run picks the first match in this order:
- The only attached physical device. If several are attached,
runstops and asks for--serial. - A running emulator.
- The first AVD, which
runboots.
Choose a target¶
| To target | Add |
|---|---|
| A physical device only, never an emulator | --device |
| An emulator, even when a phone is attached | --emulator |
| A specific AVD to boot | --avd AVD_NAME |
| A specific attached device or emulator | --serial SERIAL |
Replace AVD_NAME with a name from your AVD list and SERIAL with a serial
number that adb devices shows, such as emulator-5554.
To list attached devices and available AVDs without running anything:
kivyforge run -p android --list-devices
The list adds a Not ready: line for each device adb lists but can't use,
with its state, such as Not ready: SERIAL (unauthorized). Without the SDK's
emulator package, it shows AVDs: unavailable instead of failing.
--device cannot be combined with --emulator or --avd. When no device is
attached and the SDK has no emulator package, run says so and suggests
--device.
Control the build¶
--abi arm64_v8aor--abi x86_64builds for that ABI instead of the one the target reports. The ABI must be listed in[tool.kivy.android].abis.--no-buildinstalls and launches the APK from the last build.--releasebuilds the release variant, sobyte_compileandstrip_sourceapply as they do forkivyforge package. It uses your release key if[tool.kivy.android.signing]is configured and usable, and the debug keystore otherwise.
To build a debug APK without installing it:
kivyforge build -p android --debug --abi arm64_v8a
The APK is written to
<app>-android/app/build/outputs/apk/debug/app-debug.apk, where <app> is your
[project].name.
Run the on-device smoke test¶
--smoke runs a generated instrumented test instead of launching your app. It
checks that the Python runtime, the extension-module loader, and the pyjnius
bridge work on the device:
kivyforge run -p android --smoke
Add --release to test the byte-compiled, stripped release variant. kivyforge's
own CI runs run --smoke --release on an x86_64 emulator.
The test runs only on the device run chose, so a phone and an emulator can
both be attached.
Verify¶
run prints the device it chose, launches the app, and then follows the app's
log as it runs: everything the app's own process writes, including Python
output, tracebacks and Android crash reports, and nothing from other apps. It
keeps following until the app exits or you press Ctrl+C. Ctrl+C stops the log,
not the app.
If the app never starts, run prints the whole device log since launch, where
the reason is, and fails.
For a script, add --no-follow: run then prints the app's log once, about 25
seconds after launch, and returns.
A passing smoke test ends with Contract smoke test PASSED.
Filter the app's log¶
The followed log includes everything the app's process writes, including Android's own messages, which can bury your output. kivyforge does not filter it, but standard tools can. Two facts make that easy:
- Python's output has its own logcat tags:
print()and Kivy's log go topython.stdoutandpython.stderr. Java'sSystem.out.printlngoes toSystem.out, andLog.d("MyTag", ...)uses the tag you pass. runwrites the app's log to stdout and its own[run]messages to stderr, so a filter on stdout keeps kivyforge's messages.
Filter as run follows the log¶
# Python only
kivyforge run -p android | Select-String ' [VDIWEF] python\.std'
# Java only; add your own Log tags to the pattern
kivyforge run -p android | Select-String ' [VDIWEF] System\.(out|err)'
# Python and Java
kivyforge run -p android | Select-String ' [VDIWEF] (python\.std|System\.(out|err))'
# Everything except one noisy tag
kivyforge run -p android | Select-String -NotMatch BLASTBufferQueue
# Python only
kivyforge run -p android | grep -E ' [VDIWEF] python\.std'
# Java only; add your own Log tags to the pattern
kivyforge run -p android | grep -E ' [VDIWEF] System\.(out|err)'
# Python and Java
kivyforge run -p android | grep -E ' [VDIWEF] (python\.std|System\.(out|err))'
# Everything except one noisy tag
kivyforge run -p android | grep -v BLASTBufferQueue
Use logcat's own filters¶
logcat filters by tag and level on the device. Launch with --no-follow, then
read the log with adb. Replace PACKAGE with your
[tool.kivy.android].package:
kivyforge run -p android --no-follow
adb logcat --pid=$(adb shell pidof -s PACKAGE) python.stdout:V python.stderr:V System.out:V "*:S"
kivyforge run -p android --no-follow
adb logcat --pid=$(adb shell pidof -s PACKAGE) python.stdout:V python.stderr:V System.out:V '*:S'
*:S silences every tag you don't list, so list only the tags you want. Leave
it out to see everything. This command stops when the app's process ends; run it
again after a restart.
Use Android Studio's Logcat window¶
Launch with kivyforge run -p android --no-follow, open View > Tool Windows >
Logcat in Android Studio, and type a query into its search field:
| To see | Query |
|---|---|
| Python only | package:PACKAGE tag:python |
| Java only | package:PACKAGE tag:System.out |
| Python and Java | package:PACKAGE tag:python tag:System.out |
| Everything except one noisy tag | package:PACKAGE -tag:BLASTBufferQueue |
| Crashes only | package:PACKAGE is:crash |
tag: matches tags that contain the text, so tag:python matches both Python
tags. Several tag: terms match any of them, and a leading - excludes. Add
tag:MyTag for your own Java tags, or level:WARN to hide anything less
severe. If you open the generated project with kivyforge open -p android,
package:mine replaces package:PACKAGE. Logcat keeps following across app
restarts. See Android's Logcat
documentation for the full
query syntax.
What's next¶
- Sign and publish to Google Play when you are ready to distribute.
- Handle mobile screen geometry.
- Diagnose a failing build.