Android KTX (HWSW free! 34)

Android KTX (HWSW free! 34)

A lightning talk showing off some of the syntax gains that Google’s Android KTX library offers.

Talk recording (Hungarian): https://www.youtube.com/watch?v=R-oIo4A02CU

4047c64e3a1e2f81addd4ba675ddc451?s=128

Marton Braun

June 19, 2018
Tweet

Transcript

  1. 2.

    Android APIs are sometimes hard to use Kotlin allows us

    to reshape existing API Extension functions and properties Named and default parameters Operator overloading Lambdas, higher order functions, and inline functions Everyone has their own collection of utilities…
  2. 3.

    Google takes a stance, creates their own First appeared Feb

    2, 2018 Goals Only provide uniquely Kotlin APIs Don’t provide new implementation, just new syntax Minimize overhead, focus on zero overhead abstractions
  3. 4.
  4. 8.

    Higher order, inline extension functions for (i in 0 until

    linearLayout.childCount) { val view = linearLayout.get(i) Timber.d(view.tag) } linearLayout.forEach { view -> Timber.d(view.tag) }
  5. 10.

    Operator overloading val color = bitmap.getPixel(100, 50) val color =

    bitmap[100, 50] bitmap.setPixel(100, 50, color) bitmap[100, 50] = color
  6. 15.

    inline fun <T> SQLiteDatabase.transaction(...): T { beginTransaction() try { val

    result = body() setTransactionSuccessful() return result } finally { endTransaction() } }
  7. 17.

    Explore Android KTX yourself and use it in your projects

    https://github.com/android/android-ktx/ https://android.github.io/android-ktx/core-ktx/ Write your own extensions for APIs that don’t fit your needs If these are on Android, consider submitting a PR to Android KTX