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

Kotlin+Android

7f14c100f77d6d7e4ab6e88c8ecde208?s=47 Paul Barré
December 08, 2017

 Kotlin+Android

2012/12/8
フロントエンド勉強会 in 山陰 #03
KotlinでのAndroid開発の紹介

2017.12.21時点の修正
P.8
fun add(x: Int, y: Int): Int {
return x + y
}

7f14c100f77d6d7e4ab6e88c8ecde208?s=128

Paul Barré

December 08, 2017
Tweet

Transcript

  1. KOTLIN + ANDROID ϑϩϯτΤϯυษڧձ in ࢁӄ#03
 2017/12/08 όϨɾϙʔϧ

  2. ΞδΣϯμ ➤ Kotlinݴޠͱ͸
 ➤ Kotlinݴޠͷػೳ
 ➤ AndroidͰ࣮૷͸Ͳ͏ͳΔʁ 2

  3. ࣗݾ঺հ ➤ όϨɹϙʔϧ ➤ ϑϥϯεग़਎ ➤ ೔ຊࡏॅ̔೥໨ʢ౦ژɺٶ࡚ɺদߐʣ ➤ גࣜձࣾιχοΫϜʔϒɹౡࠜࣄۀॴɺϑϩϯτΤϯδχΞ ➤

    ϞόΠϧ։ൃܦݧ͸̍೥ʢiOSɺAndroidʣ ➤ ϒϩάɿhttp://paulbarre.co ➤ Githubɿhttps://github.com/paulbarre 3
  4. KOTLINͱ͸ ➤ KotlinʢίτϦϯʣ ➤ JetBrainsࣾ (PHPStorm, IntelliJ IDEA…ʣ
 ͕։ൃͯ͠Δ ➤

    JVM্Ͱಈ࡞͢Δ ➤ ੩తܕ෇ͷOOPݴޠ ➤ ؔ਺ܕݴޠ ➤ Φʔϓϯιʔε
 Kotlinݴޠͷϩΰ ϑΟϯϥϯυ࿷ʹ͋ΔίτϦϯౡʢϩγΞʣ 4
  5. KOTLINͷཤྺ ➤ 2010೥ɹ։ൃͷ͸͡Ί ➤ 2011೥7݄20೔ɹൃද ➤ 2012೥2݄14೔ɹApache 2.0ϥΠηϯεʹج͍ͮͯΦʔϓϯ ιʔεԽ ➤

    2016೥2݄15೔ɹKotlin v1.0ϦϦʔε ➤ 2017೥5݄17೔ɹGoogle I/OͰAndroidΞϓϦ։ൃʹ౰ͨͬͯ ୈҰڃݴޠͷҰͭબఆ 5
  6. KOTLINΛ࣮ࡍ࢖ͬͯΔΞϓϦ ➤ KotlinݴޠΛ࢖͍ͬͯΔΞϓϦ͕૿͍͑ͯΔ https://developer.android.com/kotlin/index.html 6

  7. HELLO KOTLIN! fun main(args: Array<String>) { println("Hello, Kotlin!") } 7

  8. جຊͷؔ਺ fun add(x: Int, y: Int): Int { fun x

    + y } fun add(x: Int, y: Int): Int = x + y fun add(x: Int, y: Int) = x + y 8
  9. NULL҆શ class MyClass() { fun doSomething() { … } }

    val a: MyClass = null //ίϯύΠϧΤϥʔ val b: MyClass? = null b.doSomething() //ίϯύΠϧΤϥʔ 9
  10. WHEN fun cases(obj: Any) { when (obj) { 1 ->

    println("One") "Hello" -> println("Greeting") is Long -> println("Long") !is String -> println("Not a string") else -> println("Unknown") } } 10
  11. ֦ுؔ਺ fun <T> MutableList<T>.swap(index1: Int, index2: Int) { val tmp

    = this[index1] this[index1] = this[index2] this[index2] = tmp } 11
  12. ֦ுϓϩύςΟ val <T> List<T>.lastIndex: Int get() = size - 1

    12
  13. SWIFTͱҧ͏͚ͲɺࣅͯΔͱ͜Ζ͕͋Δ // Swift var, let func foo(arg: Any) -> Any

    class MyClass {} var ins = MyClass() ins.add(x: 1, y: 2) // Kotlin var, val fun foo(arg: Any): Any class MyClass {} var ins = MyClass() ins.add(1, 2) 13
  14. SWIFTͱҧ͏͚ͲɺࣅͯΔͱ͜Ζ͕͋Δ // SwiftͷOptional let ins2: MyClass? ins2?.foo() if let ins2

    = ins2 { ins2.foo() } // KotlinͷNullable val ins2: MyClass? ins2?.foo() if (ins2 != null) { ins2.foo() } 14
  15. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛσʔλΫϥε 15 data class User(val name: String, val age: Int)

    ➤ getters / settersʢvarͷ৔߹ʣ ➤ equals ➤ hashCode ➤ toString
 
 ্هͷؔ਺͸ࣗಈͰ࡞ͬͯ͘ΕΔ
  16. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 16 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener(object : OnClickListener { override fun onClick(v: View) { toast("Click") } })
  17. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 17 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener({ view -> toast(“Click”) })
  18. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 18 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener({ toast(“Click”) })
  19. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛϥϜμػೳ 19 // Java view.setOnClickListener(new OnClickListener() { @Override public void

    onClick(View v) { Toast.makeText(v.getContext(), “Click", Toast.LENGTH_SHORT).show(); } }); // Kotlin view.setOnClickListener { toast(“Click”) }
  20. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛANDROID EXTENSION 20 // Java TextView view = (TextView) findViewById(R.id.hello);

    view.setText(“Hello!”); // Kotlin import kotlinx.android.synthetic.<layout>.* …
 hello.text = “Hello!”
  21. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛޓ׵ੑ 21 ➤ Java͔Βม׵͸؆୯ ➤ StackoverflowʢͳͲʣ͔Βͷίϐϖ͕ ؆୯ ➤ KotlinͱJava͸Ұॹʹಈ͘

  22. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛANKO ➤ AnkoɿਓؾͷϥΠϒϥϦ
 https://github.com/Kotlin/anko ➤ Anko layoutsʢDSLʣ 22 verticalLayout {

    val name = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } }
  23. ANDROIDͰ࣮૷͸Ͳ͏ͳΔʁʛANKO ➤ AnkoɿਓؾͷϥΠϒϥϦ
 https://github.com/Kotlin/anko ➤ Anko Intent 23 val intent

    = Intent(this, SomeOtherActivity::class.java) intent.putExtra("id", 5) intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP) startActivity(intent) startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
  24. KOTLINͱؾʹͳͬͨͱ͜Ζ ➤ ৚݅ԋࢉࢠ͕ͳ͍ ➤ SwiftΈ͍ͨͷguard let͕ͳ͍ ➤ let͸Swiftͱͷҧ͏
 ➤ JavaΑΓϏϧυ͕࣌ؒ஗͍


    https://medium.com/keepsafe-engineering/kotlin-vs-java- compilation-speed-e6c174b39b5d ➤ JavaΑΓύοέʔδϑΝΠϧͷαΠζ͕େ͖͍ 24 val user = findUser(id) user?.let { foundUser -> }
  25. ·ͱΊ ➤ ҆શɿNullPointerException ➤ ؆ܿɿίʔυͷྔ͕ݮΔ ➤ Javaͱͷޓ׵ੑ
 ม׵͸؆୯
 Android SDK͸ී௨ʹ࢖͑Δ

    ➤ Android StudioͰ࢖͍΍ֶͯ͘͢ͼ΍͍͢ 25
  26. ϦϯΫ ➤ Kotlin
 https://kotlinlang.org/ ➤ KotlinϓϩδΣΫτͷGithub
 https://github.com/JetBrains/kotlin
 ➤ Φεεϝͷϒϩάʢ@lime_clʣ
 https://antonioleiva.com


    ➤ Try Kotlin!
 https://try.kotlinlang.org
 26
  27. “ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ 27