Skip to content

swift_android_library

Swift shared library for Android.

Description

Compiles Swift sources with swiftc into an Android .so and emits an android_native_library provider. The Swift standard library is linked statically. Once snapshots the matching C++ shared runtime from the Swift Software Development Kit and android_binary packages both libraries under the architecture-specific directory in the Android application package.

Attributes

AttributeTypeRequiredDefaultDescription
android_abistringnoinferredAndroid ABI directory such as arm64-v8a, armeabi-v7a, x86, or x86_64. Inferred from common Android target triples when omitted
android_apiintno28Android API level appended to API-less Android target triples
targetstringnoinferredSwift target triple. Inferred from android_abi and android_api when omitted, or set directly when the ABI is inferable
module_namestringnotarget nameSwift module name
package_namestringnoemptySwift package name passed through -package-name when set
sdkstringnoOptional sysroot passed to swiftc -sdk
resource_dirstringnoOptional Swift resource directory passed to swiftc -resource-dir
cxx_runtimestringnoSwift Software Development Kit sysrootOptional absolute path to the C++ shared runtime packaged with the Swift library
swift_sdkstringnofirst Android SDKInstalled Swift SDK identifier used to discover default Android sysroot and Swift resource paths
android_ndkstringnoANDROID_NDK_HOMEAndroid NDK root used to find the LLVM tool directory
tools_directorystringnoinferredDirectory containing Android clang and linker tools passed as -tools-directory
swiftcstringnoPATHOverride Swift compiler path
swift_flagslist<string>no[]Additional Swift compiler flags
coptslist<string>no[]Bazel-compatible alias for additional Swift compiler flags
defineslist<string>no[]Conditional compilation symbols lowered to -D flags and propagated to downstream Swift modules
linkoptslist<string>no[]Additional linker flags appended after dependency linker flags and propagated downstream
datalist<string>no[]Runtime data file globs propagated to downstream consumers
swiftc_inputslist<string>no[]Bazel-compatible extra Swift compiler input globs
library_evolutionboolnofalseEnable Swift library evolution and emit a textual module interface

Accepted but unsupported attributes: always_include_developer_search_paths, alwayslink, generated_header_name, generates_header, linkstatic, plugins, and private_deps. Non-empty values fail validation.

Dependency Edges

EdgeAcceptsDescription
depsswift_module, android_native_librarySwift modules and Android native libraries linked or packaged with this library

Providers

The target emits swift_module, android_native_library, and native_linkable.

Runtime Boundary

Once follows the official Swift Software Development Kit for Android packaging model: the Swift standard library is linked statically, while libc++_shared.so is snapshotted from the selected toolchain, content-verified, cached, and emitted as a native dependency. The complete runtime closure therefore reaches the Android application package without a shell staging command.

Kotlin or Java callers still need a Java Native Interface boundary. The runnable shared-code starter includes direct entry points for its primitive example. Richer application programming interfaces should use the official SwiftJava generator, whose Java Native Interface mode supports Android.

Capabilities

CapabilityOutput groups
builddefault, native_library, swiftmodule

Source References

Target kind discovery exposes the official Swift Android Software Development Kit guide for runtime packaging and the SwiftJava project for generated Java Native Interface bindings. A coding harness can fetch either source through Once before adapting a richer interoperation boundary.

Provider Record

FieldTypeMeaning
label_idstringCanonical target id
module_namestringSwift module name
targetstringSwift target triple passed to swiftc
android_abistringAPK ABI directory
swiftmodule_dirstringDirectory holding the generated Swift module
swiftmodulestringGenerated Swift module artifact
swiftdocstringGenerated Swift documentation artifact
swiftinterfacestringGenerated textual module interface when library evolution is enabled
dylibstringAndroid shared library output
android_native_librarieslist<record>Direct native libraries with abi and path fields
transitive_android_native_librarieslist<record>Direct and dependency native libraries for APK packaging
transitive_swiftmodule_dirslist<string>Swift module search paths for downstream Swift Android libraries
transitive_swiftmodule_inputslist<string>Swift module artifacts declared as downstream compiler inputs
transitive_swift_defineslist<string>Conditional compilation symbols propagated downstream
transitive_linkoptslist<string>Linker options propagated downstream
transitive_datalist<string>Runtime data propagated downstream

Outputs

OutputLocation
Shared library.once/out/<target>/lib<module_name>.so
C++ shared runtime.once/out/<target>/libc++_shared.so
Swift module.once/out/<target>/<module_name>.swiftmodule
Swift doc.once/out/<target>/<module_name>.swiftdoc
Swift interface.once/out/<target>/<module_name>.swiftinterface when library evolution is enabled

Example

toml
[[target]]
name = "SharedSwift"
kind = "swift_android_library"
srcs = ["Sources/**/*.swift"]

[target.attrs]
android_abi = "arm64-v8a"
module_name = "SharedSwift"
swift_sdk = "swift-6.3.2-RELEASE_android"
android_ndk = "/opt/android-ndk"
swift_flags = ["-O"]

Released under an open-source license.