Docs and example on how to use a android build binary in a android app

Hei I have my spm package build with the GitHub - finagolfin/swift-android-sdk: Android SDKs for Swift but lack some experience to integrate it in an android app. Does anybody know of docs for that?

So I have done the steps and created a small spm package that build using

swift build --swift-sdk aarch64-unknown-linux-android24 --toolchain /Library/Developer/Toolchains/swift-6.0.3-RELEASE.xctoolchain/ wich is awsome.

Then when I explore what I have done I can find

.build/aarch64-unknown-linux-android24/debug/swift_android_contract.build

contains

master.priors
output-file-map.json
sources
swift_android_contract.d
swift_android_contract.emit-module.d
swift_android_contract.swift.o
swift_android_contract.swiftdeps
swift_android_contract.swiftdeps~
swift_android_contract.swiftmodule.o
swift_android_contractPackageTests.autolink

But then I think I should use the .o file now on android and configure it in gradle. The scade instructions mentioned by @Finagolfin seam to suggest to make it a jar. I know people have done it just lack a good starting point :) Thanks!

Should I create .so files of an .o file to continue?

No, I haven't seen any docs using my Android SDK bundle, and I have not tried that myself with Swift yet. Invoking native code in an Android app is a fairly straightforward process though of building native shared libraries for each of the architectures you want to support, then calling those methods through JNI. Take a look at this SCADE tutorial I just found through Bing, which gives you an idea of how it's usually done.

You will want to try the official swift-java JavaKit support that was recently released instead of their library.

@Marc_Prud_hommeaux, do you have any plans with your Skip toolchain to enable uses like this also, ie those who simply want to cross-compile some Swift and use it in their existing app? May be a usecase you want to support also, with some doc or code, not sure.

1 Like

Yes using skip not to build a whole app but a library into an existing app is what we want.

Skip does support this! While Skip's primary use-case is managing the entire Android application with a mixture of transpiled and natively-compiled Swift, our native toolchain support also enables you to build and package (as an aar file) a separate Android library that you can drop into your existing Android project. It handles exposing your Swift interfaces to Kotlin, as well as provides support for calling back out to Java/Kotlin from the Swift code.

For an overview and walkthrough of this process, check out the Sharing a Swift Model Layer blog post, and further reference the bridging documentation. Questions and feedback welcome, either here or on our Slack!

2 Likes

I have created my own Swift toolchain for Android:
aarch64-android28-swift

In my case, I use this toolchain with the Android NDK to build an ".so" library, and utilize the JNI interface for communication with the Java layer.

I believe there should be a better way to communicate with Java, such as using "Djinni" or other similar tools.

1 Like