InfoQ Homepage News Using Swift to Build Code for Android

Using Swift to Build Code for Android

Leia em Português

This item in japanese


A “Port to Android” pull request that was recently merged into the official Swift repository master branch makes it possible to create simple programs for Android. The pull request added an Android target for Swift stdlib and allows developers to use a Linux environment to cross-compile for Android on the ARMv7 processor.

For the most part, the Swift port to Android was the effort of two developers: Zhuowei Zhang, who started the project at the end of 2015, and Facebook’s Brian Gesiak, who submitted the PR and addressed all feedback that was provided before it was eventually merged into master. Due to the sheer size of the PR, which originally included 54 changed files, the whole process took almost two months from submit to merge.

As mentioned, at the moment it is only possible to cross-compile to Android from a Linux environment. Additionally, the latest version of the Android NDK is required, plus libiconv and libicu libraries built for Android. With those dependencies in place, Swift for Android can be built by invoking:

$ utils/build-script \
  -R \                                           # Build in ReleaseAssert mode.
  -T \                                           # Run all tests.
  --android \                                    # Build for Android.
  --android-deploy-device-path /data/local/tmp \ # Temporary directory on the device where Android tests are run.
  --android-ndk ~/android-ndk-r10e \             # Path to an Android NDK.
  --android-ndk-version 21 \
  --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/ \

Once the compiler is available, it can be used to create an Android executable:

$ build/Ninja/ReleaseAssert/swift-linux-x86_64/swiftc \                   # The Swift compiler built in the previous step.
    -target armv7-none-linux-androideabi \                                # Targeting android-armv7.
    -sdk ~/android-ndk-r11c/platforms/android-21/arch-arm \               # Use the same NDK path and API version as you used to build the stdlib in the previous step.
    -L ~/android-ndk-r11c/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \  # Link the Android NDK's libc++ and libgcc.
    -L ~/android-ndk-r11c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9 \

This can be deployed to a real Android device along with Swift stdlib and Android NDK libc++ using the adb push command:

$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/ /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/ /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/ /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/ /data/local/tmp
$ adb push build/Ninja-ReleaseAssert/swift-linux-x86_64/lib/swift/android/ /data/local/tmp

$ adb push ~/android-ndk-r11c/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/ /data/local/tmp

$ adb push hello /data/local/tmp

Finally, the program can be executed using the adb shell command.

Since this port currently includes only Swift stdlib, there is still a lot of work to do before Swift can be effectively used to create fully-fledged Android apps, i.e. apps that provide a graphical user interface. This could be accomplished in two different ways, either by using some sort of Swift UI framework able to run on Android, or by interfacing with Android Java frameworks. It should be kept in mind, though, that the Swift compiler does not currently include any provisions to make it easier to bridge Swift to Java code as it happens with Objective C code.

Another possibility of using Swift code inside of a real Android app is using JNI/NDK to call Swift code from Java. Zhang also provided a prototype sample app to show how Swift code can be accessed over JNI from an Android Java app.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Interesting project

    by jean-simon Larochelle,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Interesting. I thought this would be implemented first as a Swift compiler for the Java VM with Java interop added (similar to JRuby for the Oracle JVM).
    I suppose it is interesting to have a alternative for "native" applications on Android.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.