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

Kotlin for Android - A first approach

Kotlin for Android - A first approach

A first approach to Android Development using Kotlin as a main language. This presentation was part of the first Kotlin Night Meetup held in Vancouver, Canada - https://www.meetup.com/es-ES/VancouverKotlin/events/238810235/

Carlos Daniel

May 23, 2017
Tweet

More Decks by Carlos Daniel

Other Decks in Programming

Transcript

  1. Kotlin for Android A first approach Carlos Daniel Munoz -

    Android Dev @cdmunozi [email protected] Co-Founder & Co-Organizer @MedellinAndroid May 23 VANCOUVER’S KOTLIN NIGTH #1
  2. 5 Java in Android • Java 6.5 (Dic. 2006) •

    Some hard to avoid limitations: Compatibility with past versions of Android
  3. 6 Java in Android • Java 6.5 (Dic. 2006) •

    Some hard to avoid limitations: Compatibility with past versions of Android • “Compatibility” with some Java 8 features ◦ Virtual Machine Java 6 :( ◦ Not ready to support Java 8
  4. 7 Java in Android • Java 6.5 (Dic. 2006) •

    Some hard to avoid limitations: Compatibility with past versions of Android • “Compatibility” with some Java 8 features ◦ Virtual Machine Java 6 :( ◦ Not ready to support Java 8 • Java 8 is limited, compared to other modern languages
  5. 12 Problems with Java • Immutability not well implemented ◦

    Variables can be modified easily ◦ “Race condition” → who is modifying my variable? testing?
  6. 13 Problems with Java • Immutability not well implemented ◦

    Variables can be modified easily ◦ “Race condition” → who is modifying my variable? testing? ◦ We can define everything “final”, but it’s hard to use.
  7. 14 Problems with Java • Immutability not well implemented ◦

    Variables can be modified easily ◦ “Race condition” → who is modifying my variable? testing? ◦ We can define everything “final”, but it’s hard to use. ◦ Java framework and libraries use to be mutable. (i.e: Collections and Collections Utils)
  8. 16 Problems with Java • First class citizen functions ◦

    Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type
  9. 17 Problems with Java • First class citizen functions ◦

    Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type ◦ Functional programming ◦ Listeners ◦ Callbacks
  10. 18 Problems with Java • First class citizen functions ◦

    Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type ◦ Functional programming ◦ Listeners ◦ Callbacks
  11. 19 Problems with Java • First class citizen functions ◦

    Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type ◦ Functional programming ◦ Listeners ◦ Callbacks
  12. 24 Problems with Java • It’s complex to implement a

    safe singleton • Object means, class with an unique implementation → Singleton
  13. 26 Problems with Java • Is Java obsolete? ◦ Lambdas

    ◦ Functional programming ◦ Sealed classes
  14. 27 Problems with Java • Is Java obsolete? ◦ Lambdas

    ◦ Functional programming ◦ Sealed classes • With Kotlin, can be used without third libraries and even using Java 6’s VM
  15. 28 Problems with Java • Is Java obsolete? ◦ Lambdas

    ◦ Functional programming ◦ Sealed classes • With Kotlin, can be used without third libraries and even using Java 6’s VM • 100% interoperable with Java
  16. 29 Problems with Java • Is Java obsolete? ◦ Lambdas

    ◦ Functional programming ◦ Sealed classes • With Kotlin, can be used without third libraries and even using Java 6’s VM • 100% interoperable with Java • Supported by Google for Android
  17. 32 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin
  18. 33 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin • Lambda expressions
  19. 34 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin • Lambda expressions • Inline functions (A new object is not created, the code is replaced at runtime)
  20. 35 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin • Lambda expressions • Inline functions (A new object is not created, the code is replaced at runtime) • Null safety
  21. 36 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin • Lambda expressions • Inline functions (A new object is not created, the code is replaced at runtime) • Null safety • Smart cast
  22. 37 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin • Lambda expressions • Inline functions (A new object is not created, the code is replaced at runtime) • Null safety • Smart cast • String templates
  23. 38 Using Kotlin • Android Studio • Plugin Kotlin ◦

    Projects from scratch ◦ Java projects with some Kotlin • Lambda expressions • Inline functions (A new object is not created, the code is replaced at runtime) • Null safety • Smart cast • String templates • Primary constructors
  24. 40 Using Kotlin • Interfaces with code • Needless: ◦

    Semicolon ◦ “new” ◦ Types definitions
  25. 41 Kotlin Updates • Google IO 2017 → Google Supports

    Kotlin for Android Dev REFERENCES: • Android Developers: https://developer.android.com/kotlin/resources.html • Kotlin Online https://try.kotlinlang.org • Code Labs Google: https://codelabs.developers.google.com/?cat=Android
  26. 45 Kotlin project in Android step by step • Create

    project in the usual way • Browse the plugins (Jetbrains) and install Kotlin plugin
  27. 46 Kotlin project in Android step by step • Create

    project in the usual way • Browse the plugins (Jetbrains) and install Kotlin plugin • Restart the IDE
  28. 47 Kotlin project in Android step by step • Create

    project in the usual way • Browse the plugins (Jetbrains) and install Kotlin plugin • Restart the IDE • Open the MainActivity ◦ Code - Convert Java File to Kotlin File ◦ Magic!
  29. 48 Kotlin project in Android step by step • Create

    project in the usual way • Browse the plugins (Jetbrains) and install Kotlin plugin • Restart the IDE • Open the MainActivity ◦ Code - Convert Java File to Kotlin File ◦ Magic! • Tools → Kotlin → Configure Kotlin in project → Version ◦ Project full modified (including build.gradle - both, app and project)
  30. 49 Kotlin project in Android step by step • Create

    project in the usual way • Browse the plugins (Jetbrains) and install Kotlin plugin • Restart the IDE • Open the MainActivity ◦ Code - Convert Java File to Kotlin File ◦ Magic! • Tools → Kotlin → Configure Kotlin in project → Version ◦ Project full modified (including build.gradle - both, app and project) • App build.gradle (add android extension sub-plugin): to access easily view’s elements from Activity ◦ apply plugin: ‘kotlin-android-extension’
  31. 50 A Kotlin Activity • No public reserved word, in

    Kotlin everything is public • Inheritance • Override as a reserved word rather than an annotation • In function: first name, then the type • Null safety with “?”
  32. 51 Activity and On-Click listener in Kotlin * Taken from

    https://developer.android.com/kotlin/get-started.html