Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin-ize your Android Development

Kotlin-ize your Android Development

Introduction to Kotlin for Android, it's features, issues and some amazing stuff.

Akshay Chordiya

April 15, 2017
Tweet

More Decks by Akshay Chordiya

Other Decks in Technology

Transcript

  1. • Why another language? • Problems with Java - Android

    • What’s Kotlin • Features • Issues • And finally… questions Agenda
  2. Problems with Java -- Android • Stuck at Java 6-7

    • Restrictions to add functionality to Platform APIs • Android loves Inheritance • Lots of boilerplate • Ceremony of APIs • Android loves to throw nulls • Need of Modern coding style • And so much more….
  3. Kotlin • Modern & Statically typed • Targets JVM, Android

    and JavaScript • Works everywhere where Java works • 100% interoperable with Java™ • Combines Object Oriented and Functional features • Made by good folks at JetBrains • Open source
  4. // String (Implicitly inferred) val spell = "Lumos" // Int

    val number = 74 // Explicit type var spells: List<String> = ArrayList() Variables // String (Implicitly inferred) val spell = "Lumos" // Int val number = 74 // Explicit type var spells: List<String> = new ArrayList()
  5. “Use val for a value that won’t change” - Immutable

    val vs var “Use var for something that will vary with time.” - Mutable
  6. fun add(a: Int, b: Int): Int { return a +

    b } Functions fun add(a: Int, b: Int) = a + b fun add(a: Int = 0, b: Int = 0) = a + b
  7. // Compile time error val spell: String = null //

    OK val spell: String? = null Declaring null variables
  8. // Compile time error var spell: String = "Lumos" spell

    = null // OK var spell: String? = "Lumos" spell = null Even...
  9. // Compile time error val spell: String? = null val

    length = spell.length // OK val spell: String? = null if (spell != null) { // Smart cast val length = spell.length } Null Safety
  10. // Safe val spell: String? = null val length =

    spell?.length // Safe with else val length = spell?.length ?: -1 Kotlin-ized way
  11. String interpolation fun sayHello(message: String) { println("Welcome $message") } fun

    sayTime() { println("Time: ${System.currentTimeMillis()}") }
  12. fun consume(f: () -> Unit): Boolean { f() return true

    } Higher Order Functions inline fun consume(f: () -> Unit): Boolean { f() return true }
  13. fun View.visible() { visibility = VISIBLE } fun View.visible() {

    visibility = VISIBLE } fun String.isEmail(): Boolean { return Patterns.EMAIL_ADDRESS.matcher(this).matches() } fun String.isEmail(): Boolean { return Patterns.EMAIL_ADDRESS.matcher(this).matches() } Example: Extension functions
  14. val email = "[email protected]" if (email.isEmail()) { // Do login

    } Usage: Extension Function textView.visible()
  15. inline fun SharedPreferences. edit(func: SharedPreferences.Editor.() -> Unit) { val editor

    = edit() editor.func() editor.apply() } inline fun SharedPreferences. edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() } Extension + Higher Order functions // Usage val pref = PreferenceManager.getDefaultSharedPreferences(context) pref.edit{ putString("first_name", "Akshay") putString("last_name", "Chordiya") remove("last_name") }
  16. Smart Cast // Java way if (view instanceof TextView) {

    ((TextView) view).setText("Avifors"); } // Kotlin way if (view is TextView) { // Smart casting view.text = "Avifors" }
  17. with function with(textView) { text = "Accio" visible() } val

    textView = with(TextView(this)) { text = "Accio" visible() this }
  18. when expression override fun onOptionsItemSelected (item: MenuItem) = when (item.itemId)

    { R.id.action_about -> consume { startActivity(Intent( baseContext, AboutActivity:: class.java)) } else -> super.onOptionsItemSelected(item) }
  19. Issues • Slightly increased build time • Adds Dex Count

    (6K) to your app • No static methods and variables. The same functionality can be achieved using companion objects • Difficulty in coding in functional paradigm
  20. References • All the images and logos used are trademarks

    of respective companies. • Kotlin Official Website • Why Kotlin-ize your Android Development Medium - Akshay Chordiya • Antonio Leiva Blog • Why You Should Start Using Kotlin to Supercharge Your Android Development in 2017 • Android Development with Kotlin — Jake Wharton • Kotlin - Android document by Square • Droidcon NYC 2016 - Kotlin in Production • Being more productive with Kotlin