How Java and Kotlin work together @ca.kt#2

How Java and Kotlin work together @ca.kt#2

A374f41eab3f73c50d8bab0652cb207a?s=128

TakuSemba

July 19, 2017
Tweet

Transcript

  1. How Java and Kotlin work together Taku Semba CyberAgent.Inc

  2. About Me Taku Semba TakuSemba @takusemba AbemaTV.Inc

  3. Call Kotlin from Java

  4. //kotlin class User { var name: String = "" }

  5. //java User user = new User(); user.setName("taku"); user.getName(); // taku

    //kotlin class User { var name: String = "" }
  6. //java User user = new User(); user.setName("taku"); user.getName(); // taku

    //kotlin class User { val name: String = "taku" }
  7. //java User user = new User(); user.getName(); // taku //kotlin

    class User { val name: String = "taku" }
  8. //kotlin class User { val name: String = "taku" fun

    getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  9. //kotlin class User { val name: String = "taku" fun

    getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  10. //kotlin class User { @JvmField val name: String = "taku"

    fun getName(): String { return "my name is $name" } } //java User user = new User(); user.getName(); // taku
  11. //kotlin class User { @JvmField val name: String = "taku"

    fun getName(): String { return "my name is $name" } } //java User user = new User(); String name = user.name; // taku user.getName(); // my name is taku
  12. //kotlin class User { @JvmField val name: String = "taku"

    fun getName(name: String = "semba"): String { return "my name is $name" } } //java User user = new User(); String name = user.name; // taku user.getName(); // my name is taku
  13. //kotlin class User { @JvmField val name: String = "taku"

    @JvmOverloads fun getName(name: String = "semba"): String { return "my name is $name" } } //java User user = new User(); String name = user.name; // taku user.getName(); // my name is semba
  14. //kotlin class User { companion object { fun callName(name: String):

    String { return "hi, $name" } } //java User.Companion.callName("taku");
  15. //kotlin class User { companion object { @JvmStatic fun callName(name:

    String): String { return "hi, $name" } } //java User.Companion.callName("taku");
  16. //kotlin class User { companion object { @JvmStatic fun callName(name:

    String): String { return "hi, $name" } } //java User.callName("taku");
  17. //kotlin class User { companion object { val name: String

    = "taku" @JvmStatic fun callName(name: String): String { return "hi, $name" } } //java User.callName("taku"); String name = User.Companion.getName();
  18. //kotlin class User { companion object { const val name:

    String = "taku" @JvmStatic fun callName(name: String): String { return "hi, $name" } } //java User.callName("taku"); String name = User.Companion.getName();
  19. //kotlin class User { companion object { const val name:

    String = "taku" @JvmStatic fun callName(name: String): String { return "hi, $name" } } //java User.callName("taku"); String name = User.name;
  20. //prefExt.kt fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw

    IllegalStateException(“user id does not exist“) }
  21. //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) val userId = sp.getUserId() //prefExt.kt

    fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw IllegalStateException(“user id does not exist“) }
  22. //prefExt.kt fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw

    IllegalStateException(“user id does not exist“) }
  23. //prefExt.kt fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw

    IllegalStateException(“user id does not exist“) } //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); String userId = PrefExtKt.getUserId(sp);
  24. //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); String userId = PrefExtKt.getUserId(sp); //prefExt.kt

    @file:JvmName("PrefUtil") fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw IllegalStateException(“user id does not exist“) }
  25. //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); String userId = PrefUtil.getUserId(sp); //prefExt.kt

    @file:JvmName("PrefUtil") fun SharedPreferences.getUserId(): String { return getString("userId", null) ?: throw IllegalStateException(“user id does not exist“) }
  26. //prefExt.kt fun SharedPreferences.edit(func: (SharedPreferences.Editor) -> Unit) { val editor =

    edit() func(editor) editor.apply() }
  27. //prefExt.kt fun SharedPreferences.edit(func: (SharedPreferences.Editor) -> Unit) { val editor =

    edit() func(editor) editor.apply() } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { editor -> editor.putString("key", "value") }
  28. //prefExt.kt fun SharedPreferences.edit(func: (SharedPreferences.Editor) -> Unit) { val editor =

    edit() func(editor) editor.apply() } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { it.putString("key", "value") }
  29. //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { it.putString("key", "value") }

    //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() }
  30. //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) sp.edit { putString("key", "value") }

    //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() }
  31. //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor =

    edit() editor.func() editor.apply() }
  32. //prefExt.kt fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor =

    edit() editor.func() editor.apply() } //MainActivity.java PrefExtKt.edit(sp, editor -> { editor.putString("key", "value"); return Unit.INSTANCE; });
  33. //prefExt.kt inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) { val editor

    = edit() editor.func() editor.apply() } //MainActivity.java PrefExtKt.edit(sp, editor -> { editor.putString("key", "value"); return Unit.INSTANCE; });
  34. //prefExt.kt

  35. //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") }
  36. //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) val a= sp.get("key", 0L) // return as Long val b = sp.get("key", "my default") // return as String
  37. //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") }
  38. //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); PrefExtKt.get(sp, "key", 0L);
  39. //prefExt.kt inline fun <reified T> SharedPreferences.get(key: String, default: T): T

    { if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.java SharedPreferences sp = getPreferences(Context.MODE_PRIVATE); PrefExtKt.get(sp, "key", 0L); // get() has a private access in PrefExtKt
  40. How Java and Kotlin work together https://github.com/takusemba https://twitter.com/takusemba