SoFunction
Updated on 2025-04-09

Getting started with Android apps written in Swift

The Swift standard library can compile the kernel of Android armv7, which allows Swift statement code to be executed on Android mobile devices. This article explains how to run a simple "hello, world" program on your Android phone.

FAQ

Let's answer the following frequently asked questions:

Do you think I can quickly develop Android applications with Swift?

Dreaming, although the Swift compiler is capable of compiling Swift code on Android devices and running. What this requires is not only writing an APP using the Swift standard library, but more importantly, you need some frameworks to build your application user interface. The above Swift standard library cannot provide.

On the other hand, a Java application interface can be called from Swift in theory, but unlike Objective-C, the Swift compiler has no effect on Swift-to-Java bridge.

Preparation knowledge

In order to use this guide smoothly, you need:

1. Can compile Swift source code Linux environment. stdlib can only be compiled into Android-available versions in Linux environments. Before trying to build for Android, make sure you can refer to the README of the Swift project to compile for Linux.

2. Android NDK, which is higher than or equal to 21 version, is available for download at the following link:

/ndk/downloads/.

3. An Android device that can be debugged remotely. We need to deploy the stdlib results to Android devices through remote debugging. You can remote debugging according to the following official direction guide:/devtools/docs/remote-debugging.

"Hello, world" on Android 1. Build Swift Android stdlib dependencies

You may have noticed that in order to build Swift stdlib in Linux, you need apt-get install libicu-dev iciu-devtools. Simply put, building Swift stdlib used on Android devices requires libiconv and libicu. However, you need the Android version of these libraries.

Build libiconv and libicu for Android devices:

1. Make sure you have curl, antoconf, antomake, libtook and git installed.

2. CloningSwiftAndroid/libiconv-libicu-androidproject. Execute the following command via the command line: git clone git@:SwiftAndroid/.

3. Execute which ndk-build on the command line. Make sure that ndk-build can display the executable path in the Android NDK you downloaded. If it cannot be displayed, you need to add the Android NDK directory to your PATH.

4. Enter the libiconv-libicu-android directory on the command line and execute .

5. Make sure that the build script builds the armeabi-v7a/icu/source/i18n and armeabi-v7a/icu/source/common directories in your libiconv-libicu-android directory.

2. Build Switf stdlib for Android

Enter your Swift directory, then run the build script, passing the path to the Android NDK and libicu/libiconv directory:

$utils/build-script\-R\#BuildinReleaseAssertmode.--android\#BuildforAndroid.--android-ndk~/android-ndk-r10e\#PathtoanAndroidNDK.--android-ndk-version21\#TheNDKversiontouse.Mustbe21orgreater.--android-icu-uc~/libicu-android/armeabi-v7a/\--android-icu-uc-include~/libicu-android/armeabi-v7a/icu/source/common\--android-icu-i18n~/libicu-android/armeabi-v7a/\--android-icu-i18n-include~/libicu-android/armeabi-v7a/icu/source/i18n/[/code]
3. Compile and run on Android device

Create a simple Swift file named:

print("Hello,Android")[/code]

Use the Swift compiler built in step 2 to compile the Swift source code, and set the target to Android:

$build/Ninja/ReleaseAssert/swift-linux-x86_64/swiftc\#TheSwiftcompilerbuiltinthepreviousstep.-targetarmv7-none-linux-androideabi\#Targetingandroid-armv7.-sdk~/android-ndk-r10e/platforms/android-21/arch-arm\#UsethesameNDKpathandversionasyouusedtobuildthestdlibinthepreviousstep.-L~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a\#LinktheAndroidNDK'slibc++andlibgcc.-L~/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8\[/code]

This should generate a hello executable file in the directory where you execute the command. If you try to execute this executable in your Linux environment, you will see the following error:

Copy the codeThe code is as follows:
cannotexecutebinaryfile:Execformaterror

This is exactly the error we want: because this is an executable built on an Android device for execution - it should not be able to execute on Linux. Next, let's deploy it to an Android device to execute it.

4. Deploy the built products to the device

You can use the adb push command to copy the built product from the Linux environment to the Android device. Before you execute the adb devices command, make sure that your device is connected and can be listed, then execute the following command to copy Swift Android stdlib:

$adbpushbuild/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android//data/local/tmp$adbpushbuild/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android//data/local/tmp$adbpushbuild/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android//data/local/tmp$adbpushbuild/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android//data/local/tmp$adbpushbuild/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android//data/local/tmp[/code]

In addition, you also need to copy libc++ of Android NDK:

$adbpush~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so/data/local/tmp[/code]

Finally, you need to copy the hello executable file you built the previous step:

$adbpushhello/data/local/tmp[/code] 5. Execute "Hello, World" on Android device

You can use the adb shell command on your Android device to execute the hello executable file:

$adbshellLD_LIBRARY_PATH=/data/local/tmphello[/code]

You can see the following output:

Copy the codeThe code is as follows:
Hello,Android

Congratulations! You just ran your first Swift program on Android.