苹果官方文档:在Android设备上运行Swift代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
查看查看1073 回复回复12 收藏收藏 分享淘帖 转播转播 分享分享 微信
查看: 1073|回复: 12
收起左侧

苹果官方文档:在Android设备上运行Swift代码

[复制链接]
安卓大师 发表于 2016-8-10 18:00:57 | 显示全部楼层 |阅读模式
快来登录
获取优质的苹果资讯内容
收藏热门的iOS等技术干货
拷贝下载Swift Demo源代码
订阅梳理好了的知识点专辑
本帖最后由 安卓大师 于 2016-8-10 18:10 编辑

苹果官方推出了文档,支持在Android设备上运行Swift代码!

英文原文:Getting Started with Swift on Android

苹果官方文档:在Android设备上运行Swift代码 1

苹果官方文档:在Android设备上运行Swift代码 - 敏捷大拇指 - 苹果官方文档:在Android设备上运行Swift代码 1


以下是中英文混搭资料,奉献给敏捷大拇指上的各位Swift开发者,以后就不要说什么苹果安卓不是一家的分外话了 ^_* 。

更多Swift运行在Android上的资料,请看我的淘帖:《Swift开发安卓Android App》,欢迎订阅。




1、Quick Start

The Swift stdlib can be compiled for Android armv7 targets, which makes it possible to execute Swift code on a mobile device running Android. This guide explains:

  • How to run a simple "Hello, world" program on your Android device.
  • How to run the Swift test suite, targeting Android, and on an Android device.


If you encounter any problems following the instructions below, please file a bug using https://bugs.swift.org/.

目前,Swift的标准库stdlib可以被编译运行在Android armv7环境下,这就意味着我们可以在一个承载着Android系统的移动设备上运行Swift代码。本部分也就是说明如何在Android设备上运行熟知的Hello World。如果你在实践的过程中发现了什么问题,可以在这里寻求帮助。




2、FAQ

Let's answer a few frequently asked questions right off the bat:

Does this mean I can write Android applications in Swift?

No. Although the Swift compiler is capable of compiling Swift code that runs on an Android device, it takes a lot more than just the Swift stdlib to write an app. You'd need some sort of framework to build a user interface for your application, which the Swift stdlib does not provide.

Alternatively, one could theoretically call into Java interfaces from Swift, but unlike as with Objective-C, the Swift compiler does nothing to facilitate Swift-to-Java bridging.

需要注意的是,虽然现在Swift编译器能够直接在Android设备上运行Swift代码,但是并不意味着你可以用Swift来直接写一个APP了。对于一个常见的APP而言还包含着大量的用户界面的构建,这并不是Swift stdlib所能支持的。不过你可以选择从Swift中调用Java的界面构造,但是这个和OJC与Swift之间的关系也不一样,Swift编译器并不能够对这一步进行优化。




3、Prerequisites

首先,你需要准备以下的开发环境:



To follow along with this guide, you'll need:

  • A Linux environment capable of building Swift from source, specifically Ubuntu 15.10 (Ubuntu 14.04 is not currently supported). The stdlib is currently only buildable for Android from a Linux environment. Before attempting to build for Android, please make sure you are able to build for Linux by following the instructions in the Swift project README.
  • The latest version of the Android NDK (r12 at the time of this writing), available to download here: http://developer.android.com/ndk/downloads/index.html.
  • An Android device with remote debugging enabled. We require remote debugging in order to deploy built stdlib products to the device. You may turn on remote debugging by following the official instructions: https://developer.chrome.com/devtools/docs/remote-debugging.


苹果官方文档:在Android设备上运行Swift代码 2

苹果官方文档:在Android设备上运行Swift代码 - 敏捷大拇指 - 苹果官方文档:在Android设备上运行Swift代码 2





4、Part One: "Hello, world" on Android



4.1、构建Swift Android标准库依赖 Downloading (or building) the Swift Android stdlib dependencies

首先我们需要构建以下标准库:

[Bash shell] 纯文本查看 复制代码
apt-get install libicu-dev icu-devtools


