Introduction to writing Android application with Swift language

  • 2021-07-09 09:07:40
  • OfStack

The Swift standard library compiles the Android armv 7 kernel, which makes it possible to execute Swift statement code on Android mobile devices. This article explains how to run a simple "hello, world" program on your Android phone.

Frequently Asked Questions

Let's answer the following frequently asked questions:

Does this mean that I can quickly develop Android applications with Swift?

Dreaming, though, the Swift compiler is capable of compiling and running Swift code on Android devices. This requires not only writing an APP with the Swift standard library, but also a framework to build your application user interface, which cannot be provided by the above Swift standard library.

On the other hand, an Java application program interface can theoretically be called from Swift, but unlike Objective-C, the Swift compiler has no effect on Swift-to-Java bridging.

Preparatory knowledge

In order to use this wizard smoothly, you need to:

1. Linux environment that can compile Swift source code. At present, stdlib can only be compiled into Android usable version under Linux environment. Before trying to build Android, make sure you can compile Linux with reference to README of Swift project.

2. Android NDK, greater than or equal to version 21, available for download at the following link:

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

3. 1 Android device that can be debugged remotely. We need to deploy stdlib results to Android devices through remote debugging. You can debug remotely by following the official wizard: https://developer.chrome.com/devtools/docs/remote-debugging.

"Hello, world" on Android 1. Building Swift Android stdlib dependencies

You may have noticed that in order to build Swift stdlib under Linux, you need apt-get install libicu-dev icu-devtools. Simply put, Swift stdlib built on Android devices requires libiconv and libicu. However, you need Android device versions of these libraries.

Building libiconv and libicu for Android devices:

1. Make sure you have curl, antoconf, antomake, libtook and git installed.

2. Cloning of SwiftAndroid/libiconv-libicu-android project. Execute the following command from the command line: git clone git @ github. com: SwiftAndroid/libiconv-libicu-android. git.

3. Execute which ndk-build on the command line. Make sure ndk-build displays the executable path in the Android NDK you download. If it doesn't, you'll need to add the Android NDK directory to your PATH.

4. Enter the libiconv-libicu-android directory on the command line and execute build. sh.

5. Make sure the build script builds the armeabi-v7a/icu/source/i18n and armeabi-v7a/icu/source/common directories in your libiconv-libicu-android directory.

2. Build Switf stdlib for Android

Enter your Swift directory, then run the build script and 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 hello. swift and run it on Android devices

Create a simple Swift file named hello. swift:

print("Hello,Android")[/code]

Compile the Swift source code using the Swift compiler built in Step 2, targeting 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 an hello executable file in the directory where you execute the command. If you try to execute this executable in your Linux environment, you will see the following error:

cannotexecutebinaryfile:Execformaterror

This is exactly the error we want: because this is an executable built on Android devices for execution-it should not be able to execute on Linux. Next, let's deploy it to Android devices to execute it.

4. Deploy the built product to the device

You can use the adb push command to copy the built product from the Linux environment to Android devices. Make sure your device is connected and listed before you execute the adb devices command, and then execute the following command to copy Swift 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 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 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 Android devices to execute the hello executable:

$adbshellLD_LIBRARY_PATH=/data/local/tmphello[/code]

You can see the following output:

Hello,Android

Congratulations! You just ran your first Swift program on Android.


Related articles: