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

Kotlin Java interop and Kotlin Migration

A6c0dcba5f373b33df2c2c55540faab1?s=47 Hadi Tok
December 07, 2019

Kotlin Java interop and Kotlin Migration

A6c0dcba5f373b33df2c2c55540faab1?s=128

Hadi Tok

December 07, 2019
Tweet

Transcript

  1. KOTLIN'E GEÇIŞ VE JAVA ILE BIRLIKTE KULLANIMI

  2. BEN KIMIM ➤ Lead Android Dev @CizitzenMe ➤ https://haditok.com

  3. KOTLIN ➤ 2011 Yılında Jetbrains tarafından duyuruldu ➤ 2017’de Google

    Android Resmi dil olarak duyuruldu
  4. KOTLIN ➤ http://kotlinlang.org/docs/reference/ ➤ https://try.kotlinlang.org/ ➤ https://android.github.io/kotlin-guides/interop.html

  5. SOURCE CODE ➤ https://github.com/oshamahue/KotlinInteropCases

  6. KOTLIN KONFIGÜRASYONU ➤ Kotlin’i aktive etme ➤ Tools > Kotlin

    > Configure Kotlin in Project ➤ Gradle dependency annotationProcessor -> kapt ➤ Kotlin Interop Lint check ➤ Preferences> Editor> Inspections ➤ Android>Lint > Interoperability>Kotlin Interoperability
  7. JAVA KODUNU KOTLINE ÇEVIRME

  8. GETTER SETTER

  9. GETTER SETTER JAVA private String javaString = "non final string";

    public String getJavaString() { return javaString; } public void setJavaString(String javaString) { this.javaString = javaString; }
  10. GETTER SETTER KOTLIN val kotlinValString = "Value"

  11. GETTER SETTER KOTLIN TO JAVA val string = javaToKotlin.javaString javaToKotlin.javaString

    = "New variable"
  12. GETTER SETTER JAVA TO KOTLIN String var = kotlinToJava.getKotlinVarString(); kotlinToJava.setKotlinVarString("New

    var");
  13. NULL SAFETY

  14. KOTLIN NULL SAFETY var nullableString: String? = null var notNullableString:

    String = "foo"
  15. JAVA NULL SAFETY private String nullableString = null; public String

    getNullableString() { return nullableString; } public void setNullableString(String nullableString) { this.nullableString = nullableString; }
  16. JAVA TO KOTLIN NULL SAFETY val nullableLength: Int = javaToKotlin.nullableString.length

    val notNullableLength: Int = javaToKotlin.notNullableString.length
  17. JAVA TO KOTLIN NULL SAFETY private String nullableString = null;

    public String getNullableString() { return nullableString; } public void setNullableString(String nullableString) { this.nullableString = nullableString; }
  18. JAVA TO KOTLIN NULL SAFETY @Nullable private String nullableString =

    null; @Nullable public String getNullableString() { return nullableString; } public void setNullableString(@Nullable String nullableString) { this.nullableString = nullableString; }
  19. JAVA TO KOTLIN NULL SAFETY @NotNull private String notNullableString =

    "bar"; @NotNull public String getNotNullableString() { return notNullableString; } public void setNotNullableString(@NotNull String notNullableString) { this.notNullableString = notNullableString; }
  20. JAVA TO KOTLIN NULL SAFETY val nullableLength: Int = javaToKotlin.nullableString.length

    val notNullableLength: Int = javaToKotlin.notNullableString.length
  21. KOTLIN NULLABLE TYPE val nullableLength: Int? = javaToKotlin.nullableString.length val notNullableLength:

    Int = javaToKotlin.notNullableString.length
  22. KOTLIN SAFE CALL OPERATOR val nullableLength: Int = javaToKotlin.nullableString ?.length

  23. KOTLIN ELVIS OPERATOR val nullableLength: Int = javaToKotlin.nullableString ?.length ?:

    0
  24. KOTLIN !! OPERATOR val nullableLength: Int = javaToKotlin.nullableString ?.length !!

  25. STATIC VS COMPANION OBJECT

  26. JAVA STATIC public static String javaStaticString = "myJavaStaticString";

  27. JAVA STATIC String myString = JavaToKotlin.javaStaticString;

  28. KOTLIN TO JAVA STATIC val myJavaStaticString = JavaToKotlin.javaStaticString

  29. KOTLIN TO JAVA STATIC class KotlinToJava { companion object {

    val companionString = "companion string" fun companionFunction(): String { return "companion function" } } }
  30. KOTLIN TO JAVA STATIC val companionValString = KotlinToJava.companionString val companionFunString

    = KotlinToJava.companionFunction()
  31. JAVA TO KOTLIN COMPANION OBJECT companionValString = KotlinToJava.Companion.getCompanionString(); companionFunString =

    KotlinToJava.Companion.companionFunction();
  32. KOTLIN TO JAVA STATIC class KotlinToJava { companion object {

    val companionString = "companion string" fun companionFunction(): String { return "companion function" } } }
  33. KOTLIN TO JAVA STATIC class KotlinToJava { companion object {

    @JvmStatic val companionString = "companion string" @JvmStatic fun companionFunction(): String { return "companion function" } } }
  34. JAVA TO KOTLIN COMPANION OBJECT companionValString = KotlinToJava.Companion.getCompanionString(); companionFunString =

    KotlinToJava.Companion.companionFunction();
  35. JAVA TO KOTLIN COMPANION OBJECT companionValString = KotlinToJava.getCompanionString(); companionFunString =

    KotlinToJava.companionFunction();
  36. PACKAGE LEVEL MEMBERS

  37. PACKAGE LEVEL MEMBERS const val packageLevelConstant = "kotlinToJava" fun packageLevelFunction():

    String { return "this is a package level function" }
  38. PACKAGE LEVEL MEMBERS import com.oshamahue.kotlindemo.packageLevelConstant import com.oshamahue.kotlindemo.packageLevelFunction fun otherPackageLevelFunction() {

    var string = packageLevelFunction() string = packageLevelConstant }
  39. JAVA TO KOTLIN PACKAGE LEVEL MEMBERS String string = KotlinToJavaKt.packageLevelFunction();

    string = KotlinToJavaKt.packageLevelConstant;
  40. JAVA TO KOTLIN PACKAGE LEVEL MEMBERS @file:JvmName("KotlinToJavaUtil") package com.oshamahue.kotlindemo import

    java.io.IOException . . . .
  41. JAVA TO KOTLIN PACKAGE LEVEL MEMBERS String string = KotlinToJavaUtil.packageLevelFunction();

    string = KotlinToJavaUtil.packageLevelConstant;
  42. HIGHER ORDER FUNCTIONS

  43. HIGHER ORDER FUNCTIONS ➤ Bir fonksiyon döndüren ➤ Parametre olarak

    fonksiyon alabilen ➤ Temelde bir fonksiyona bir değişken olarak davranabilme
  44. HIGHER ORDER FUNCTIONS fun functionPass(func: (String) -> Unit) { func("myString")

    //invoking a function }
  45. HIGHER ORDER FUNCTIONS functionPass(this ::nullables)

  46. HIGHER ORDER FUNCTIONS val func = { println("my function") }

  47. HIGHER ORDER FUNCTIONS fun functionReturn(): () -> Unit { val

    func = { println("my function") } //function creation return func }
  48. JAVA TO KOTLIN HIGHER ORDER FUNCTIONS fun functionPass(func: (String) ->

    Unit) { func("myString") //invoking a function }
  49. JAVA TO KOTLIN HIGHER ORDER FUNCTIONS kotlinToJava.functionPass(new Function1<String,Unit>() { @Override

    public Unit invoke(String string) { return null; } });
  50. JAVA TO KOTLIN HIGHER ORDER FUNCTIONS fun functionReturn(): () ->

    Unit { val func = { println("my function") } //function creation return func }
  51. JAVA TO KOTLIN HIGHER ORDER FUNCTIONS Function0<Unit> func = kotlinToJava.functionReturn();

    func.invoke();
  52. EXTENSIONS

  53. EXTENSIONS ➤ Hali hazırda bulunan Class’lara kalıtım(inheritance) olmadan yeni fonksiyon

    ya da değişken eklemeyi sağlar ➤ Herhangi bir class olabilir ➤ Static olarak derlenir
  54. EXTENSION FUNCTIONS fun String.getThridLetter(): Char { if (this.length > 2)

    { return this.toCharArray()[2] } else throw RuntimeException("Length should be at least 3 ") }
  55. EXTENSION FUNCTIONS val thridLetter = "foo".getThridLetter()

  56. JAVA TO KOTLIN EXTENSION FUNCTIONS char thirdLetter = KotlinToJavaUtil.getThridLetter("foo");

  57. TEŞEKKÜRLER HTTPS://MOCKK.IO/