- 
                Notifications
    You must be signed in to change notification settings 
- Fork 253
Add a Getting Started document for the Swift Android SDK #985
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 13 commits
      Commits
    
    
            Show all changes
          
          
            28 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a56b44a
              
                Add article: Getting Started with the Swift Android SDK
              
              
                marcprux f8d1810
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux 6a53050
              
                Merge branch 'main' into android-docs
              
              
                marcprux f1943a2
              
                Merge branch 'main' into android-docs
              
              
                marcprux 7a81b4d
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux 00e2fe7
              
                Update instructions for Swift 6.2 SDK, ndk 27d, and add some wording …
              
              
                marcprux 74eced9
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux 915477b
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux d404a53
              
                Update documentation/articles/swift-android-getting-started.md
              
              
                marcprux 7fdc0be
              
                Update post with recommended changes
              
              
                marcprux 221221a
              
                Update examples to use swift-DEVELOPMENT-SNAPSHOT-2025-10-02-a-androi…
              
              
                marcprux e73a03f
              
                Fix swiftly link
              
              
                marcprux 9b69eed
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux c76ef1b
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux 0dc655c
              
                Update Android install instructions to point to swift-DEVELOPMENT-SNA…
              
              
                marcprux 09cdb07
              
                Update documentation/articles/swift-android-getting-started.md
              
              
                marcprux fe86eaa
              
                Update documentation/articles/swift-android-getting-started.md
              
              
                marcprux 35cba99
              
                Update documentation/articles/swift-android-getting-started.md
              
              
                marcprux 7b4e13e
              
                Update documentation/articles/swift-android-getting-started.md
              
              
                marcprux a07a5cd
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux 2b540bd
              
                Update documentation/articles/swift-android-getting-started.md
              
              
                marcprux eaf4f40
              
                Update _data/documentation.yaml
              
              
                marcprux dd3722e
              
                Rename swift-android-getting-started.md to swift-sdk-for-android-gett…
              
              
                marcprux 2becba8
              
                Merge branch 'swiftlang:main' into android-docs
              
              
                marcprux 8cc07e3
              
                Update _data/documentation.yaml title
              
              
                finagolfin 165df18
              
                Update documentation/articles/swift-sdk-for-android-getting-started.m…
              
              
                finagolfin 6e994f2
              
                Update documentation/articles/swift-sdk-for-android-getting-started.m…
              
              
                finagolfin 5ddf3a8
              
                Update documentation/articles/swift-sdk-for-android-getting-started.md
              
              
                shahmishal File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -510,6 +510,13 @@ parispittman: | |
