Slide 1

Slide 1 text

Smoother Android development with Kotlin Márton Braun • AutSoft • [email protected]

Slide 2

Slide 2 text

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…

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Extension properties view.setVisibility(View.VISIBLE) view.visibility = View.VISIBLE view.isVisible = true

Slide 6

Slide 6 text

Default and named parameters view.setPadding(20, view.topPadding, 20, view.bottomPadding) view.updatePadding(left = 20, right = 20)

Slide 7

Slide 7 text

Operator overloading and extension functions linearLayout.addView(view) linearLayout += view linearLayout.removeView(view) linearLayout -= view

Slide 8

Slide 8 text

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) }

Slide 9

Slide 9 text

Extension functions val bitmap = Bitmap.createBitmap( view.width, view.height, Bitmap.Config.ARGB_8888) view.draw(Canvas(bitmap)) val bitmap = view.toBitmap()

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Extension functions, default parameters Toast.makeText(this, "Hello world", Toast.LENGTH_SHORT) .show() toast("Hello world")

Slide 12

Slide 12 text

Extension functions, default parameters Toast.makeText(this, "Hello world", Toast.LENGTH_LONG) .show() toast("Hello world", duration = Toast.LENGTH_LONG)

Slide 13

Slide 13 text

Higher order, inline extension functions sharedPreferences.edit() .putBoolean("drawerOpened", true) .apply() sharedPreferences.edit { putBoolean("drawerOpened", true) }

Slide 14

Slide 14 text

Higher order, inline extension functions db.beginTransaction() db.insert("users", null, values) db.setTransactionSuccessful() db.endTransaction() db.transaction { insert("users", null, values) }

Slide 15

Slide 15 text

inline fun SQLiteDatabase.transaction(...): T { beginTransaction() try { val result = body() setTransactionSuccessful() return result } finally { endTransaction() } }

Slide 16

Slide 16 text

Destructuring val lat = location.latitude val lon = location.longitude val (lat, lon) = location

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Questions? Márton Braun • AutSoft • [email protected]