Foreignerds 3 1
Foreignerds 3 1

In recent developments, we proudly unveiled Spark for Teams, an innovative email client poised to revolutionize how teams interact with emails. While Spark has already earned accolades for its prowess in the realm of personal email management on iPhone, iPad, Mac, and Apple Watch, we’re excited to announce that the Android version is on the horizon. In the interim, we’re thrilled to introduce you to a groundbreaking toolchain we’ve developed, one that streamlines the process of working with Swift for Android. This toolchain empowers development teams to leverage their existing codebase from iOS and Mac platforms to craft exceptional Android applications.

Unveiling the Swift Android Compiler

The Apple Swift compiler has had the capacity to compile code for the Android platform for some time now. However, its complexity has deterred many developers from exploring its potential. Our toolchain addresses this challenge by simplifying the process, enabling developers to concentrate on creating remarkable apps for their users.

Why Swift for Android?

When embarking on the Android version of Spark, we sought a cross-platform language that could seamlessly serve both Android and iOS development. It was around this juncture that Swift evolved into an open-source project and eventually gained the capability to compile code for Android. It became evident to us that this was the direction we needed to take.

The Inner Workings of the Swift Android Compiler

The key to the Swift Android Compiler lies in the Low-Level Virtual Machine (LLVM). LLVM serves as a compiler infrastructure rooted in a three-phase design with re-targetability. In simpler terms, this means that any language equipped with an LLVM front-end compiler can be compiled for any target supported by an LLVM backend compiler. Google already harnesses LLVM compilers for native Android development with C/C++, making the integration of Swift code seamless from an Android device perspective. This approach allows developers to utilize the same instruments designed for C/C++ development, including Android profiling tools and an Android low-level debugger, with some restrictions on evaluating Swift code.

Available Frameworks

The available frameworks in our Swift Android toolchain encompass SwiftCore, Dispatch, and SwiftFoundation. SwiftCore and Dispatch closely resemble their iOS and Mac counterparts. However, SwiftFoundation differs, as it is a re-implementation of the original library’s classes. Consequently, SwiftFoundation may lack some features but adequately fulfills fundamental needs, such as network requests, JSON/XML parsing, and data storage.

Notably, high-level frameworks like UIKit are not available, meaning that your Swift app for iOS cannot effortlessly transition to Android. While Swift can be employed for business logic code, user interfaces and OS-dependent components must be re-written specifically for the Android platform.

Limitations of the Swift Android Compiler

As of now, the Swift compiler exclusively supports the ARM-v7A Android platform. The minimum supported Android version is 5.0 (Lollipop). One significant limitation stems from the generation of 32MB+ APK files, primarily due to the inclusion of SwiftCode, SwiftFoundation, and libDispatch.

Introducing the Swift Android Toolchain

One of the most substantial challenges of using the Swift compiler for Android development is the absence of official support from Apple. This translates to a lack of continuous integrations and official builds. Recognizing these hurdles, we set out to optimize the development experience for Android developers while minimizing the typical headaches associated with these tools. The result is the Swift Android Toolchain, a collection of tools that empowers Android developers to seamlessly incorporate Swift into their projects.

Our Swift Android Toolchain is an open-source project built upon the foundation of the Apple Swift compiler, SwiftJava, and SwiftAndroid projects. You can obtain the toolchain directly from Bintray (Mac only) or compile it yourself from GitHub. Currently, we are utilizing a forked version of Swift that includes options to disable @objc and dynamic features in the compiler, addressing compatibility issues on non-Darwin platforms. We are actively working towards transitioning to the official Swift repository in the future.

In addition, we leverage the Swift Package Manager (SPM) as the build system for Swift, aligning with Apple’s practices in compiler development and other Swift libraries. Our toolchain comes complete with SPM scripts that automate development and testing processes for Android apps.

Gradle Plugin for Swift

While building Swift code from the command line is feasible, Android developers are more inclined to utilize the Android Studio IDE. Fortunately, Android Studio utilizes the versatile Gradle build system. To facilitate this transition, we have developed a Gradle plugin that seamlessly integrates Swift compilation into the build process, enabling the development of mixed Swift/Java/Kotlin projects with ease.

JVM Interoperability

Since all Android applications run within the Java Virtual Machine (JVM), integrating native code, including Swift, requires loading libraries and establishing communication through the Java Native Interface (JNI). While the choice of JVM language (Java or Kotlin) and the approach to writing the bridging code is flexible, we recommend exploring our Swift Annotation Processor. This tool generates all JNI code from Java classes and interfaces, simplifying the integration process.

Blueprint Project: Swift in Action

To showcase the capabilities of the Swift Android Toolchain, the Android team at Readdle has developed the Blueprint project. This sample Swift application serves as an excellent starting point for those eager to explore Swift for Android. The Blueprint project is a straightforward to-do app, with business logic implemented in Swift, while the user interface is constructed using Java.

Streamlined Testing with XCTest

Testing is an integral part of any development process. Fortunately, the Swift project includes a testing framework called XCTest, which can be harnessed for testing Swift code on the Android platform. The Swift Package Manager simplifies test compilation into one comprehensive binary executable. Developers can then upload this executable to Android devices and execute tests via the Android shell.

BONUS: Our toolchain streamlines every facet of the testing process, and the Blueprint project includes a selection of simple tests for you to explore.

Conclusion

Swift for Android presents an exciting opportunity for developers seeking to expand their horizons. While it comes with certain limitations and challenges, the Swift Android Toolchain offers a comprehensive solution to make the journey smoother. As we continue to refine and enhance our tools, we invite you to explore the potential of Swift for Android and join us in shaping the future of cross-platform app development.

© 2013 - 2024 Foreignerds. All Rights Reserved

facebookFacebook
twitterTwitter
linkedinLinkedin
instagramInstagram
whatsapp
support