Modular Programming for Android Using Swift


Yuri Brigance | 11/18/2016 | Digital Experience and Mobile | Thought Leadership

Built from scratch with modern technologies and released in 2014, Apple’s Swift has gained a lot of momentum and credibility as a programming language. Initially launched for iOS, Apple has positioned Swift to eventually replace its stalwart language, Objective-C, but has recently made it open source. In some ways, this move makes it a genuine competitor to Java on a number of platforms, especially within the mobile development community.

Java, of course, is still the gold standard in open source programming languages. As of July 2016, it holds the #2 spot of most popular programming languages according to Redmonk’s biannual programming language rankings list, which compares the size of a language’s community to its use in projects using data from GitHub and StackOverflow. If you’re curious, the #1 spot goes to JavaScript with C++ and C# ranking #5 on the list behind PHP and Python. C ranks #9 and Objective-C ranks #10.

Swift ranks #17. It’s actually pretty impressive how high it is on the list given its relative infancy. In fact, this most recent list is the first ranking where Swift has failed to make a huge leap in the ranks, but its popularity hasn’t subsided; the competition is just tougher at the top. To encourage its use, Apple has created the Swift Playgrounds app for budding developers to learn to code in Swift, potentially positioning Swift as a cornerstone programming language for the next generation. Recently, Swift has even found practical application outside of mobile apps, with some interesting examples of use in server-side scripting. In partnership with Apple, IBM made its cloud-hosted application development environment open to Swift to encourage apps for the enterprise.

Given that Swift is now open source, the question arises of whether Swift might be a better option than Java for certain objectives. In a surprise twist, Google even hinted that it might consider adopting Swift on Android. A comparison of the two languages at present shows the strengths and weaknesses of each:

Java’s Upsides

  • Java is one of the most popular languages with an immense developer base.
  • Java has established open-source libraries and frameworks.
  • It’s flexible enough to write Android mobile apps and server-side code. It’s still king on the server side.

Java’s Downsides

  • Java on Android has nagging verbosity problems that Swift can solve, specifically around closures, which are currently implemented as inline Runnable instances on Android. However, this is likely to be challenged in the near future by Java’s lambda expressions once full support arrives to the Android platform.
  • Java only has eight primitive types. Swift has many more (though technically they are not “primitives” in that they are all first-class objects). This greatly simplifies work with binary data and byte streams compared to Java. Ever had to parse a 12-bit unsigned integer in Java? Swift makes it much easier.
  • While Java has introduced some optimizations to make the language less verbose, it will probably take more iterations of the language to address these issues and programmers will have to live with these issues for some time to come.
  • The long-running legal battle between Oracle and Google’s use of Java APIs has gathered storm clouds over the language.

Swift’s Upsides

  • It’s fairly fast and compiles directly to machine code
  • The syntax will be familiar to Java, C, and JavaScript developers. It’s less verbose than Java.
  • Swift is not garbage collected, so there is no “random” garbage collection cycle where memory is cleaned up automatically, meaning you don’t get the occasional UI hiccup that you get with Java apps.

Swift’s Major Downsides

  • Swift is still changing as it evolves. Swift 3 introduced yet another barrage of breaking changes by upending some of the most basic and well established programming paradigms, such as how for-loops are defined.
  • While Swift will compile on most platforms, if you want to build UI you are stuck using Apple’s Cocoa and CocoaTouch frameworks, which only run on Apple operating systems. You won’t be building Android UI with Swift any time soon.
  • There are no Swift/Java bindings, so it’s not (yet) possible to use Swift exclusively to write full-blown Android applications.
  • Developer tools aren’t very robust, yet. There is no official Windows IDE for Swift. In fact, at the time of this article’s writing, only two IDEs support Swift: Xcode (free) and AppCode (paid).

Swift on Android – Why Bother?

So why do we bother talking about Swift usage on Android? Well, iOS and Android are the two most popular mobile platforms, and often times developers have to create two versions of the same app – one for iOS and one for Android. Since Java is not supported on iOS, anything you write for Android is not portable in the mobile context. You could use C or C++, as both will compile to iOS and Android – a rather cumbersome approach. Swift, however, is a modern first-class language on iOS that happens to compile to Android with relative ease.

Let’s propose a practical scenario where you may want to do this. Imagine that you’re building an app that manipulates images—something similar to Instagram or Prisma, both popular applications that apply image filters. Would you rather:

a) Write your filter algorithms in both Java and Swift, and maintain two separate codebases with two separate test suites? Or…

b) Write them once in Swift and use on both Android and iOS?

In this example the benefit of using Swift is very clear. And image manipulation is not the only use case. Audio processing, algorithmic tasks, database manipulation, compression, and more are all compelling use cases to use Swift for modular cross-platform development. You can even reuse the same Swift code on Linux, and certainly (eventually) on Windows. The cost savings alone would be worth it.

Cross Platform vs. Hybrid

When it comes to mobile, where Swift is primarily being utilized, it’s important to keep in mind that it’s not a cross-platform miracle language. Just because it compiles to machine code and can actually run on both iOS and Android (and Linux) does not mean that it enables a “write once, run everywhere” approach to mobile development. iOS and Android have vastly different internal frameworks for everything. Things like UI, networking, sound, camera access, and so on are completely different on both platforms.

That said, for many years now native “hybrid” applications have used cross-platform technologies, namely JavaScript, to create consistent cross-platform UI elements for both iOS and Android. Zillow is a great example – their pie charts and other graphs are actually JavaScript code rendered within a web view and embedded within Android and iOS native apps. Why write two separate implementations for the same UI element when you can make them look native and have identical behavior on both platforms?

But being an interpreted language, JavaScript isn’t fast enough to do complex CPU intensive tasks to be useful as a cross-platform tool for anything other than simple math or UI. This is where Swift can help bridge the gap. The list of advantages is quite compelling:

  • Save time and money by writing the code once and using it on both platforms.
  • Save more time and money by maintaining and testing just one codebase instead of two separate implementations.
  • Writing a Swift framework forces developers to separate functionalityinto independent, interchangeable modules, making the code reusable and easier to test.
  • Consistent performance across all supported platforms.

And you can be sure that people are working on these things. If/when it becomes possible to fully utilize Swift on Android, plenty of iPhone developers will surely jump to the Android platform and provide the quality app development they’ve been doing for Apple for quite some time.

The Judge and Jury: Swift has Great Momentum

Last year, AIM cautioned that Swift is not yet ready for prime time, but it has made significant progress since then.

Apple is doing a magnificent job so far in positioning Swift for the future. Because it’s Apple, it has instant credibility. Large enterprises are now hiring Swift developers. Swift is already part of the conversation in developer job interviews.

It’s easy to envision a small startup writing its server-side code in Swift to be edgy and then being acquired by a large enterprise, which then tinkers with the code and uses it in its own production. It would only take a few moves for Swift to legitimately be used at the enterprise level.

While Swift isn’t quite ready yet take on Java, it is looking increasingly more likely to begin filling up SD cards of Android users in the form of modular bits of cross-platform code. Cost cutting is a major incentive for technology adoption, and it’s only a matter of time until we see case studies of companies shipping Swift modules across multiple platforms and bragging about how much time and money it has saved them. It’s a brilliant language that is changing the way developers think about apps.