Skip to content

android_binary

Android APK target.

Description

Builds an Android APK from Java and Kotlin sources, Android resources, native shared libraries, android_resource deps, and android_library deps. The target kind links resources with aapt2, compiles Java sources with javac, compiles Kotlin sources with kotlinc, dexes runtime jars with d8, packages dex and native libraries into the APK, zipaligns it, and signs it with a debug key by default.

Attributes

AttributeTypeRequiredDefaultDescription
application_idstringyesAndroid application id
manifeststringnoAndroidManifest.xmlPackage-relative Android manifest
resource_fileslist<string>nofiles under resAndroid resource file glob patterns
resource_dirslist<string>no["res"]Resource roots passed to aapt2 compile
assetslist<string>no[]Android asset file glob patterns
asset_dirslist<string>no["assets"]Asset roots packaged into the APK
assets_dirstringnoSingle package-relative asset root alias
namespacestringnoapplication_idJava package for generated R classes
custom_packagestringnoAlias for generated R package
compile_sdkintnohighest installedAndroid SDK API level used for android.jar
min_sdk_versionintno23Minimum Android API level
target_sdk_versionintnocompile_sdkTarget Android API level
version_codeintno1APK versionCode passed to aapt2
version_namestringno1.0APK versionName passed to aapt2
signingstringnodebugdebug for debug signing or none for unsigned output
debug_keystorestringnolocal debug keystoreOptional package-relative debug keystore
debug_keystore_passwordstringnoandroidFixed public debug signing password
debug_key_aliasstringnoandroiddebugkeyKey alias for debug signing only
adbstringnoSDK platform-toolsOverride adb path for run
adb_serialstringnoDevice serial passed to adb -s
emulatorstringnoAndroid software development kit emulatorOverride Android emulator path used by visible runs
emulator_devicestringnoAndroid Virtual Device name started by once run --visible
launch_activitystringnolauncher intentActivity component launched by once run
instrumentstargetnoApplication target this package instruments when used by android_instrumentation_test
build_tools_versionstringnohighest installedAndroid SDK build-tools version
android_sdkstringnoenvAndroid SDK root, otherwise ANDROID_HOME or ANDROID_SDK_ROOT
java_language_levelstringno17Java source and target level passed to javac
javac_optslist<string>no[]Additional javac flags
javacoptslist<string>no[]Bazel-compatible alias for additional javac flags
kotlinc_optslist<string>no[]Additional kotlinc flags
dexoptslist<string>no[]Additional d8 flags

Tool override attrs are also available for javac, jar, java, java_home, kotlinc, kotlin_home, kotlin_stdlib, aapt2, d8, apksigner, zipalign, and adb.

Dependency Edges

EdgeAcceptsDescription
depsandroid_library, android_resource, android_native_libraryAndroid libraries, resources, and native shared libraries packaged into the APK

Providers

The target emits android_application and android_apk.

Capabilities

CapabilityOutput groupsRequires
builddefault, apk, dex, resources
rundefaultapk

Source References

Target kind discovery returns these external concepts for partial graph adoption:

Use them to inspect the source target that owns final application packaging, then reproduce only its required dependency closure.

Outputs

OutputLocation
APK.once/out/<target>/<name>.apk
Debug keystore.once/out/<target>/debug.keystore when debug_keystore is set
Unsigned APK.once/out/<target>/unsigned.apk
Dex directory.once/out/<target>/dex
Linked resource package.once/out/<target>/resources.apk

Native library deps are copied into the unsigned APK under lib/<abi>/<library>. Providers such as swift_android_library and rust_mobile_library emit the required android_native_libraries records.

Signing

signing = "debug" stages and signs with debug_keystore when it is set. When it is omitted, Once signs with ANDROID_DEBUG_KEYSTORE or ~/.android/debug.keystore in place. Once does not ship debug private key material. The keystore SHA-256 is part of the signing action identity so changing the local key invalidates cached signing output.

debug_keystore_password must stay android. Custom signing passwords are not supported because action metadata and process arguments are not treated as secret channels. signing = "none" leaves the APK unsigned after zipalign. Production signing is not implemented yet.

Running

once run first builds the APK because the run capability requires the apk output group. The run action then executes direct adb commands: wait for a device, install the APK with adb install -r -d, and launch the app.

Pass once run --visible to request a visible runtime interface. When emulator_device is set, Once starts that Android Virtual Device with the Android emulator before waiting for a device. If emulator_device is empty, visible runs use the already-connected physical device or emulator.

When launch_activity is empty, Once resolves the launcher activity on the device with cmd package resolve-activity --brief, then starts the resolved component with am start -n. When launch_activity is set, Once launches it directly with adb shell am start -n <component>. Components may be written as package/.Activity, package/FullyQualifiedActivity, or just an activity name, which Once pairs with application_id.

Set adb_serial when more than one Android device or emulator is connected.

Instrumentation

Set instruments on an android_binary test package to identify the app under test. Then declare an android_instrumentation_test target that depends on both the app and the test package. The test target installs both packages on a device or emulator and runs am instrument.

Limitations

The target supports Java sources, Kotlin sources, resources, native shared library packaging, debug signing, Android resource dependencies, Android library dependencies, application package installation, and application launch. Data binding, manifest placeholder expansion, native splits, shrinking, resource filtering, density filtering, no-compress packaging, and startup profile packaging are not implemented yet. Non-empty values for unsupported attributes fail validation instead of being ignored.

Use android_local_test for host-side Java and Kotlin tests.

Example

toml
[[target]]
name = "HelloResources"
kind = "android_resource"

[target.attrs]
package = "dev.once.hello"
manifest = "ResourcesManifest.xml"
resource_files = ["res/**"]
min_sdk_version = 23

[[target]]
name = "Greeting"
kind = "android_library"
srcs = ["src/**/*.java"]
deps = ["./HelloResources"]

[target.attrs]
namespace = "dev.once.greeting"
manifest = "LibraryManifest.xml"
resource_files = []
min_sdk_version = 23

[[target]]
name = "Hello"
kind = "android_binary"
srcs = ["src/**/*.kt"]
deps = ["./Greeting"]

[target.attrs]
application_id = "dev.once.hello"
manifest = "AndroidManifest.xml"
resource_files = []
min_sdk_version = 23
version_code = 1
version_name = "1.0"

Released under an open-source license.