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

A Taste of Kotlin on Android (Google I/O Extended 2017 Budapest)

A Taste of Kotlin on Android (Google I/O Extended 2017 Budapest)

An introductory talk on Kotlin prepared right before Google made it an official language on Android. A brief history of where the language comes from, what benefits it offers, and many examples of its syntax and features.

Talk recording (Hungarian): https://youtu.be/cRyi5crJRDU?t=31m33s

Marton Braun

May 18, 2017
Tweet

More Decks by Marton Braun

Other Decks in Programming

Transcript

  1. Ízelítő a Kotlin nyelvű
    Android fejlesztésből
    Braun Márton Szabolcs
    zsmb13
    [email protected] zsmb13

    View full-size slide

  2. Háttér
    • JetBrains
     2010: fejlesztés kezdete
     2016 február: Kotlin 1.0
    • JVM nyelv
     Java 6-os bytecode-ra fordul
     100%-os együttműködés Java kóddal

    View full-size slide

  3. • A megszokott környezetben használható
    • Android Studio plugin
    • 4 sor a Gradle build fájlokba
     Kis méretű runtime és stdlib (<1 MB)
    • Fájlonként adoptálható
     Új kód Kotlinban
     Meglévő Java kód refaktorálása
    Használat

    View full-size slide

  4. Kotlin gyorstalpaló

    View full-size slide

  5. Változók
    // Java
    int x = 1;
    final int x = 1;
    // Kotlin
    var x: Int = 1
    val x = 1

    View full-size slide

  6. Függvények
    fun add(a: Int, b: Int): Int = a + b
    fun add(a: Int, b: Int): Int {
    return a + b
    }
    fun add(a: Int, b: Int) = a + b

    View full-size slide

  7. Osztályok
    class Person(val name: String, var age: Int) {
    }

    View full-size slide

  8. Az osztály Java megfelelője
    public class Person {
    private final String name;
    private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    }

    View full-size slide

  9. Data class
    class Person(val name: String, var age: Int)
    data

    View full-size slide

  10. A data class Java megfelelője
    public class Person {
    private final String name;
    private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String toString() {
    return "Person{" +
    "name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass())
    return false;
    Person person = (Person) o;
    if (age != person.age) return false;
    return name != null ? name.equals(person.name)
    : person.name == null;
    }
    public int hashCode() {
    int result = name != null ? name.hashCode() : 0;
    result = 31 * result + age;
    return result;
    }
    }

    View full-size slide

  11. Data class - összefoglaló
    data class Person(
    val name: String,
    var age: Int)
    public class Person {
    private final String name;
    private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String toString() {
    return "Person{" +
    "name='" + name + '\'' +
    ", age=" + age +
    '}';
    }
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Person person = (Person) o;
    if (age != person.age) return false;
    return name != null ? name.equals(person.name) : person.name == null;
    }
    public int hashCode() {
    int result = name != null ? name.hashCode() : 0;
    result = 31 * result + age;
    return result;
    }
    }

    View full-size slide

  12. Property hozzáférések
    data class Person(val name: String, var age: Int)
    // Kotlin
    val person = Person("Sam", 41)
    println(person.name)
    person.age = 42

    View full-size slide

  13. Property hozzáférések
    data class Person(val name: String, var age: Int)
    // Java
    Person person = new Person("Sam", 41);
    System.out.println(person.getName());
    person.setAge(42);

    View full-size slide

  14. Property hozzáférések
    // Kotlin
    recyclerView.adapter = adapter
    recyclerView.isDrawingCacheEnabled = true
    recyclerView.drawingCacheQuality = QUALITY_HIGH
    // Java
    recyclerView.setAdapter(adapter);
    recyclerView.setDrawingCacheEnabled(true);
    recyclerView.setDrawingCacheQuality( );
    QUALITY_HIGH

    View full-size slide

  15. Kotlin Android Extensions
    // MainActivity.kt
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    ...
    btn0.setText(R.string.btn0_label)
    btn0.setOnClickListener(...)
    }
    }
    // activity_main.xml

    View full-size slide

  16. Lambda kifejezések
    fun useNumber(number: Int, operation: (Int) -> Unit) {
    operation(number)
    }
    useNumber(5, { x -> println(x * 2) })
    useNumber(5) { x -> println(x * 2) }
    useNumber(5) { println(it * 2) }

    View full-size slide

  17. SAM conversion
    public interface OnClickListener {
    void onClick(View v);
    }

    View full-size slide

  18. SAM conversion
    btnLogin.setOnClickListener(
    object : View.OnClickListener {
    override fun onClick(v: View) {
    presenter.startLogin()
    }
    }
    )

    View full-size slide

  19. SAM conversion
    btnLogin.setOnClickListener {
    presenter.startLogin()
    }
    btnLogin.setOnClickListener(
    object : View.OnClickListener {
    override fun onClick(v: View) {
    presenter.startLogin()
    }
    }
    )

    View full-size slide

  20. button.setVisibility(View.GONE)
    Extension function
    button.visibility = View.GONE
    button.hide()
    fun View.hide() {
    this.visibility = View.GONE
    }

    View full-size slide

  21. Lambda + extension function
    fun useRandom(actions: Random.() -> Unit) {
    val random = Random()
    random.actions()
    }
    useRandom {
    this.nextInt()
    nextDouble()
    }

    View full-size slide

  22. android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    Deklaratív hierarchia leírás

    View full-size slide

  23. Imperatív hierarchia építés
    LinearLayout linearLayout = new LinearLayout(this);
    EditText editText = new EditText(this);
    Button button = new Button(this);
    button.setOnClickListener(...);
    linearLayout.addView(editText);
    linearLayout.addView(button);
    setContentView(linearLayout);

    View full-size slide

  24. Anko View DSL
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    verticalLayout {
    val name = editText()
    button("Say Hello") {
    onClick {
    toast("Hello, ${name.text}!")
    }
    }
    }
    }
    https://github.com/Kotlin/anko

    View full-size slide

  25. Anko View DSL

    View full-size slide

  26. MaterialDrawer
    https://github.com/mikepenz/MaterialDrawer

    View full-size slide

  27. MaterialDrawerKt
    drawer {
    closeOnClick = true
    primaryItem("Home") {
    icon = R.drawable.ic_home
    }
    primaryItem("Settings") {
    icon = R.drawable.ic_settings
    }
    }
    https://github.com/zsmb13/MaterialDrawerKt

    View full-size slide

  28. • JVM
     Android
     Desktop
     Szerver
    • Gradle Script Kotlin
    • JavaScript (1.1 óta stabil)
     Kliens oldal
     Szerver oldal
    • (Native)
    Több, mint egy “jobb Java”
    android {
    buildToolsVersion("25.0.0")
    compileSdkVersion(23)
    defaultConfig {
    minSdkVersion(15)
    targetSdkVersion(23)
    applicationId = "com.ex.app"
    versionCode = 1
    versionName = "1.0"
    }
    }

    View full-size slide

  29. • kotlinlang.org
    Merre tovább?

    View full-size slide

  30. Merre tovább?
    • Kotlin – Ready for Production (Hadi Hariri)
     https://youtu.be/R0J_Jl7bKY8
    • Android Development with Kotlin (Jake Wharton)
     https://youtu.be/A2LukgT2mKc
    • Kotlin in Production (Christina Lee)
     https://youtu.be/mDpnc45WwlI
    • 10 Kotlin Tricks in 10(ish) Minutes (Jake Wharton)
     https://youtu.be/YKzUbeUtTak

    View full-size slide

  31. Merre tovább?
    • Using Project Kotlin for Android (Jake Wharton)
     https://docs.google.com/document/d/1ReS3ep-
    hjxWA8kZi0YqDbEhCqTt29hG8P44aA9W0DM8/preview
    • Why you should totally switch to Kotlin (Magnus Vinther)
     https://medium.com/@magnus.chatt/why-you-should-totally-
    switch-to-kotlin-c7bbde9e10d5
    • What do 17 Google Developers Experts for Android think
    about Kotlin? (Antonio Leiva)
     https://antonioleiva.com/google-kotlin/

    View full-size slide

  32. Merre tovább?
    • Kotlin on Android. Now official (JetBrains, Kotlin Blog)
     https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-
    now-official/
    • Kotlin and Android | Android Developers
     https://developer.android.com/kotlin/index.html
    • Kotlin on Android FAQ (Google)
     https://developer.android.com/kotlin/faq.html

    View full-size slide

  33. Merre tovább?
    • Introduction to Kotlin (Hadi Hariri, Andrey Breslav)
     Péntek (május 19.), 19:30-20:30
    • Life is great and everything will be ok, Kotlin is here
    (Christina Lee, Jake Wharton)
     Péntek (május 19.), 23:30-00:30

    View full-size slide

  34. Amire nem jutott idő…
    • Null safety
    • Collection manipulations
    • Inline and infix functions
    • Named and default parameters
    • Sealed class, when expression
    • Declaration site variance
    • Destructuring declarations
    • Operator overloading
    • Delegation
    • String templates
    • Sequences
    • Singletons
    • Coroutines
    • …

    View full-size slide

  35. Köszönöm a figyelmet!
    zsmb13
    [email protected] zsmb13
    Fotó:
    Alexey Sergeev

    View full-size slide