Skip to content

examples: a runnable Android APK example (Kotlin → Swift JNI → Swift) - #4

Merged
AttilaTheFun merged 1 commit into
android-supportfrom
android-example-app
Jun 25, 2026
Merged

examples: a runnable Android APK example (Kotlin → Swift JNI → Swift)#4
AttilaTheFun merged 1 commit into
android-supportfrom
android-example-app

Conversation

@AttilaTheFun

@AttilaTheFun AttilaTheFun commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Makes the Android example actually runnable instead of building only the JNI
.so with a prose packaging recipe. It's now a real android_binary (built with
rules_android + rules_kotlin) that loads libSwiftJNI.so and shows
"Hello from Swift, Android!" on screen — verified on an emulator:

The example app running on an Android emulator, showing "Hello from Swift, Android!"

This is the application half of the chain end-to-end:
MainActivity.kt → NativeBridge.greetingFromSwift() (Kotlin, JNI) →
libSwiftJNI.so (swift_binary(linkshared), @_cdecl) → Greeter
(swift_library).

What's here

  • examples/cross_compilation/android_app/BUILD.bazel (new): android_binary +
    kt_android_library, the two cc_library .so wrappers,
    select_android_runtime_lib/transition_binary for libc++, and a
    build_test. Everything tagged manual (kept out of the //examples/...
    wildcard, like the rest of cross_compilation).
  • The READMEs are rewritten to point at the runnable target.
  • CI: the macos_cross_compilation task gains --config=android_example and the
    app_build_test.

The one rules_swift-specific detail

swift_binary(linkshared = True) exposes its .so via DefaultInfo, not
CcInfo, so it can't go straight into android_binary.deps. Wrapping it in a
cc_library (a prebuilt .so in srcs becomes a CcInfo dynamic library) lets
android_binary's per-ABI native split collect it into lib/arm64-v8a/.
libc++_shared.so goes in the same way via select_android_runtime_lib.

Dependency weight (the open question)

rules_android / rules_kotlin / rules_java / rules_jvm_external are all
dev_dependency = True (pinned Maven lock at //:rules_android_maven_install.json,
hermetic @androidsdk from hermetic_android_toolchains — no local SDK), so
consumers of rules_swift are unaffected. protobuf stays at 34 (no conflict).
The rules_android flags live behind a scoped --config=android_example so the
rest of the build is untouched.

Totally fine if this is too heavy to live in rules_swift — it can drop back to
the .so + recipe, or move to a standalone example repo. Opening it as a
separate commit on top of the Android PR so the delta is easy to see.

(Screenshot lives on the assets/android-example-screenshot branch, not in this PR's diff.)

…Swift)

The cross_compilation example previously built only the JNI .so and documented
(in prose) how a downstream module would package it into an APK. Replace that
recipe with a real, building android_binary that runs on a device/emulator and
shows the Swift greeting on screen — a far more compelling demonstration of
integrating Swift into an Android app.

It's packaged the real-world way, with rules_android (android_binary) and
rules_kotlin (kt_android_library). The one rules_swift-specific detail: the
swift_binary(linkshared) .so arrives via DefaultInfo (not CcInfo), so it's
wrapped in a cc_library to feed android_binary's per-ABI native split; libc++
is selected via select_android_runtime_lib the same way.

rules_android/rules_kotlin/rules_java/rules_jvm_external are all dev_dependency
deps (with a pinned Maven lock and the hermetic @AndroidSDK), so consumers of
rules_swift are unaffected. CI builds the APK via a build_test under
--config=android_example (scoped flags; the rest of the build is untouched).

Claude-Session: https://claude.ai/code/session_01SmG1kqA3qB4WsLGU2xavuJ
AttilaTheFun added a commit that referenced this pull request Jun 25, 2026
@AttilaTheFun
AttilaTheFun merged commit 8668382 into android-support Jun 25, 2026
1 check passed
@AttilaTheFun

Copy link
Copy Markdown
Owner Author

Auto-resolved as merged: the example commit is now folded into the base Android PR (bazelbuild#1818), whose description carries the emulator screenshot. Nothing further needed here.

@AttilaTheFun
AttilaTheFun deleted the android-example-app branch July 2, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant