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

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

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

2019 GDG Busan - Kotlin

TaeHwan

July 06, 2019
Tweet

More Decks by TaeHwan

Other Decks in Programming

Transcript

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

    ਍৔ • https://thdev.tech • GDG Seoul ਍৔૓ • ٘۽੉٘ա੉எ ਍৔૓ • [email protected]
  2. • Kotlin ঌইࠁӝ • Google Kotlin ૑ਗ • Coroutinesਸ ઑӘ

    ঌইࠁҊ • Kotlin ૑ਗ ۄ੉࠳۞ܻ ഛੋ • Coroutines ૑ਗ ۄ੉࠳۞ܻ ഛੋ द੘ೞӝ ੹ী
  3. ਃӝਃী Kotlin ੸ਊ ੷ח 2011 2012 2016 2017 2018 v1.1

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

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

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

    য়೑ࣗझ • JVMਤীࢲ ز੘ೞח ੿੸ ঱য • Java৬ ࢚ഐ ഐജ оמ(100%) • Coroutines ઁҕ(Asynchronous programming) • Effective Java ܙਸ ই઱ ੜ ૑ఃҊ ੓ח ঱য • Effective Java ଼ ࠁחѤ ୶ୌ
  7. 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.
  8. 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") } }
  9. 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()
  10. 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()
  11. Null Safety class Sample { var name: String? = "taehwan"

    @Test fun test() { println("name $name name.length ${name?.length}") } } ? ࠢৈ null ೲਊ nullਸ ࢎਊೞӝ ਤ೧ࢲ ? ୊ܻ
  12. 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೦धਵ۽ ߸ജ
  13. NotNull class Sample { var name: String = "taehwan" @Test

    fun test() { println("name $name name.length ${name.length}") } } ?о হয NotNull NotNull਷ Ӓր ࢎਊ
  14. 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 ୓௼ ۽૒
  15. 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); } ߸ࣻ݅ ॶٸ ߸ࣻ ੉࢚ਸ ॶٸ ੗߄ীࢲח +۽ ೤஘׮
  16. Class ࢚ࣘ class Test public :nal class Test { }

    Kotlin class ੿੄ Javaীࢲח final੉ ನೣ
  17. abstract class abstract class Test public abstract class Test {

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

  19. 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)) }
  20. 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) }
  21. Kotlin First and coroutines support • Google I/O App -

    2018 Kotlin base • Sunflowerer example • Codelab • Sample code hnps://developer.android.com/kotlin/resources
  22. 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
  23. • CoroutineScope / GlobalScope • Coroutineਸ ز੘ೞӝ ਤೠ ࢤࢿ੗ •

    Coroutines प೯ਸ ਤೠ blocks • launch, async/await ١ਸ ઁҕ • ௏ܖ౯ਸ ز੘ दఃӝ ਤೠ blocks • Dispatchers • Main(UI ୊ܻ), IO(ߔӒۄ਍٘ ੘স) झாે۞ܳ ઁҕ • suspend • ೣࣻ۽ coroutinesਸ ੿੄ Coroutinesਸ ઑӘ ঌইࠁҊ о੗
  24. • OkHttp੄ Kotlin ߸ജ ੘স • Java 59.1% • Kotlin

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

    ߄ੋ٬(Observable) • RxBinding 3.x ӝળ • Java 45.6% • Kotlin 54.1% RxBinding hnps://github.com/JakeWhason/RxBinding
  26. • 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!") } ) }
  27. • 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
  28. • 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>
  29. • 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
  30. 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
  31. 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
  32. 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
  33. • Kotlinਸ ݽܰҊ উ٘۽੉٘ ѐߊ਷ য۵׮ • Javaب ই૒਷ ঌইঠ

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