Swift SDK for Android プレビューを動かしてみる
Swift SDK for Androidのプレビューリリース版が2025年10月27日に公開されました。
さっそく動かしてみたので、以下手順をメモしておきます。
動作環境
Apple Swift version 6.3-dev (LLVM 0d0246569621d5b, Swift 199240b3fe97eda)
Target: arm64-apple-macosx15.0
macOS Sequoia 15.6.1(24G90)
Android Studio Narwhal 4 Feature Drop | 2025.1.4
1. ホストツールチェーンをインストールする
swiftly コマンドでホストツールチェーンをインストールします。(まだ swiftly をインストールしていない方は、Install Swift - macOS | Swift.orgからインストールしておきましょう)
注意すべき点は、クロスコンパイル版Swift SDKを使用するには、ホストツールチェーンとSwift SDKのバージョンが完全に一致している必要があるということです。そのため、特定のSwift SDKバージョンに対応するホストツールチェーンのバージョンをインストールする必要があります。
$ swiftly install main-snapshot-2025-10-16
$ swiftly use main-snapshot-2025-10-16
$ swiftly run swift --version
Apple Swift version 6.3-dev (LLVM 0d0246569621d5b, Swift 199240b3fe97eda)
Target: arm64-apple-macosx15.0
2. Swift SDK for Androidをインストールする
次に、swift sdk コマンドを使用してSwift SDKバンドルをインストールします。
$ swift sdk install https://download.swift.org/development/android-sdk/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a_android-0.1.artifactbundle.tar.gz --checksum 451844c232cf1fa02c52431084ed3dc27a42d103635c6fa71bae8d66adba2500
$ swiftly run swift sdk list
swift-DEVELOPMENT-SNAPSHOT-2025-10-16-a-android-0.1
3. 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.
とあるので、バージョン27dをインストールしました。
※NDKをすでに別の場所にインストールしている場合は、ANDROID_NDK_HOME 環境変数をその場所に設定して setup-android-sdk.sh スクリプトを実行するだけで済むそうです。
$ 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
NDKをダウンロードして展開したら、Swift SDKバンドルに含まれている setup-android-sdk.sh スクリプトを実行して、Swift SDK for Androidにリンクする必要があります。
cd ~/Library/org.swift.swiftpm || cd ~/.swiftpm
./swift-sdks/swift-DEVELOPMENT-SNAPSHOT-2025-10-16-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
この時点で、Android用のクロスコンパイルツールチェーンが完成しました 🎉
サンプルアプリをビルドしてみる
Swift SDK for Androidを活用したAndroidアプリのサンプル集が https://github.com/swiftlang/swift-android-examples リポジトリに用意されています。この中の hello-swift をビルドしてみます。
必須要件として
-
Java Development Kit (JDK): We recommend using JDK 25. Ensure the
JAVA_HOMEenvironment variable is set to your JDK installation path.
と書かれているので、JDK 25をインストールしていない方はセットアップしてPATHを通しておきましょう。
手順
- Android Studioを起動して
swift-android-examplesを開く(hello-swiftを開かないように!) - Gradleターゲットを
hello-swiftにセット - Android emulator or 実機でRun

無事アプリが起動しました 🎉🎉🎉
ソースコードを読んでみる
せっかくなので、ソースコードを少しだけ読んでみます。
Swiftのソースコードはこちら
import Android
@_cdecl("Java_org_example_helloswift_MainActivity_stringFromSwift")
public func MainActivity_stringFromSwift(env: UnsafeMutablePointer<JNIEnv?>, clazz: jclass) -> jstring {
let hello = ["Hello", "from", "Swift", "❤️"].joined(separator: " ")
return hello.withCString { ptr in
env.pointee!.pointee.NewStringUTF(env, ptr)!
}
}
JNI の命名規則に従って @_cdecl 属性で関数を実装し、hello の内容を返しています。
次に、Androidアプリ側のソースコードを読んでみましょう。
package org.example.helloswift
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<TextView>(R.id.sample_text).text = stringFromSwift()
}
/**
* A native method that is implemented by the 'helloswift' native library,
* which is packaged with this application.
*/
external fun stringFromSwift(): String
companion object {
// Used to load the 'helloswift' library on application startup.
init {
System.loadLibrary("helloswift")
}
}
}
Kotlinで書かれたシンプルなアクティビティで、外部関数 stringFromSwift() を宣言してネイティブライブラリをロードしていました。
この他にも様々なサンプルソースが公開されているので、興味のある方は試してみると良いでしょう。
(2025.10.28追記:サンプルコードがさっそく更新されたようで、下記リストはもう新しいものに置き換えられていました!)
- hello-swift - basic Swift integration that calls a Swift function.
- hello-swift-callback - demonstrates bidirectional communication with Swift timer callbacks updating Android UI.
- hello-swift-library - shows how to package Swift code as a reusable Android library component.
- native-activity - complete native Android activity with OpenGL ES rendering written entirely in Swift.
- swift-java-hashing-example - application that demonstrates how to call Swift code from an Android app with automatically generated Java wrappers and JNI code using swift-java.
ハマったところ
Task 'wrapper' not found in project ':hello-swift'というエラー
Android Studioで直接 hello-swift ディレクトリを開いていたのが原因。Gradle wrapper ファイルがある swift-android-examples を開きましょう。
Caused by: java.lang.ClassNotFoundException: org.gradle.util.VersionNumber
これは色々いじっていたら直った(適当ですみません)。Android StudioやAPIのバージョンが古かったのが原因かもしれません。
JDKが古い OR そもそも見つからない
次の手順を試してください。
- Android Studio を開く
- メニュー → Settings(または Preferences)
- 左メニューからBuild, Execution, Deployment → Build Tools → Gradle
- 「Gradle JDK」を JDK 25 に変更
Discussion