You may have noticed that, in order to build the Swift stdlib for Linux, you needed to apt-get install libicu-dev icu-devtools. Similarly, building the Swift stdlib for Android requires the libiconv and libicu libraries. However, you'll need versions of these libraries that work on Android devices.

You may download prebuilt copies of these dependencies, built for Ubuntu 15.10 and Android NDK r12. Click here to download, then unzip the archive file.


另外为了构建libiconv与libic:

(1)确保已经安装了curl、autoconf、automake、libtool以及git工具。

(2)使用git clone git@github.com:SwiftAndroid/libiconv-libicu-android.git命令从项目中克隆下源代码

(3)在命令行中运行which ndk-build,来确保部署好的NDK能够成功执行。

(4)进入libiconv-libicu-android目录然后启动build.sh脚本。

(5)确保在libiconv-libicu-android目录下出现了armeabi-v7a/icu/source/i18n 与 armeabi-v7a/icu/source/common两个文件夹。

Alternatively, you may choose to build libiconv and libicu for Android yourself:

  • Ensure you have curl, autoconf, automake, libtool, and git installed.
  • Clone the SwiftAndroid/libiconv-libicu-android project. From the command-line, run the following command: git clone https://github.com/SwiftAndroid/libiconv-libicu-android.git .
  • From the command-line, run which ndk-build. Confirm that the path to the ndk-build executable in the Android NDK you downloaded is displayed. If not, you may need to add the Android NDK directory to your PATH.
  • Enter the libiconv-libicu-android directory on the command line, then run build.sh.
  • Confirm that the build script created armeabi-v7a/icu/source/i18n and armeabi-v7a/icu/source/common directories within your libiconv-libicu-android directory.




4.2、构建Android的Swift stdlib   Building the Swift stdlib for Android

进入Swift文件夹,运行构建脚本,注意要传入Android NDK与libicu/libiconv目录:

Enter your Swift directory, then run the build script, passing paths to the Android NDK, as well as the directories that contain the libicuuc.so and libicui18n.so you downloaded or built in step one:

[Bash shell] 纯文本查看 复制代码
$ 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/




4.3、编译hello.swift并且运行在Android设备上  Compiling hello.swift to run on an Android device


4.3.1、创建一个简单的Swift文件,叫做hello.swift

Create a simple Swift file named hello.swift:

[Swift] 纯文本查看 复制代码
print("Hello, Android")


To compile it, we need to make sure the correct linker is used. Symlink the gold linker in the Android NDK into your PATH:

[Bash shell] 纯文本查看 复制代码
$ sudo ln -s \
    /path/to/android-ndk-r12/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/arm-linux-androideabi/bin/ld.gold \
    /usr/bin/armv7-none-linux-androideabi-ld.gold



4.3.2、使用Swift编译器进行编译,注意选择的目标要是Android

Then use the built Swift compiler from the previous step to compile a Swift source file, targeting Android:

[Bash shell] 纯文本查看 复制代码
$ 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环境下运行该文件,可能出现以下错误:

This should produce a hello executable in the directory you executed the command. If you attempt to run this executable using your Linux environment, you'll see the following error:

[Bash shell] 纯文本查看 复制代码
cannot execute binary file: Exec format error


This is exactly the error we want: the executable is built to run on an Android device--it does not run on Linux. Next, let's deploy it to an Android device in order to execute it.



4.4、部署到设备上  Deploying the build products to the device

You can use the adb push command to copy build products from your Linux environment to your Android device. If you haven't already installed adb, you may do so via apt-get:

[Bash shell] 纯文本查看 复制代码
$ sudo apt-get install android-tools-adb


你需要使用adb push命令来讲刚才构建好的可执行程序从Linux环境中复制到Android设备上。确保你的设备已经成功连接到了Linux系统上,可以使用adb devices命令进行验证,然后使用如下命令来讲Swift Android stdlib复制到设备上:

Once you have adb installed, verify your device is connected and is listed when you run the adb devices command, then run the following commands to copy the Swift Android stdlib:

[Bash shell] 纯文本查看 复制代码