Developers can use available workarounds today, but the coming open-sourced Swift will be the real enabler.
Apple’s Swift language, initially intended for iOS and OS X applications, will become available for Android development as part of Apple’s decision to open-source Swift. But developers can start using Swift now for Android, says developer Romain Goyet, author of a recent blog post on the concept.
Swift uses the LLVM compiler, notes Goyet, who gained notoriety by reverse-engineering Apple’s Siri. “The current Swift compiler plugs into LLVM to generate binaries,” he writes. “It is also worth noting that both LLVM and Swift were designed by Chris Lattner.”
Instead of generating code for a specific architecture, LLVM generates assembly code for an imaginary machine; the intermediate representation is then converted to actual code for the targeted architecture.
Goyet describes a process in which the Swift compiler is asked to generate LLVM-IR (intermediate representation); LLVM also generates the ARM ELF file format used from the intermediate representation. Then the Android NDK, which allows for use of native-code languages on Android, generates a binary linking against the generated object file. The file is packaged as an Android app.
But not all parts are in place for the coding process, Goyet notes. “The biggest issue here is going to be a missing SwiftCore library. … Right now Apple is shipping one for iOS, OS X, and WatchOS. But that’s it — and obviously they don’t ship an Android version.”
Not all Swift code requires the SwiftCore library, however. “So as long as we use the subset of Swift that doesn’t hit SwiftCore, we should be OK,” Goyet says.
Still, Goyet does not see much potential yet for Swift on Android “because the language is only part of the problem. The other — and bigger — half being the UI framework. And there is no Swift UI framework on Android yet.”
Goyet expects Apple’s open-sourcing effort for Swift to include SwiftCore. “Then getting a SwiftCore library on Android should be a matter of rebuilding it.”
Goyet is not the only developer to venture into using Swift for Android. Mobile development studio ArcTouch last year wrote a proof-of-concept calculator app for both Android and iOS. ArcTouch used a custom compiler that parsed Swift code and output Java source code. The Swift code was written in Apple’s Xcode IDE, and the code was eventually moved toGoogle’s Android Studio IDE to finish the build process and produce an Android APK.
When Swift is available via open source, development tool companies will be able to write IDEs in which developers can write in Swift and directly compile to Android, ArcTouch CEO Eric Shapiro tells InfoWorld.
“Swift as a programming language is very similar to Java, very similar to C#, and it was not very difficult to get Swift to compile toward Android,” Shapiro says. Although it is not completely clear that Swift is better than Java, Shapiro says, Swift is newer and has some newer constructs, although Java keeps getting improved.