-
Notifications
You must be signed in to change notification settings - Fork 38
Documentation support #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // swift-tools-version: 6.0 | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "swift-android-examples", | ||
| products: [ | ||
| .library(name: "SwiftAndroid", targets: ["SwiftAndroid"]) | ||
| ], | ||
| dependencies: [ | ||
| .package( | ||
| url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0") | ||
| ], | ||
| targets: [ | ||
| .target(name: "SwiftAndroid") | ||
| ]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Building for Android | ||
|
|
||
| Cross-compile Swift code and package it into Android applications | ||
|
|
||
| ## Overview | ||
|
|
||
| Swift on Android uses cross-compilation: you build on a macOS or Linux host, and the compiler produces native ARM or x86_64 binaries targeting Android. | ||
|
|
||
| ```shell | ||
| $ swift build --swift-sdk aarch64-unknown-linux-android28 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope to get this changed to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that WIP somewhere? That would be a great improvement, and will be relevant for all the SDKs going forward.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's in trunk now, 😄 trying to get it into 6.3 here, swiftlang/swift-package-manager#9998. |
||
| ``` | ||
|
|
||
| ## Conditional compilation | ||
|
|
||
| Use `#if os(Android)` to conditionalize code for Android: | ||
|
|
||
| ```swift | ||
| #if os(Android) | ||
| import Android | ||
| #elseif canImport(Darwin) | ||
| import Darwin | ||
| #endif | ||
| ``` | ||
|
|
||
| ## Foundation | ||
|
|
||
| Foundation networking types require a separate import on non-Apple platforms: | ||
|
|
||
| ```swift | ||
| import Foundation | ||
| #if canImport(FoundationNetworking) | ||
| import FoundationNetworking | ||
| #endif | ||
| ``` | ||
|
|
||
| ## JNI integration | ||
|
|
||
| Export Swift functions for JNI using `@_cdecl`: | ||
|
|
||
| ```swift | ||
| @_cdecl("Java_com_example_MyClass_hello") | ||
| func hello(env: UnsafeMutablePointer<JNIEnv>, thisObj: jobject) -> jstring { | ||
| return env.pointee.pointee.NewStringUTF(env, "Hello from Swift")! | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Swift on Android | ||
|
|
||
| Use Swift to build native libraries and applications for Android | ||
|
|
||
| @Metadata { | ||
| @TechnologyRoot | ||
| } | ||
|
|
||
| ## Topics | ||
|
|
||
| ### Getting Started | ||
|
|
||
| - <doc:GettingStarted> | ||
| - <doc:BuildingForAndroid> | ||
|
|
||
| ### Guides | ||
|
|
||
| - <doc:PortingPackages> | ||
| - <doc:Integration> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Getting Started | ||
|
|
||
| Install the Swift toolchain, Android NDK, and Swift SDK for Android | ||
|
|
||
| ## Overview | ||
|
|
||
| Development requires three components: | ||
|
|
||
| 1. **Swift Toolchain** -- install with [swiftly](https://github.com/swiftlang/swiftly): `swiftly install latest` | ||
| 2. **Android NDK** -- download from [developer.android.com](https://developer.android.com/ndk/downloads) (r27d+ recommended) | ||
| 3. **Swift SDK for Android** -- install with `swift sdk install` (see [swift.org](https://www.swift.org/documentation/articles/swift-sdk-for-android-getting-started.html)) | ||
|
|
||
| ## Hello World | ||
|
|
||
| ```shell | ||
| $ swift package init --type executable | ||
| $ swift build --swift-sdk aarch64-unknown-linux-android28 | ||
| $ adb push .build/aarch64-unknown-linux-android28/debug/HelloWorld /data/local/tmp/ | ||
| $ adb shell /data/local/tmp/HelloWorld | ||
| Hello, world! | ||
| ``` | ||
|
|
||
| ## Target triples | ||
|
|
||
| | Architecture | Triple | | ||
| |---|---| | ||
| | ARM64 | `aarch64-unknown-linux-android28` | | ||
| | ARM32 | `armv7-unknown-linux-androideabi28` | | ||
| | x86_64 | `x86_64-unknown-linux-android28` | | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 32bit x86 is also supported in the Android SDK bundled with the Windows installer
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of us use that SDK, which doesn't even have any install instructions yet. I have asked those behind it to write some up.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 32bit x86 Android is almost completely non-existent. I think we should avoid any mention of it, even if the Windows SDK happens to still support it. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Integration | ||
|
|
||
| Integrate Swift builds with Android tooling and frameworks | ||
|
|
||
| ## Gradle | ||
|
|
||
| Add a custom Gradle task to invoke `swift build` and copy the resulting `.so` into `jniLibs/`: | ||
|
|
||
| ```kotlin | ||
| tasks.register<Exec>("buildSwiftLibrary") { | ||
| workingDir = file("${rootDir}/swift") | ||
| commandLine("swift", "build", | ||
| "--swift-sdk", "aarch64-unknown-linux-android28", | ||
| "-c", "release", "--static-swift-stdlib") | ||
| } | ||
| ``` | ||
|
|
||
| ## Swift Package Manager | ||
|
|
||
| ```shell | ||
| $ swift build --swift-sdk aarch64-unknown-linux-android28 | ||
| ``` | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Swift SDK for Android -- Getting Started](https://www.swift.org/documentation/articles/swift-sdk-for-android-getting-started.html) | ||
| - [swift-java](https://github.com/swiftlang/swift-java) -- Java/Kotlin interop | ||
| - [swift-android-action](https://github.com/marketplace/actions/swift-android-action) -- GitHub Actions | ||
| - [Swift Forums](https://forums.swift.org/) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Porting Swift Packages to Android | ||
|
|
||
| Adapt existing Swift packages to build and run on Android | ||
|
|
||
| ## Overview | ||
|
|
||
| Many Swift packages build for Android with minimal changes. The typical process is: attempt a build, fix platform-specific code with conditional compilation, then test on a device. | ||
|
|
||
| ## Good candidates | ||
|
|
||
| - Business logic, algorithms, data structures | ||
| - Networking and API clients | ||
| - Parsers, formatters, encoders/decoders | ||
| - SQLite and other database access | ||
|
|
||
| ## Conditional imports | ||
|
|
||
| ```swift | ||
| #if canImport(Darwin) | ||
| import Darwin | ||
| #elseif canImport(Android) | ||
| import Android | ||
| #elseif canImport(Glibc) | ||
| import Glibc | ||
| #endif | ||
| ``` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| // This file is included so SwiftPM considers the target to be a Swift target. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or Windows