| name: Paris Pittman | ||
| github: parispittman | ||
|  | ||
| marcprux: | ||
| name: Marc Prud'hommeaux | ||
| email: [email protected] | ||
| gravatar: marcprux | ||
| github: marcprux | ||
| about: "Marc Prud'hommeaux works on bringing Swift application development to Android at Skip.tools." | ||
|  | ||
| maxdesiatov: | ||
| name: Max Desiatov | ||
| github: maxdesiatov | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
        
          
          
            158 changes: 158 additions & 0 deletions
          
          158 
        
  documentation/articles/swift-android-getting-started.md
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| --- | ||
| layout: page | ||
| date: 2025-06-01 12:00:00 | ||
| title: Getting Started with the Swift SDK for Android | ||
| author: [marcprux] | ||
| --- | ||
|  | ||
| Since it was first open-sourced in 2015, Swift has grown from a language focused on creating apps for Darwin-based systems (iOS, macOS, etc.) into a cross-platform development language supporting Linux, Windows, and various embedded systems. With the release of the Swift SDK for Android, it is now possible to use Swift for Android application development as well. | ||
|  | ||
| ### Getting Started | ||
|  | ||
| Building a Swift package for Android involves installing and configuring a cross-compilation SDK. Cross-compilation is the process of building code on one platform, the host, to run on a different platform, the target. In the context of Swift for Android, this typically involves compiling Swift code on a host macOS or Linux machine to produce executables or libraries for the target Android OS. This differs from compiling Swift for the host platform, where the host and target are the same (for example, compiling and running Swift code on macOS for macOS). | ||
|  | ||
| To cross-compile Swift code for Android, you need three separate components: | ||
|  | ||
| 1. **The Host Toolchain**: this is the `swift` command and related tools that you will use to build and run your Swift code. | ||
| 2. **The Swift SDK for Android**: the set of libraries, headers, and other resources needed to generate and run Swift code for the Android target. | ||
| 3. **The Android NDK**: the "Native Development Kit" for Android includes the cross-compilation tools like `clang` and `ld` that are used by the host toolchain to cross-compile and link. | ||
|  | ||
|  | ||
| #### 1. Install the Host Toolchain | ||
|  | ||
| The first thing to note is that while `swift` may already be installed on your system (such as through an Xcode installation on macOS), using a cross-compilation SDK requires that the host toolchain and the SDK versions match exactly. For this reason, you will need to install the specific version of the host toolchain for the given SDK version. | ||
|         
                  marcprux marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| The easiest and recommended way to manage host toolchains on macOS and Linux is to use [the swiftly command](https://www.swift.org/swiftly/documentation/swiftly/getting-started) command. Once that has been setup, you can install the host toolchain with: | ||
|         
                  marcprux marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ```console | ||
| $ swiftly install main-snapshot-2025-10-02 | ||
| Installing Swift main-snapshot-2025-10-02 | ||
| Installing package in user home directory... | ||
| main-snapshot-2025-10-02 installed successfully! | ||
|  | ||
| $ swiftly use main-snapshot-2025-10-02 | ||
| The global default toolchain has been set to `main-snapshot-2025-10-02` (was 6.2.0) | ||
|  | ||
| $ swiftly run swift --version | ||
| Apple Swift version 6.3-dev (LLVM 0d0246569621d5b, Swift 199240b3fe97eda) | ||
| Target: arm64-apple-macosx15.0 | ||
| ``` | ||
|  | ||
| #### 2. Install the Swift SDK for Android | ||
|  | ||
| Next, download and install the SDK bundle using the built-in `swift sdk` command: | ||
|         
                  marcprux marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ```console | ||
| $ swift sdk install https://github.com/swift-android-sdk/swift-android-sdk/releases/download/DEVELOPMENT-SNAPSHOT-2025-10-02-a/swift-DEVELOPMENT-SNAPSHOT-2025-10-02-a-android-0.1.artifactbundle.tar.gz --checksum 46f01534412f4998f996a6231ad695ed67760cac59f82f86b48a2eae7eb8fd66 | ||
|  | ||
| Swift SDK bundle at `https://github.com/swift-android-sdk/swift-android-sdk/releases/download/DEVELOPMENT-SNAPSHOT-2025-10-02-a/swift-DEVELOPMENT-SNAPSHOT-2025-10-02-a-android-0.1.artifactbundle.tar.gz` successfully installed as swift-DEVELOPMENT-SNAPSHOT-2025-10-02-a-android-0.1.artifactbundle. | ||
| ``` | ||
|  | ||
| You should now see the Android SDK included in the `swift sdk list` command: | ||
|         
                  marcprux marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ```console | ||
| $ swiftly run swift sdk list | ||
| swift-DEVELOPMENT-SNAPSHOT-2025-10-02-a-android-0.1 | ||
| ``` | ||
|  | ||
| #### 3. Install and configure the Android NDK | ||
|  | ||
| The Swift SDK for Android depends on the Android NDK version 27d to provide the headers and tools necessary for cross-compiling to Android architectures. There are a variety of ways to [install the Android NDK](https://developer.android.com/ndk/guides), but the simplest is to just download and unzip the archive from the [NDK Downloads page](https://developer.android.com/ndk/downloads/#lts-downloads) directly. | ||
|  | ||
| You can automate this with the following commands in a directory of your choosing: | ||
|  | ||
| ```console | ||
| $ mkdir ~/android-ndk | ||
| $ cd ~/android-ndk | ||
| $ curl -fSLO https://dl.google.com/android/repository/android-ndk-r27d-$(uname -s).zip | ||
| $ unzip -q android-ndk-r27d-*.zip | ||
| $ export ANDROID_NDK_HOME=$PWD/android-ndk-r27d | ||
| ``` | ||
|  | ||
| Once you have downloaded and unpacked the NDK, you can link it up to the Swift SDK for Android by running the `setup-android-sdk.sh` utility script included with the SDK bundle: | ||
|         
                  marcprux marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| ```console | ||
| $ cd ~/Library/org.swift.swiftpm || cd ~/.swiftpm | ||
| $ ./swift-sdks/swift-DEVELOPMENT-SNAPSHOT-2025-10-02-a-android-0.1.artifactbundle/swift-android/scripts/setup-android-sdk.sh | ||
| setup-android-sdk.sh: success: ndk-sysroot linked to Android NDK at android-ndk-r27d/toolchains/llvm/prebuilt | ||
| ``` | ||
|  | ||
| *Note that if you have already installed the NDK in a different location, you can simply set the `ANDROID_NDK_HOME` environment variable to that location and run the `setup-android-sdk.sh` script.* | ||
|  | ||
| At this point, you will have a fully working cross-compilation toolchain for Android. | ||
|  | ||
| ### Hello World on Android | ||
|  | ||
| Now let's try it out with the canonical "Hello World" program. First, create a directory to hold your code: | ||
|  | ||
| ```console | ||
| $ cd /tmp | ||
| $ mkdir hello | ||
| $ cd hello | ||
| ``` | ||
|  | ||
| Next, ask Swift to create a new program package for you: | ||
|  | ||
| ```console | ||
| $ swiftly run swift package init --type executable | ||
| ``` | ||
|  | ||
| Try it out by building and running locally for the host machine: | ||
|  | ||
| ```console | ||
| $ swiftly run swift build | ||
| Building for debugging... | ||
| [8/8] Applying hello | ||
| Build complete! (15.29s) | ||
|  | ||
| $ .build/debug/hello | ||
| Hello, world! | ||
| ``` | ||
|  | ||
| With the Swift SDK for Android installed and configured, you can now cross-compile the executable to Android for the `x86_64` architecture: | ||
|  | ||
| ```console | ||
| $ swiftly run swift build --swift-sdk x86_64-unknown-linux-android28 --static-swift-stdlib | ||
| Building for debugging... | ||
| [8/8] Linking hello | ||
| Build complete! (2.04s) | ||
|  | ||
| $ file .build/x86_64-unknown-linux-android28/debug/hello | ||
| .build/x86_64-unknown-linux-android28/debug/hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, with debug_info, not stripped | ||
| ``` | ||
|  | ||
| or for the `aarch64` architecture: | ||
|  | ||
| ```console | ||
| $ swiftly run swift build --swift-sdk aarch64-unknown-linux-android28 --static-swift-stdlib | ||
| Building for debugging... | ||
| [8/8] Linking hello | ||
| Build complete! (2.04s) | ||
|  | ||
| $ file .build/aarch64-unknown-linux-android28/debug/hello | ||
| .build/aarch64-unknown-linux-android28/debug/hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, with debug_info, not stripped | ||
| ``` | ||
|  | ||
| With a connected Android device that has [USB debugging enabled](https://developer.android.com/studio/debug/dev-options#Enable-debugging) or a locally-running Android [emulator](https://developer.android.com/studio/run/emulator#get-started), you can now copy the executable over, along with the required `libc++_shared.so` dependency from the Android NDK, and run it with the [`adb`](https://developer.android.com/tools/adb) utility: | ||
|  | ||
| ```console | ||
| $ adb push .build/aarch64-unknown-linux-android28/debug/hello /data/local/tmp | ||
| .build/aarch64-unknown-linux-android28/debug/hello: 1 file pushed, 0 skipped. 155.9 MB/s (69559568 bytes in 0.425s) | ||
|  | ||
| $ adb push $ANDROID_NDK_HOME/toolchains/llvm/prebuilt/*/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so /data/local/tmp/ | ||
| aarch64-linux-android/libc++_shared.so: 1 file pushed, 0 skipped. 145.7 MB/s (1794776 bytes in 0.012s) | ||
|  | ||
| $ adb shell /data/local/tmp/hello | ||
| Hello, world! | ||
| ``` | ||
|  | ||
| ### Next Steps | ||
|  | ||
| Congratulations, you have built and run your first Swift program on Android! | ||
|  | ||
| Note that Android applications are typically not deployed as command-line executable tools. Rather, they are assembled into an `.apk` archive and launched as an app from the home screen. To support this, Swift modules can be built as shared object libraries for each supported architecture and included in the app archive. The Swift code can then be accessed from the Android app — which is typically written in Java or Kotlin — through the Java Native Interface ([JNI](https://developer.android.com/training/articles/perf-jni)). | ||
|         
                  marcprux marked this conversation as resolved.
              Show resolved
            Hide resolved         
                  marcprux marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| Visit the [swift-android-examples](https://github.com/swiftlang/swift-android-examples) repository to see a variety of projects that demonstrate how to build full Android applications that utilize the Swift SDK for Android. | ||
|  | ||
| These larger development topics will be expanded on in future articles and documentation. In the meantime, please visit the [Swift Android forums](https://forums.swift.org/c/platform/android/115) to discuss and seek help with the Swift SDK for Android. | ||
|  | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.