Share
Embed
Transcript
61 slides
-
• KAKAOPAY উ٘۽٘ • Է ݆ ѐߊо غ! ࠶۽Ӓ
• https://thdev.tech • GDG Seoul • ٘۽٘աஎ • [email protected] -
• Kotlin ঌইࠁӝ • Google Kotlin ਗ • Coroutinesਸ ઑӘ
ঌইࠁҊ • Kotlin ਗ ۄ࠳۞ܻ ഛੋ • Coroutines ਗ ۄ࠳۞ܻ ഛੋ दೞӝ ী -
ਃӝਃী Kotlin ਊ ח 2011 2012 2016 2017 2018 v1.1
ٸ ࠗఠ ҕࠗ ਬؘ ъب ೠߣ ନҊ ড 40% Kotlin ਊ Coroutines ਊ 2019 Kotlin coroutines ݅ ࢎਊ -
• Java ٘۽ ࢿೠ ٘ܳ Kotlinਵ۽ ߸ജ • RxJava, MVP
ಁఢ ١ਸ ೠߣী ण • ਬؘ ъܳ ନਵݶࢲ ળ࠺ೠ ܐ۽ ण۱ ૐ • ഥࢎীࢲ पઁ ਊ • Kotlin/coroutines • пઙ ౣܽ ҙ۲ ߊ ण ߑߨ -
• IO 2017 աҊ ഥࢎী جইо ਊೞѷҊ ࠁҊ(যוب णೠ Ѧ
ঌӝী थੋ) • ࢜۽ ചݶী ਊ റ উੋ ࠁҊ, റ ӝઓ ചݶ ߸ജ ࣻ द ୶о ਊ • ಕয۽Ӓې߁ਸ ా೧ णബҗ ૐ • Kotlin documents ୭ೠ ഝਊ https://kotlinlang.org/docs/reference/ • Kotlin convert ୭ೠ ഝਊ җѢ ഥࢎীࢲ ਊ ߑߨ -
Kotlin? • JetBrains ѐߊ ب • Apache 2 license ӝ߈
য়ࣗझ • JVMਤীࢲ زೞח য • Java৬ ࢚ഐ ഐജ оמ(100%) • Coroutines ઁҕ(Asynchronous programming) • Effective Java ܙਸ ই ੜ ఃҊ ח য • Effective Java ଼ ࠁחѤ ୶ୌ -
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. -
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") } } -
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() -
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() -
Null Safety class Sample { var name: String? = "taehwan"
@Test fun test() { println("name $name name.length ${name?.length}") } } ? ࠢৈ null ೲਊ nullਸ ࢎਊೞӝ ਤ೧ࢲ ? ܻ -
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೦धਵ۽ ߸ജ -
NotNull class Sample { var name: String = "taehwan" @Test
fun test() { println("name $name name.length ${name.length}") } } ?о হয NotNull NotNull Ӓր ࢎਊ -
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 ۽ -
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); } ߸ࣻ݅ ॶٸ ߸ࣻ ࢚ਸ ॶٸ ߄ীࢲח +۽ -
Class ࢚ࣘ class Test public :nal class Test { }
Kotlin class Javaীࢲח final ನೣ -
Class ࢚ࣘ - open open class Test public class Test
{ } Open keyword Final হ -
abstract class abstract class Test public abstract class Test {
} Kotlin class Javaীࢲח final ನೣ -
Data class • data class(equals, hash, toString() ١ ઁҕ) •
data class DataClass(val name: String) -
Higher-Order function val sum: Int.(b: Int) -> Int = {
this + b } println(10.sum(10)) -
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)) } -
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) } -
Kotlin First and coroutines support • Google I/O App -
2018 Kotlin base • Sunflowerer example • Codelab • Sample code hnps://developer.android.com/kotlin/resources -
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 -
• CoroutineScope / GlobalScope • Coroutineਸ زೞӝ ਤೠ ࢤࢿ •
Coroutines प೯ਸ ਤೠ blocks • launch, async/await ١ਸ ઁҕ • ܖ౯ਸ ز दఃӝ ਤೠ blocks • Dispatchers • Main(UI ܻ), IO(ߔӒۄ٘ স) झாે۞ܳ ઁҕ • suspend • ೣࣻ۽ coroutinesਸ Coroutinesਸ ઑӘ ঌইࠁҊ о -
Coroutines @Test fun coroutinesTest() { CoroutineScope(Dispatchers.Default).launch { delay(500L) println("ࠗ") }
print("Hello, GDG ") Thread.sleep(1000L) } -
• OkHttp Kotlin ߸ജ স • Java 59.1% • Kotlin
40.8% • Kotlin/Coroutines ۄ࠳۞ܻ ઓࢿ ਃ • minSdk 21 OkHttp hnps://github.com/square/okhnp/issues/4723 -
• RxJava৬ ೣԋ ࢎਊ • Android UI নೠ ߮ܳ RxJava
߄ੋ٬(Observable) • RxBinding 3.x ӝળ • Java 45.6% • Kotlin 54.1% RxBinding hnps://github.com/JakeWhason/RxBinding -
• 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!") } ) } -
• 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 -
• 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> -
• 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 -
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 -
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 -
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 -
• Kotlinਸ ݽܰҊ উ٘۽٘ ѐߊ য۵ • Javaب ই ঌইঠ
ೠ.(ݽٚ ഥࢎীࢲ ই Kotlinਸ ॳחѤ ইפ) • Kotlin + coroutines ঌইفݶ જ. • Flutterب ঌইفݶ… Feature