GDG Busan - 코틀린이 안드로이드 개발에 미친 영향

7589a5a8fec022e8af4e46525150a291?s=47 TaeHwan
July 06, 2019

GDG Busan - 코틀린이 안드로이드 개발에 미친 영향

2019 GDG Busan - Kotlin

7589a5a8fec022e8af4e46525150a291?s=128

TaeHwan

July 06, 2019
Tweet

Transcript

  1. ௏ౣܽ੉ উ٘۽੉٘ ѐߊী ޷஘ ৔ೱ ӂకജ ஠஠য়ಕ੉

  2. • KAKAOPAY উ٘۽੉٘ ౱ • Է ݆਷ ѐߊ੗о غ੗! ࠶۽Ӓ

    ਍৔ • https://thdev.tech • GDG Seoul ਍৔૓ • ٘۽੉٘ա੉எ ਍৔૓ • taehwan@thdev.net
  3. • Kotlin ঌইࠁӝ • Google Kotlin ૑ਗ • Coroutinesਸ ઑӘ

    ঌইࠁҊ • Kotlin ૑ਗ ۄ੉࠳۞ܻ ഛੋ • Coroutines ૑ਗ ۄ੉࠳۞ܻ ഛੋ द੘ೞӝ ੹ী
  4. ੷ח

  5. ਃӝਃী Kotlin ੸ਊ ੷ח 2011 2012 2016 2017 2018 v1.1

    ٸ ࠗఠ ҕࠗ ਬؘ޷ ъ੄ب ೠߣ ନҊ ড 40% Kotlin ੸ਊ Coroutines ୊਺ ੸ਊ 2019 Kotlin coroutines ݅ ࢎਊ
  6. • Java ௏٘۽ ੘ࢿೠ ௏٘ܳ Kotlinਵ۽ ߸ജ • RxJava, MVP

    ಁఢ ١ਸ ೠߣী ೟ण • ਬؘ޷ ъ੄ܳ ନਵݶࢲ ળ࠺ೠ ੗ܐ۽ ೟ण۱ ૐ؀ • ഥࢎীࢲ पઁ ੸ਊ • Kotlin/coroutines • пઙ ௏ౣܽ ҙ۲ ߊ಴ ೟ण ߑߨ
  7. • IO 2017੉ ՘աҊ ഥࢎী جইо ੸ਊೞѷ׮Ҋ ࠁҊ(যו੿ب ೟णೠ Ѧ

    ঌӝী थੋ) • ࢜۽਍ ചݶী ੸ਊ റ উ੿੸ੋ૑ ࠁҊ, ੉റ ӝઓ ചݶ ߸ജ ࣻ੿ द ୶о ੸ਊ • ಕয೐۽Ӓې߁ਸ ా೧ ೟णബҗ ૐ؀ • Kotlin documents ୭؀ೠ ഝਊ https://kotlinlang.org/docs/reference/ • Kotlin convert ୭؀ೠ ഝਊ җѢ ഥࢎীࢲ ੸ਊ ߑߨ
  8. Kotlinਸ ঌইࠁ੗

  9. Kotlin? • JetBrains ѐߊ ઱ب • Apache 2 license ӝ߈੄

    য়೑ࣗझ • JVMਤীࢲ ز੘ೞח ੿੸ ঱য • Java৬ ࢚ഐ ഐജ оמ(100%) • Coroutines ઁҕ(Asynchronous programming) • Effective Java ܙਸ ই઱ ੜ ૑ఃҊ ੓ח ঱য • Effective Java ଼ ࠁחѤ ୶ୌ
  10. Kotlin history 2011 ୭ୡ ҕѐ 2012 Open sourced project Apache

    2.0 license 2016 v1.0 release v1.1 release উ੿੸ੋ ঱য۽ ٜযࢲ׮ 2017 Google announced first-class support for Kotlin on Android. v1.2 release 2018 v1.3 release
 v1.0 release coroutines 2019 Kotlin programming language is now its preferred language for Android app developers.
  11. Kotlin ஹ౵ੌ

  12. Kotlin ӝࠄ ޙߨ

  13. var vs val

  14. var vs val class Sample { var name: String? =

    "taehwan" val age: Int = 0 @Test fun test() { println("name $name name.length ${name?.length} age $age") } }
  15. var public :nal class Sample { @Nullable private String name

    = "taehwan"; private :nal int age; @Nullable public :nal String getName() { return this.name; } public :nal void setName(@Nullable String var1) { this.name = var1; } public :nal int getAge() { return this.age; } } var ߸ࣻ name var ߸ࣻ getName() var ߸ࣻ setName()
  16. val public :nal class Sample { @Nullable private String name

    = "taehwan"; private :nal int age; @Nullable public :nal String getName() { return this.name; } public :nal void setName(@Nullable String var1) { this.name = var1; } public :nal int getAge() { return this.age; } } val ߸ࣻ age val ߸ࣻ getAge()
  17. Null Safety

  18. Null Safety class Sample { var name: String? = "taehwan"

    @Test fun test() { println("name $name name.length ${name?.length}") } } ? ࠢৈ null ೲਊ nullਸ ࢎਊೞӝ ਤ೧ࢲ ? ୊ܻ
  19. Null Safety public :nal class Sample { @Nullable private String

    name = "taehwan"; @Nullable public :nal String getName() { return this.name; } public :nal void setName(@Nullable String var1) { this.name = var1; } @Test public :nal void test() { StringBuilder var10000 = (new StringBuilder()).append("name ").append(this.name).append(" name.length "); String var10001 = this.name; String var1 = var10000.append(var10001 != null ? var10001.length() : null).toString(); System.out.println(var1); } } @Nullable чਸ ೲਊ 3೦धਵ۽ ߸ജ
  20. NonNull or NotNull

  21. NotNull class Sample { var name: String = "taehwan" @Test

    fun test() { println("name $name name.length ${name.length}") } } ?о হয NotNull NotNull਷ Ӓր ࢎਊ
  22. NotNull public :nal class Sample { @NotNull private String name

    = "taehwan"; @NotNull public :nal String getName() { return this.name; } public :nal void setName(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, "<set-?>"); this.name = var1; } @Test public :nal void test() { String var1 = "name " + this.name + " name.length " + this.name.length(); System.out.println(var1); } } NotNull ੉޲۽ null ୓௼ ۽૒
  23. String template

  24. String template println("name $name name.length ${name.length}") @Test public :nal void

    test() { String var1 = "name " + this.name + " name.length " + this.name.length(); System.out.println(var1); } ߸ࣻ݅ ॶٸ ߸ࣻ ੉࢚ਸ ॶٸ ੗߄ীࢲח +۽ ೤஘׮
  25. Class ࢚ࣘ

  26. Class ࢚ࣘ class Test public :nal class Test { }

    Kotlin class ੿੄ Javaীࢲח final੉ ನೣ
  27. Class ࢚ࣘ - open open class Test public class Test

    { } Open keyword Final੉ হ׮
  28. abstract class abstract class Test public abstract class Test {

    } Kotlin class ੿੄ Javaীࢲח final੉ ನೣ
  29. Etc

  30. Data class • data class(equals, hash, toString() ١ ઁҕ) •

    data class DataClass(val name: String)
  31. Infix notation inline infix fun Int.sum(b: Int) = this +

    b 10 sum 10
  32. Higher-Order function val sum: Int.(b: Int) -> Int = {

    this + b } println(10.sum(10))
  33. Stream val list = mutableListOf(1, "a", 10.0f, 22.00, sum) list

    .asSequence() .filter { it is Int } .map { it as Int } .forEach { println(it.sum(it)) }
  34. Google kotlin ૑ਗ

  35. 2017 Google I/O Android ѐߊ ݫੋ ঱য ૑ਗ ഛ؀۽ Kotlin

    ঱য ࢶఖ
  36. Jetpack - KTX Library • KTX ۄ੉࠳۞ܻܳ ࣗѐ (https://developer.android.com/kotlin/ktx) •

    Extension functions • Extension properties • Lambdas • Named parameters • Parameter default values // Commit a new value asynchronously sharedPreferences.edit { putBoolean("key", value) }
  37. Google I/O 2019

  38. Kotlin First

  39. • Architecture Components ࣁ࣌ীࢲ coroutines ૑ਗਸ ঌܿ Coroutines Support hnps://www.youtube.com/watch?v=Qxj2eBmXLHg

  40. Kotlin First and coroutines support • Google I/O App -

    2018 Kotlin base • Sunflowerer example • Codelab • Sample code hnps://developer.android.com/kotlin/resources
  41. Additional Kotlin resources hnps://developer.android.com/kotlin/resources

  42. Google I/O 2019 • Kotlin਷ о੢ ࡅܰѱ ߊ੹ೞח ঱য ઺

    ೞա • Androidীࢲ 50%੄ ѐߊ੗о Kotlin ࢎਊ • Jetpack Architecture Componentsী coroutines ੸ਊ • APIs ٣੗ੋ਷ Kotlin ਤ઱۽ ઁҕ(Kotlin nullability annotation ੸ਊ) • Jetpack KTX ੉৻ীب ੸ӓ੸ਵ۽ Kotlin بੑ ৘੿ • KTX https://developer.android.com/kotlin/ktx.html • Benchmark https://developer.android.com/studio/profile/benchmark.html
  43. Coroutinesਸ ઑӘ ঌইࠁҊ о੗

  44. • CoroutineScope / GlobalScope • Coroutineਸ ز੘ೞӝ ਤೠ ࢤࢿ੗ •

    Coroutines प೯ਸ ਤೠ blocks • launch, async/await ١ਸ ઁҕ • ௏ܖ౯ਸ ز੘ दఃӝ ਤೠ blocks • Dispatchers • Main(UI ୊ܻ), IO(ߔӒۄ਍٘ ੘স) झாે۞ܳ ઁҕ • suspend • ೣࣻ۽ coroutinesਸ ੿੄ Coroutinesਸ ઑӘ ঌইࠁҊ о੗
  45. Coroutines @Test fun coroutinesTest() { CoroutineScope(Dispatchers.Default).launch { delay(500L) println("ࠗ࢑") }

    print("Hello, GDG ") Thread.sleep(1000L) }
  46. • Coroutines for asynchronous programming and more • https://kotlinlang.org/docs/reference/coroutines-overview.html Coroutinesਸ

    ઑӘ ঌইࠁҊ о੗
  47. Kotlinਵ۽ ߸ജ ઺ੋ ۄ੉࠳۞ܻ

  48. • OkHttp੄ Kotlin ߸ജ ੘স • Java 59.1% • Kotlin

    40.8% • Kotlin/Coroutines ۄ੉࠳۞ܻ ੄ઓࢿ ೙ਃ • minSdk 21 OkHttp hnps://github.com/square/okhnp/issues/4723
  49. • RxJava৬ ೣԋ ࢎਊ • Android UI੄ ׮নೠ ੉߮౟ܳ RxJava

    ߄ੋ٬(Observable) • RxBinding 3.x ӝળ • Java 45.6% • Kotlin 54.1% RxBinding hnps://github.com/JakeWhason/RxBinding
  50. Kotlin ੹ਊ ۄ੉࠳۞ܻ

  51. • RxKotlin਷ RxJava ؀୓ਊ੉ ইצ ഛ੢ਸ ૑ਗ • ৮੹ೠ RxKotlin਷

    ই૒ ઓ੤ೞ૑ ঋ਺ RxKotlin hnps://github.com/ReactiveX/RxKotlin fun main(args: Array<String>) { val list = listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon") list.toObservable() // extension function for Iterables .filter { it.length >= 5 } .subscribeBy( // named arguments for lambda Subscribers onNext = { println(it) }, onError = { it.printStackTrace() }, onComplete = { println("Done!") } ) }
  52. • Kotlin ੹ਊ DI(dependency injection) • A pragmatic lightweight dependency

    injection framework for Kotlin developers. • Koin is a DSL, a light container and a pragmatic API KOIN hnps://github.com/InsesKoinIO/koin
  53. Coroutines ૑ਗ ࠗ࠙

  54. • Type-safe HTTP client for Android and Java by Square,

    Inc. • Coroutines ૑ਗ • Jake Wharton੉ ٜ݅঻؍ Adapterח ؊ ੉࢚ ߓನೞ૑ ঋח׮. Retrofit hnps://github.com/square/retrout @GET("api/me") suspend fun getUser(@Query(“id”) id: Long): Response<User>
  55. • ViewModelScope : androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01 • LifecycleScope : androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 • liveData

    : androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01 Use Kotlin coroutines with Architecture components hnps://developer.android.com/topic/libraries/architecture/coroutines
  56. class MyViewModel: ViewModel() { init { viewModelScope.launch { // Coroutine

    that will be canceled when the ViewModel is cleared. } } } ViewModelScope hnps://developer.android.com/topic/libraries/architecture/coroutines
  57. class MyFragment: Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?)

    { super.onViewCreated(view, savedInstanceState) viewLifecycleOwner.lifecycleScope.launch { val params = TextViewCompat.getTextMetricsParams(textView) val precomputedText = withContext(Dispatchers.Default) { PrecomputedTextCompat.create(longTextContent, params) } TextViewCompat.setPrecomputedText(textView, precomputedText) } } } LifecycleScope hnps://developer.android.com/topic/libraries/architecture/coroutines
  58. val user: LiveData<User> = liveData { val data = database.loadUser()

    // loadUser is a suspend function. emit(data) } Use coroutines with LiveData hnps://developer.android.com/topic/libraries/architecture/coroutines
  59. Feature

  60. • Kotlinਸ ݽܰҊ উ٘۽੉٘ ѐߊ਷ য۵׮ • Javaب ই૒਷ ঌইঠ

    ೠ׮.(ݽٚ ഥࢎীࢲ ই૒ Kotlinਸ ॳחѤ ইפ׮) • Kotlin + coroutines਷ ঌইفݶ જ׮. • Flutterب ঌইفݶ… Feature
  61. End…