Kotlin for Android Developers
javaadvent.com
javaadvent.com
In addition to Anko, there is also a similar (but much smaller) library that I maintain - https://github.com/zserge/anvil
It is also a DSL for defining view layouts in code, much like Annko. The major difference is that Anvil supports data binding and has reactive nature (think of it as of react.js for android).
I've been working more with React Native recently because I'm working on a cross-platform project, but for Android-only development the combination of Kotlin and Anvil gives you pretty much the same level of declarativeness and conciseness while still enforcing a decent type system and not being so far removed from the platform.
public override fun view() {
// A designer gave us an XML with some fancy layout:
// a viewgroup with a button and a progress bar in it
xml(R.layout.my_layout) {
backgroudnColor(Settings.bgColor) // will modify root layout view color
withId(R.id.my_button) {
// button state may depend on some variable
enabled(isMyButtonEnabled)
// button click listener can be attached
onClick { v ->
...
}
}
withId(R.id.my_progress_bar) {
visible(isMyProgressBarShown)
progress(someProgressValue)
}
}
}
I know many people are opposed to defining layouts in code, so this can be a good compromise for them. Also, it's very helpful if you have a large project made with XMLs and want to refactor it.I am have nothing against getting rid of XML : it is not that bad in practice since the IDE writes the boring parts but still ugly to read.
Separating the presentation from the code is an important feature though, especially for very large applications.
Suggestion : I don't see anything regarding density buckets, composability with merge tags or other strengths of the existing system on the repo readme. It might be a good idea to treat these as well.
I'm still kind of flabbergasted that Google has no official (publicly known) plan to start moving away from Java given the lawsuits, general disregard for Java (keep in mind I say this as someone whose day job is Android development in Java), warm reception of Swift, etc.
I was hoping that the Android Studio/JetBrains connection would result in some sort of officially-blessed move towards Kotlin, but disappointed on that front so far.
Not that you need Google's official blessing to use Kotlin -- it works great today, but there's often political pressure not to use "fringe" languages when working on projects at most companies which is a pain to deal with and which is still forcing a lot of people to use shitty old Java 6.
In the meantime, like you say, Kotlin works flawlessly on Android without requiring any support from Google.
https://news.ycombinator.com/item?id=10521258 http://flutter.io/faq/
Make no mistake: if Java is ever replaced as the main language on Android, it will be by a language that's at least as fast (which kind of implies that language has to be statically typed, at the very least).
If it doesn't already[1], it could leverage the optional typing for block versioning on initial compilation, and assume that version is correct most of the time (generating the most-optimized version initially because you expect to not bail out of this block version). This way you wouldn't have to warm up for type inference. If the program type-checks you can get almost static type performance.
Dart also has an advantage in app startup time since you can just load your snapshot.
--
1: I'm not familiar with the internals of the Dart VM, but I don't believe it leverages block versioning[2] at the moment. That seems like a low hanging fruit when you already have type hints. Your initial baseline compile can generate a specialized version from the get-go then lazily add other blocks if the type hints are wrong.
Dart is between two and five times slower than Java, as of six months ago [1].
It's not clear whether Dart can close that gap since Java is not standing still either, so I remain completely unconvinced that Dart will ever be the replacement of Java on Android.
Dart could even in theory have a compiler that assumes all your type annotations are correct and simply AOT compiles your code. (So your workflow would be: develop -> fix type errors -> run in regular VM -> loop -> AOT compile -> deploy.) Whether that would be useful is another matter, but it could certainly be possible (some Common Lisp implementations in fact do exactly this [2]).
--
1: https://www.dartlang.org/support/faq.html#q-why-dart
2: http://www.sbcl.org/1.0/manual/Declarations-as-Assertions.ht...
Sadly the official response of the Android frameworks team is that they have no plan to do so in the near future since : -if you want to write your app in Kotlin, you can already do so. It is completely supported by Android (even though truthfully you don't have idiomatic support of Kotlin in the platform's API). -moving to Kotlin would be a very big commitment for the platform and there are some big questions to answer like what about the people who want to write Java ?
Still, if in 2 years a good chunk of the community has moved to Kotlin, it might be reason enough for them to reconsider. Some members of the Android teams are already using Kotlin extensively, for exemple in the DataBinding library.
supporting the language of the moment is a terrible idea.
Even with a relatively nice and declarative development API (which Android provides), the amount of code to put anything on the screen, much less accomplish any useful work, has always put me off of building anything of note on the platform. My brain turns off when there's so much reading to be done to derive any meaning from a function; e.g. "What does this do?"...three dense pages of code later..."Ah, it saves the current state to storage."
In short, I'm pretty excited about Kotlin. Maybe I'll finally start working on Android apps, beyond mere tinkering.
And configure your project to support Kotlin with a find action or a `Tools > Kotlin > Configure in Project` Menu item click. From there you can mix and match Java and Kotlin classes in the same project with great interoperability for calling code between each. The Kotlin plugin also has a nice feature (Ctrl + Alt + Shift + K) to convert a Java class into idiomatic Kotlin which is a great way to explore Kotlin features.
Even whilst the plugin has stability issues, I don't see myself developing another Android App with vanilla Java 7 - Kotlin's just so much a more productive and enjoyable dev experience, that Android Studio should have the Kotlin plugin enabled by default.
Also, Scala will soon only support Java 8, which Android doesn't support.
Kotlin has neither of these problems.
https://github.com/JetBrains/kotlin/blob/build-1.0.0-beta-35...
var i: Int = 1 var i = 1 //Type inference
var i:Int = 1 //Explicit type
i.e allowing you to optionally enhance existing code with explicit type info without changing the existing definition. Int:var i
var:Int i
var Int:i var i = 1 //Type inference
var Int i = 1 //Explicit type
But it seems a common convention for optional typing. Maybe it suits dynamic developers?Also you can parse var i: Int = 1 unambiguously without backtracking, while you have to backtrack on var Int i = 1 (unless you enforce some constraint like type names must start with capitals, in which case you may as well just do Int i = 1).
- static typing (non negotiable)
- performance (Kotlin is on par with Java, Groovy is... still very slow, which is especially a big deal on Android)