Run Your First Swift “Hello, World” on Android: Step‑by‑Step Guide

This tutorial explains how to compile the Swift standard library for Android armv7, build required dependencies, create a simple Swift program, deploy the binaries via ADB, and successfully run a "Hello, Android" application on an Android device.

21CTO
21CTO
21CTO
Run Your First Swift “Hello, World” on Android: Step‑by‑Step Guide

FAQ

Q: Can I quickly develop Android apps with Swift? A: Not directly; the Swift standard library can compile for Android armv7, but you need additional UI frameworks, and Swift‑to‑Java bridging is not supported.

Prerequisites

A Linux environment capable of building Swift source code.

Android NDK version 21 or newer (download from the official Android site).

An Android device that can be remote‑debugged (see Chrome remote‑debugging guide).

Step 1: Build Swift Android stdlib dependencies

Install libicu-dev and icu-devtools on Linux, then build libiconv and libicu for Android as follows:

1. Ensure curl , autoconf , automake , libtool and git are installed. 2. Clone the repository: git clone [email protected]:SwiftAndroid/libiconv-libicu-android.git 3. Verify that ndk-build is in your PATH . 4. Run build.sh inside the cloned directory. 5. The script creates armeabi-v7a/icu/source/i18n and armeabi-v7a/icu/source/common directories.

Step 2: Build the Swift stdlib for Android

Run the build script with the appropriate NDK and ICU paths:

$ utils/build-script \
 -R \
 --android \
 --android-ndk ~/android-ndk-r10e \
 --android-ndk-version 21 \
 --android-icu-uc ~/libicu-android/armeabi-v7a/libicuuc.so \
 --android-icu-uc-include ~/libicu-android/armeabi-v7a/icu/source/common \
 --android-icu-i18n ~/libicu-android/armeabi-v7a/libicui18n.so \
 --android-icu-i18n-include ~/libicu-android/armeabi-v7a/icu/source/i18n/

Step 3: Compile hello.swift and run on the device

Create a file hello.swift containing: print("Hello, Android") Compile it with the Swift compiler built in the previous step:

$ build/Ninja/ReleaseAssert/swift-linux-x86_64/swiftc \
 -target armv7-none-linux-androideabi \
 -sdk ~/android-ndk-r10e/platforms/android-21/arch-arm \
 -L ~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \
 -L ~/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8 \
 hello.swift

The resulting hello binary will fail on Linux with “cannot execute binary file: Exec format error”, which is expected because it is built for Android.

Step 4: Deploy to the Android device

Use adb push to copy the Swift stdlib libraries, the NDK’s libc++_shared.so, and the compiled hello binary to /data/local/tmp on the device:

$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftCore.so /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftGlibc.so /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftOnoneSupport.so /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftRemoteMirror.so /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/libswiftSwiftExperimental.so /data/local/tmp
$ adb push ~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so /data/local/tmp
$ adb push hello /data/local/tmp

Step 5: Run on the Android device

Execute the binary via adb shell: $ adb shell LD_LIBRARY_PATH=/data/local/tmp hello The output should be: Hello, Android Congratulations – you have run your first Swift program on Android.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cross-platformAndroidSwiftTutorial
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.