The Swift standard library can compile the Android armv7 kernel, which allows swift statement code to be executed on the Android mobile device. This article explains how to run a simple "Hello,world" program on your Android phone.
Frequently Asked Questions
Let's answer the questions that are often asked:
Does this mean that I can use Swift to develop Android apps quickly?
Dream, although Swift compilers are capable of compiling swift code and running on Android devices. It's not just about using the SWIFT standard library to write an app, but more about the framework you need to build your application user interface, which these Swift standard libraries cannot provide.
On the other hand, one can theoretically invoke the Java application interface from Swift, but unlike the Objective-c,swift compiler, it has no effect on the Swift-to-java bridge.
Preliminary knowledge
In order to successfully use this guide, you need to:
1. You can compile the swift source Linux environment. Stdlib currently can only compile Shing available versions in Linux environments. Before attempting to build for Android, make sure you can refer to the readme of the Swift project for Linux compilation.
2. Android NDK, above or equal to 21 version, is available in the following link download:
Http://developer.android.com/ndk/downloads/index.html.
3. An Android device that can be debugged remotely. We need remote debugging to say STDLIB results are deployed to Android devices. You can debug remotely by following the Official wizard: Https://developer.chrome.com/devtools/docs/remote-debugging.
"Hello, World" on Android 1. Build Swift Android stdlib dependencies
You may have noticed that in order to build the Swift Stdlib under Linux, you need apt-get install Libicu-dev icu-devtools. Simply put, the swift Stdlib built on Android devices requires Libiconv and LIBICU. However, you will need these library versions of Android devices.
Build Libiconv and Libicu for Android devices:
1. Make sure you have installed curl, antoconf, Antomake, Libtook and Git.
2. Cloning of the Swiftandroid/libiconv-libicu-android project. Execute the following command from the command line: Git clone git@github.com:swiftandroid/libiconv-libicu-android.git.
3. Perform which ndk-build at the command line. Make sure Ndk-build can display executable paths in your downloaded Android NDK. If not, you need to add an Android NDK directory to your path.
4. Enter the Libiconv-libicu-android directory at the command line, and then execute build.sh.
5. Make sure that the build script builds the armeabi-v7a/icu/source/i18n and Armeabi-v7a/icu/source/common directories in your libiconv-libicu-android directory.
2. Construct the SWITF Stdlib used by Android
Enter your Swift directory, and then run the build script to pass the path to the Android NDK and Libicu/libiconv directories:
$utils/build-script\-r\ #BuildinReleaseAssertmode.--android\ #BuildforAndroid.--android-ndk~/android-ndk-r10e\# Pathtoanandroidndk.--android-ndk-version21\ #TheNDKversiontouse. mustbe21orgreater.--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/[/code]
3. Compile the Hello.swift and run it on the Android device
Create a simple Swift file named Hello.swift:
Print ("Hello,android") [/code]
Use the Swift compiler built in step 2 to compile the swift source code with the goal set to Android:
$build/ninja/releaseassert/swift-linux-x86_64/swiftc\# Theswiftcompilerbuiltinthepreviousstep.-targetarmv7-none-linux-androideabi\ #Targetingandroid-armv7.-sdk~/ android-ndk-r10e/platforms/android-21/arch-arm\# usethesamendkpathandversionasyouusedtobuildthestdlibinthepreviousstep.-l~/android-ndk-r10e/sources/cxx-stl/ Llvm-libc++/libs/armeabi-v7a\ #LinktheAndroidNDK ' slibc++andlibgcc.-l~/android-ndk-r10e/toolchains/ Arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8\hello.swift[/code]
This should generate a hello executable file in the directory where you execute the command. If you try to execute this executable file in your Linux environment, you will see the following error:
Copy Code code as follows:
Cannotexecutebinaryfile:execformaterror
That's exactly what we wanted: because this is an executable file built on an Android device-it shouldn't be done on Linux. Next, let's deploy it to an Android device to execute it.
4. Deploy the built products to the device
You can use the ADB push command to copy a built product from a Linux environment to an Android device. Before you execute the ADB devices command, make sure your device is connected and can be listed, and then execute the following command to copy Swift's Android stdlib:
$adbpushbuild/ninja-releaseassert/swift-linux-x86_64/lib/swift/android/libswiftcore.so/data/local/tmp$ adbpushbuild/ninja-releaseassert/swift-linux-x86_64/lib/swift/android/libswiftglibc.so/data/local/tmp$ adbpushbuild/ninja-releaseassert/swift-linux-x86_64/lib/swift/android/libswiftswiftononesupport.so/data/local/ tmp$adbpushbuild/ninja-releaseassert/swift-linux-x86_64/lib/swift/android/libswiftremotemirror.so/data/local/ tmp$adbpushbuild/ninja-releaseassert/swift-linux-x86_64/lib/swift/android/libswiftswiftexperimental.so/data/ Local/tmp[/code]
In addition, you also need to copy the libc++ of Android NDK:
$adbpush ~/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so/data/local/tmp[/code]
Finally, you need to copy the Hello executable that you built in the previous step:
$adbpushhello/data/local/tmp[/code] 5. Execute "Hello, World" on Android devices
You can use the ADB shell command on the Android device to execute the Hello executable file:
$adbshellLD _library_path=/data/local/tmphello[/code]
You can see the following output:
Copy Code code as follows:
Congratulations to you! You just ran your first Swift program on Android.