使用 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程序。
來自: http://www.oschina.net/translate/getting-started-with-swift-on-android