使用 Swift 语言编写 Android 应用入门

Swift标准库可以编译安卓armv7的内核,这使得可以在安卓移动设备上执行Swift语句代码。本文解释了如何在你的安卓手机上运行一个简单的“hello,world”程序。

如果你遇到了任何问题,请参考下面的说明,上传BUG到 https://bugs.swift.org/.

常见问题解答

让我们来回答如下经常被问及的问题吧:

这是否以为着我能够用Swift快速的开发安卓应用?

做梦,虽然Swift编译器可以胜任在安卓设备上编译Swift代码并运行。这需要的不仅仅是用Swift标准库编写一个APP,更多的是你需要一些框架来搭建你的应用用户界面,以上这些Swift标准库不能提供。

另一方面,一个理论上可以从Swift调用Java应用程序接口,但是不同于Objective-C,Swift编译器对Swift-to-Java桥接毫无作用。

预备知识

为了能顺利使用这份向导,你需要:

1. 可以编译Swift源码的Linux环境。stdlib目前只能在Linux环境下编译成安卓可用版本。在尝试为安卓构建之前,确保你能够参考Swift项目的README为Linux做编译。

2. 安卓NDK,高于或等于21版本,在以下链接提供下载:

http://developer.android.com/ndk/downloads/index.html.

3. 一台可以远程调试的安卓设备。我们需要通过远程调试来讲stdlib结果部署到安卓设备上。你可以按以下官方向导来远程调试: https://developer.chrome.com/devtools/docs/remote-debugging.

安卓上的”Hello, world”

1. 构建Swift Android stdlib 依赖

你可能注意到了,为了构建Linux下的Swift stdlib,你需要 apt-get install libicu-dev icu-devtools。 简单来说,构建在安卓设备上使用的Swift stdlib需要libiconv和libicu。然而,你需要这些库的安卓设备版本。

为安卓设备构建libiconv和libicu:

1. 确定你安装了 curl, antoconf, antomake, libtook 和git。

2. 克隆 SwiftAndroid/libiconv-libicu-android 项目。通过命令行执行以下命令:git clone git@github.com:SwiftAndroid/libiconv-libicu-android.git。

3. 在命令行执行 which ndk-build。确定在你下载的安卓NDK里ndk-build能显示可执行路径。如果不能显示,你需要将安卓NDK的目录加到你的PATH里。

4. 在命令行输入 libiconv-libicu-android 目录,然后执行 build.sh。

5. 确定构建脚本在你的libiconv-libicu-android目录构建了 armeabi-v7a/icu/source/i18n和armeabi-v7a/icu/source/common目录。

2. 构建安卓使用的Switf stdlib

输入你的Swift目录,然后运行构建脚本,将路径传递给安卓NDK和libicu/libiconv目录:

$ utils/build-script \
    -R \                                           # Build in ReleaseAssert mode.
    --android \                                    # Build for Android.
    --android-ndk ~/android-ndk-r10e \             # Path to an Android NDK.
    --android-ndk-version 21 \                     # The NDK version to use. Must be 21 or greater.
    --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/

3. 编译hello.swift并在安卓设备上运行

创建一个简单的Swift文件,命名为 hello.swift:

print("Hello, Android")

使用步骤2中构建好的Swift编译器来编译Swift源码,目标设定为安卓:

$ 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-r10e/platforms/android-21/arch-arm \               # Use the same NDK path and version as you used to build the stdlib in the previous step.
    -L ~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a \  # Link the Android NDK's libc++ and libgcc.
    -L ~/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8 \
    hello.swift

这样应该会在你执行命令的目录下生成一个hello可执行文件。如果你试图在你的Linux环境下执行这个可执行文件,你会看到如下错误:

cannot execute binary file: Exec format error

这正是我们想要的错误:因为这是为执行在安卓设备上构建的可执行文件–它不应该能在Linux上执行。下一步,让我们将它部署到安卓设备上来执行它。

4. 将构建好的产品部署到设备

你可以使用adb push 命令来将构建好的产品从Linux环境拷贝到安卓设备。当你执行adb devices命令前确定你的设备连接好并且可以被列出,然后执行以下命令来拷贝Swift Android stdlib:

$ 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

另外,你也需要拷贝安卓NDK的libc++:

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

最后,你需要拷贝你前一步构建好的hello可执行文件:

$ adb push hello /data/local/tmp

5. 在安卓设备上执行“Hello, World”

你可以在安卓设备上使用 adb shell 命令来执行hello可执行文件:

$ adb shell LD_LIBRARY_PATH=/data/local/tmp hello

你可以看到以下输出:

Hello, Android

祝贺你!你刚刚在安卓上运行了你的第一个Swift程序。

本文文字及图片出自 OSchina

余下全文(1/3)
分享这篇文章:

请关注我们:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注