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

Getting familiar with Kotlin

Getting familiar with Kotlin

Introductory talk about Kotlin for Android. In this talk my goal is to teach you something about Kotlin and show how you can use Kotlin to build robust and less crashable apps while having fun developing.

Akshay Chordiya

August 27, 2017
Tweet

More Decks by Akshay Chordiya

Other Decks in Programming

Transcript

  1. +AkshayChordiya • Entrepreneur • Android Developer by heart • Active

    community speaker • Graduated in 2016 Akshay Chordiya @Akshay_Chordiya
  2. Agenda • What’s Kotlin? • Syntax • Features • Live

    Demos • Issues • What’s next? • And finally… questions
  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
  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. // Kotlin mButton.setOnClickListener { // Your logic } Lambda Expressions

    // Before Java 8 mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Your logic } });
  12. Usage: Higher Order Functions // We want fragmentManager.load { it.replace(R.id.home_content,

    MyFragment.newInstance()) } // Old way val trans = fragmentManager.beginTransaction() trans.replace(R.id.home_content, MyFragment.newInstance()) trans.commit()
  13. Code: Higher Order Functions fun FragmentManager.load(f: (FragmentTransaction) -> Unit) {

    val transaction = beginTransaction() f(transaction) transaction.commit() } inline fun FragmentManager.load(f: (FragmentTransaction) -> Unit) { val transaction = beginTransaction() f(transaction) transaction.commit() }
  14. Usage: Extension functions with receiver // Previously fragmentManager.load { it.replace(R.id.home_content,

    MyFragment.newInstance()) } // We want fragmentManager.load { replace(R.id.home_content, MyFragment.newInstance()) }
  15. Code: Extension functions with receiver inline fun FragmentManager.load(f: FragmentTransaction.() ->

    Unit) { val transaction = beginTransaction() transaction.f() transaction.commit() }
  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. String interpolation fun sayHello(message: String) { println("Welcome $message") } fun

    sayTime() { println("Time: ${System.currentTimeMillis()}") }
  18. measureTimeMillis function // Java long startTime = System.currentTimeMillis(); // Do

    your task long executionTime = System.currentTimeMillis() - startTime; System.out.println("Execution Time = " + executionTime + "ms"); // Kotlin val executionTime = measureTimeMillis { // Do your task } println("Execution Time = $executionTime ms")
  19. when (x) { in 1..10 -> print("x is in") !in

    10..20 -> print("x is outside") else -> print("To infinity & beyond") } when expression when { x.isOdd() -> print("x is Odd") x.isEven() -> print("x is Even") else -> print("x is different") }
  20. 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
  21. #ProTips • Always remember with Kotlin there is always gonna

    be a better way to do something • Use Kotlin to Java converter to convert Java files to Kotlin (Shortcut: Ctrl + Alt + Shift + K) • You can begin writing test cases or data classes for your models to slowly start adding Kotlin to your project
  22. References • All the images and logos used are trademarks

    of respective companies. • Kotlin Official Website • Kotlin and Android • Why Kotlin-ize your Android Development Medium - Akshay Chordiya • Antonio Leiva Blog • Android Development with Kotlin — Jake Wharton • Easter Functions in Kotlin • Being more productive with Kotlin • Kotlin - Goodbye findViewById