Anko 0.6 is Released

Posted on by Andrey Breslav

Today we are glad to present the new version of Anko — a library which facilitates Android application development. We are happy to have received lots of feedback, and some of the changes in 0.6 were actually proposed by the community.

WARNING: Package Name Changed

Well, we are sorry. For historical reasons, Anko package name used to be kotlinx.android.anko, and we changed it to org.jetbrains.anko in 0.6, to be consistent with the Maven artifact name.

New Listeners

Anko 0.5 introduced partially defined listeners that reduce code verbosity: when we only need to define one method of a multi-methods listener, we do not have to implement the methods we do not care about. Based on your feedback (thanks, SalomonBrys!) this feature has been redesigned in 0.6:

  • Partially defined listeners can be now used outside DSL layouts as well as inside;
  • Syntax is easier to understand;
  • The logic “under the hood” is simpler.

This is what it looks like now:

editText {
    textChangedListener {
        onTextChanged { text, start, before, count ->
            toast("New text: $text")
        }
    }
}

Configuration Qualifiers

Qualifiers are used to support different layouts for different devices, locales etc.

Anko’s DSL now supports the configuration() function that specifies qualifiers the layout is meant for:

configuration(screenSize = ScreenSize.LARGE, orientation = Orientation.LANDSCAPE) {
    /* 
      This code will be only executed
      if the screen is large and its orientation is landscape
    */
}

This code is equivalent to having your XML layout under the layout-large-land directory. Technically, it is implemented through checking the specified qualifiers and only executing the code inside the configuration() if their values match. Therefore, usages of configuration() are not limited to DSL only: for example, you can safely call Android SDK functions which are not present in older versions of system using configuration(fromSdk = <version>) { /* code */ }.

The full list of supported qualifiers is available here.

Custom View Creation

The neatest way of incorporating your own custom views into the DSL is to create your own builder-like functions, but since it is time-consuming, Anko now supports a quicker way:

frameLayout {
    customView<CustomView> {
        backgroundResource = R.drawable.custom_view_bg
    }.linearLayout(width = matchParent)
}

It is implemented via Java Reflection. Though it is slower than the normal DSL functions, it is much easier when you are prototyping.

appcompat.v7 Views and Properties

We have made an initial step to support appcompat.v7 Android library. Extension functions for View classes in the support package and extension properties for its attributes are added to Anko. Widget tinting is not supported yet, we are hoping to implement it in later versions.

Top-level DSL Functions for Simple Views are Removed

Since it is very unlikely to have a simple non-container view (such as TextView) as the content view of your activities, we removed DSL functions for such views for the Activity and Fragment receivers. In the unlikely case of needing such a view on the top level, use UI() wrapper function:

UI {
    textView(R.string.name)
}

Your Feedback is Welcome

Anko is licensed under Apache Licence 2.0, and the project is available on Github.
Your feedback and pull requests are welcome!

Comments below can no longer be edited.

15 Responses to Anko 0.6 is Released

  1. Salomon BRYS says:

    April 30, 2015

    That’s great news 🙂 Thanks for this amazing work !

    One thing I’d love to see in Anko is RxJava’s integration. The same way that you auto generate *Listener builder methods, you could generate *Observable methods. Then, to listen to OnTextChanged events with something like view.textChangedObservable() .filter { it is OnTextChanged } .subscribe {}.
    I believe being able to have all listeners as Observable would be a very powerful feature. We could write all such Observables by hand, as RxKotlin-Framework has started to, but you guys have developped what is needed to automatically generate those ;).

    • Yan Zhulanow says:

      May 2, 2015

      That’s a good idea! I don’t know though if it should be in the core Anko package, but we will definitely think about supporting RxJava. Thank you!

  2. QuangSon says:

    April 30, 2015

    I go to blog day by day to see news. Today I very support for new features of Anko.
    Hope Kotlin comunity will larger and larger 🙂

  3. QuangSon says:

    May 1, 2015

    support -> surprise (sorry, I can’t edit comment).

  4. Yoav Sternberg says:

    May 1, 2015

    Hi, the dsl preview plugin do not work for me on 1.2. It shows white screen.

    • Yan Zhulanow says:

      May 2, 2015

      For what kind of class you want to generate a preview? appcompat-v7 (ActionBarActivity) is not supported yet.

      • Yoav Sternberg says:

        May 2, 2015

        Thank you for telling. Hope it will be supported soon, since AppCompatActivity is absolutely the new standard for Android development.

  5. Michel Alexandre Salim says:

    May 1, 2015

    Great news! Any idea when the DSL preview plugin will receive a corresponding update?

    • Yan Zhulanow says:

      May 2, 2015

      It should be available now.

  6. Dmitriy F says:

    May 2, 2015

    I don’t buy the idea of composing views programmatically.
    Type-safety, Null-safety with views are not that huge to worry about imo. Declarations about standard view system not allowing code reuse are not true – you can always come up with custom View class and use it in xml.
    On the other hand, programmatic approach does not provide quick response. So, you are either forced to cut and paste code into a custom design previewer or launch app on device (which is not always convenient or even possible).
    It also makes case for indilegent programmers mixing logic with view construction resulting in messy code. For these reasons I think you could decompose library into smaller chunks.

    As for code simplification, I think it is always great.
    I would also love to see Rx integration similar to the RxAndroid project. Rx seems to be a valuable toolbox and integrating it (making concise interfaces) would definitely benefit programmers like me.
    Or even cooler: you could implement WPF-like data binding and come up with MVVM architecture for Android.

    Bottom line: it’s cool that you’re trying out new things and I’m definitely looking forward Kotlin in + Android (I think it would take another year or maybe 2 for language to be released and practices to emerge). Please, consider separating Anko functionality into multiple modules.
    Thanks!

  7. Android-Apps mit Kotlin: Anko 0.6 erschienen - jaxenter.de says:

    May 4, 2015

    […] das Schreiben von sauberem Code zu gewährleisten. Wie die Entwickler auf JetBrains’ Kotlin-Blog mitteilen, soll die neue Version von Anko klarer strukturiert und leichter zu bedienen sein. Durch […]

  8. jeff zhu says:

    July 7, 2015

    IncompatibleClassChangeError: Found class org.jetbrains.kotlin.psi.JetClassOrObject, but interface was expected

    android studio 141.2024585
    Anko DSL Preview 0.6.2

    • Yan Zhulanow says:

      July 15, 2015

      Anko 0.6.3 plugin is available. If the error is still present in it, please let us know.

  9. Stanley Au says:

    July 18, 2015

    How can we go about setting layoutParams on the children of a custom layout?
    e.g. Android Design Support Library’s CoordinatorLayout

    customView<CoordinatorLayout> {
    customView<FloatingActionButton> {
    id = ID_FAB
    onClick {
    Snackbar.make(it, “Hello Snackbar”, Snackbar.LENGTH_LONG).show()
    }
    }.layoutParams() { // This doesn’t work!
    margin = dip(16)
    gravity = Gravity.BOTTOM or Gravity.END
    }
    }

    • Yan Zhulanow says:

      July 23, 2015

      Additional Android support libraries will be fully supported in the new version of Anko.
      For now you can apply LayoutParams manually by setting View.layoutParams extension property.