59 points · by ingve · 10y ago · 36 comments
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.
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).
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.
In the meantime, like you say, Kotlin works flawlessly on Android without requiring any support from Google.
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. 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).
Int:var i
var:Int i
var Int:i- static typing (non negotiable)
- performance (Kotlin is on par with Java, Groovy is... still very slow, which is especially a big deal on Android)