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

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

苹果官方文档:在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] 纯文本查看 复制代码
$ 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


另外,你也需要复制Android NDK的libc++:

In addition, you'll also need to copy the Android NDK's libc++:

[Bash shell] 纯文本查看 复制代码
$ adb push ~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so /data/local/tmp


最后,你需要将`hello`这个可编译包复制过去:

Finally, you'll need to copy the hello executable you built in the previous step:

[Bash shell] 纯文本查看 复制代码
$ adb push hello /data/local/tmp




4.5、运行Hello World  Running "Hello, world" on your Android device

在将所有的依赖项与可执行程序复制到了Android系统中之后,可以使用`adb shell`命令来执行`hello`这个可执行程序:

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

[Bash shell] 纯文本查看 复制代码
$ adb shell LD_LIBRARY_PATH=/data/local/tmp hello


那么将得到如下的结果:

You should see the following output:

[Swift] 纯文本查看 复制代码
Hello, Android


Congratulations! You've just run your first Swift program on Android.




5、Part Two: Running the Swift test suite hosted on an Android device

When running the test suite, build products are automatically pushed to your device. As in part one, you'll need to connect your Android device via USB:

  • Connect your Android device to your computer via USB. Ensure that remote debugging is enabled for that device by following the official instructions: https://developer.chrome.com/devtools/docs/remote-debugging.
  • Confirm the device is connected by running adb devices. You should see your device listed.
  • Run the tests using the build script:


[Bash shell] 纯文本查看 复制代码
$ utils/build-script \
  -R \                                           # Build in ReleaseAssert mode.
  -T \                                           # Run all tests.
  --android \                                    # Build for Android.
  --android-ndk ~/android-ndk-r12 \              # Path to an Android NDK.
  --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/


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

都看到这里了,就把这篇资料推荐给您的好朋友吧,让他们也感受一下。

回帖是一种美德,也是对楼主发帖的尊重和支持。

*声明:敏捷大拇指是全球最大的Swift开发者社区、苹果粉丝家园、智能移动门户,所载内容仅限于传递更多最新信息,并不意味赞同其观点或证实其描述;内容仅供参考,并非绝对正确的建议。本站不对上述信息的真实性、合法性、完整性做出保证;转载请注明来源并加上本站链接,敏捷大拇指将保留所有法律权益。如有疑问或建议,邮件至marketing@swifthumb.com

*联系:微信公众平台:“swifthumb” / 腾讯微博:@swifthumb / 新浪微博:@swifthumb / 官方QQ一群:343549891(满) / 官方QQ二群:245285613 ,需要报上用户名才会被同意进群,请先注册敏捷大拇指

嗯,不错!期待更多好内容,支持一把:
支持敏捷大拇指,用支付宝支付10.24元 支持敏捷大拇指,用微信支付10.24元

评分

参与人数 1金钱 +10 贡献 +10 专家分 +10 收起 理由
Anewczs + 10 + 10 + 10 32个赞!专家给力!

查看全部评分

本帖被以下淘专辑推荐:

Anewczs 发表于 2016-8-10 18:13:04 | 显示全部楼层
安卓和苹果原来是一家!
Anewczs 发表于 2016-8-10 18:13:12 | 显示全部楼层
安卓和苹果原来是一家!
 楼主| 安卓大师 发表于 2016-8-10 18:14:48 | 显示全部楼层
Anewczs 发表于 2016-8-10 18:13
安卓和苹果原来是一家!

56个民族,56朵花,爱我Swift!
昏戏湿 发表于 2016-8-10 18:45:01 | 显示全部楼层
这等于把安卓的老底给端掉了啊
growthhacker 发表于 2016-8-10 21:29:11 | 显示全部楼层
会进一步从Android设备到其它设备吧?
rightnow 发表于 2016-8-11 19:31:22 | 显示全部楼层
官方啊!!!
swifter 发表于 2016-8-17 02:15:58 | 显示全部楼层
昏戏湿 发表于 2016-8-10 18:45
这等于把安卓的老底给端掉了啊

同感!
软蛋 发表于 2016-9-5 23:38:21 | 显示全部楼层
苹果这招真狠,比MoveToIos还狠!
不是我教你诈 发表于 2016-9-18 22:22:10 | 显示全部楼层
狠!!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

做任务,领红包。
我要发帖

分享扩散

都看到这里了,就把这资料推荐给您的好朋友吧,让他们也感受一下。
您的每一位朋友访问此永久链接后,您都将获得相应的金钱积分奖励
热门推荐

合作伙伴

Swift小苹果

  • 北京治世天下科技有限公司
  • ©2014-2016 敏捷大拇指
  • 京ICP备14029482号
  • Powered by Discuz! X3.1 Licensed
  • swifthumb Wechat Code
  •   
快速回复 返回顶部 返回列表