新言語KotlinでAndroidプログラミング #DroidKaigi

新言語KotlinでAndroidプログラミング #DroidKaigi

DroidKaigiでの発表で使用したスライドです。

14c9795d267f5b85abb98ca5e8780646?s=128

Taro Nagasawa

April 25, 2015
Tweet

Transcript

  1. ৽ݴޠ KotlinͰ Androidϓϩάϥϛϯά  %SPJE,BJHJ ! ೔ຊ,PUMJOϢʔβάϧʔϓ
 ௕ᖒଠ࿠!OHTX@UBSP ͜ͱΓΜ

  2. ࣗݾ঺հ

  3. ͸͡Ί·ͯ͠ʂ w ௕ᖒଠ࿠!OHTX@UBSP w ࢓ࣄͰ͸+BWB "OESPJEɺͱ͖Ͳ͖4DBMB w ࡀɺֈ࠲ɺͱ͕େ޷͖

  4. Θͨ͠ͱAndroid w ग़ձ͍͸೥ɻ)5"Ͱ༡ΜͰΈͨͷ͕࠷ॳ w ݸਓͰʮເͱຐ๏ͷ଴ͪ࣌ؒʯͱ͍͏ΞϓϦΛ։ ൃɾެ։͍ͯ͠·͢ ສ%-  w ॻ੶΍ٕज़ܥಉਓࢽͳͲʹܞΘΒ͍͍ͤͯͨͩͨ

    ܦݧ͕͋Γ·͢
  5. Θͨ͠ͱKotlin w ເதʹͳΓ࢝Ίͨͷ͸೥ w Ҏདྷɺϒϩά΍ษڧձͰ৘ใൃ৴ w 4PGUXBSF%FTJHO ٕධ Ͱ,PUMJOهࣄΛ࿈ࡌத

  6. ΋͘͡ ,PUMJOͱ͸ +BWBͱൺ΂Δ ,PUMJOͰ"OESPJEϓϩάϥϛϯά

  7. 1. Kotlinͱ͸

  8. ࣭໰ Kotlinͱ͍͏ϓϩάϥϛϯάݴޠ Λฉ͍ͨ͜ͱ͕͋Γ·͔͢ʁ

  9. w !+BLF8IBSUPO͞Μ w ʮ6TJOH1SPKFDU,PUMJOGPS"OESPJEʯ w !LJSJNJO͞Μ w ʮ೥ͷ"OESPJE։ൃ͸,PUMJOͰܾ·Γ͔΋ ͠Εͳ͍ʯ Androidք۾Ͱόζͬͨʁ

  10. Kotlinͱ͸

  11. Kotlinͱ͸ Better Java

  12. w ೥Նʹ+FU#SBJOTʹΑΓൃද w Φʔϓϯιʔε "QBDIF-JDFOTF  w +7.ݴޠɺBMU+4ɺ"OESPJE΋αϙʔτ w ੩తܕ෇͚ΦϒδΣΫτࢦ޲ݴޠ

    Kotlinͱ͸ (ৄ͘͠)
  13. ଞͷJVMݴޠͱͷҧ͍͸

  14. ଞͷJVMݴޠͱͷҧ͍͸ ϚΠϧυ ݱ࣮ݟͯΔײ

  15. Kotlinͷಛ௃ ؆୯ ҆શ

  16. 2. Javaͱൺ΂Δ

  17. Hello World (Java) class HelloWorld { public static void main(String[]

    args) { System.out.println("Hello, world!"); } }
  18. Hello World (Kotlin) fun main(args: Array<String>) { println("Hello, world!") }

  19. Hello World (Kotlin) fun main(args: Array<String>) { println("Hello, world!") }

    τοϓϨϕϧʹ ؔ਺Λஔ͚Δ ഑ྻʹ δΣωϦΫε ηϛίϩϯ ෆཁ
  20. ม਺ (Java) final int a = 0;

  21. ม਺ (Kotlin) val a = 0

  22. ม਺ (Kotlin) val a = 0 ܕਪ࿦ Մมͳม਺ʹ͸ WBSΩʔϫʔυΛ

  23. Ϋϥε (Java) class User { private final long id; private

    final String name; public User(long id, String name) { this.id = id; this.name = Objects.requireNonNull(name); } public long getId() { return id; } public String getName() { return name; } / / toString, equals, hashCodeͱ͔ }
  24. Ϋϥε (Kotlin) class User(val id: Long, val name: String) {

    / / toString, equals, hashCodeͱ͔ }
  25. Ϋϥε (Kotlin) class User(val id: Long, val name: String) {

    / / toString, equals, hashCodeͱ͔ } ίϯετϥΫλҾ਺͕ ͦͷ··ϓϩύςΟʹ
  26. dataΞϊςʔγϣϯ data class User(val id: Long, val name: String)

  27. σʔλΫϥε val taro = User(123, "Taro") println(taro.id) / / =>

    123 println(taro.name) / / => Taro println(taro) / / => User(id=123, name=Taro)
  28. ؔ਺ܕΠϯλϑΣʔε (Java) / / anonymous class button.setOnClickListener(new View.OnClickListener() { @Override

    public void onClick(View v) { toast("Clicked!"); } }); ! / / lambda button.setOnClickListener(v -> toast("Clicked!"));
  29. ؔ਺ܕΠϯλϑΣʔε (Kotlin) var count = 0 button.setOnClickListener { toast("${count++}") }

  30. ؔ਺ܕΠϯλϑΣʔε (Kotlin) var count = 0 button.setOnClickListener { toast("${count++}") }

    Ϋϩʔδϟ
  31. ߴ֊ؔ਺ͰίϨΫγϣϯૢ࡞ (1..999) .filter { it % 3 == 0 ||

    it % 5 == 0 } .reduce { a, b -> a + b }
  32. ߴ֊ؔ਺ͰίϨΫγϣϯૢ࡞ (1..999) .filter { it % 3 == 0 ||

    it % 5 == 0 } .reduce { a, b -> a + b } ʮΠϯϥΠϯؔ਺ʯ ؔ਺Ϧςϥϧ͕ల։͞Ε ΦϒδΣΫτΛੜ੒͠ͳ͍
  33. ಠࣗศརϝιου (Java) / / ఆٛ static void toast(Context c, String

    s) {...} ! / / ݺͼग़͠ MyUtils.toast(context, "Hello");
  34. ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !

    / / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello")
  35. ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !

    / / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello") ϝιουΛੜ΍ͤΔ ʮ֦ுؔ਺ʯ ੩తʹղܾ͞ΕΔʂ
  36. Optional (Java) String a = "hoge"; Optional<String> b = Optional.of("fuga");

    Optional<String> c = null;
  37. Optional (Java) String a = "hoge"; Optional<String> b = Optional.of("fuga");

    Optional<String> c = null; ΦϒδΣΫτੜ੒ ୭ʹ΋OVMM͸ࢭΊΒΕͳ͍ʂ
  38. Optional (Kotlin) val a: String = "hoge" val b: String

    = null / / NG ! val c: String? = "hoge" val d: String? = null / / OK ! d.toUpperCase() / / NG
  39. NULL҆શ val a: String? = "hoge" val b: String? =

    null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null
  40. NULL҆શ val a: String? = "hoge" val b: String? =

    null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null OVMMͰͳ͍͜ͱ͕อূ͞Ε͍ͯΔ ৔ॴͰ͸/PU/VMMܕͱͯ͠ѻ͑Δ
  41. 3. KotlinͰ Androidϓϩάϥϛϯά

  42. Android StudioͰ࢖͏ ී௨ʹϓϩδΣΫτΛ࡞੒ +BWB,PUMJOม׵ Φϓγϣϯ  CVJMEHSBEMFͷઃఆ ָ͍͠,PUMJOϥΠϑͷ࢝·Γʂ

  43. KotterKnife w,PUMJO൛#VUUFS,OJGF w࡞ऀ͸+BLF8IBSUPO͞Μ wಋೖ΍࢓૊Έղઆ͸ͪ͜ΒΛࢀর IUUQRJJUBDPNOHTX@UBSPJUFNTDEDEE

  44. KotterKnife ࢖༻લ var button: Button? = null ! button =

    findViewById(R.id.button) as Button ! button?.setOnClickListener {...}
  45. KotterKnife ࢖༻લ var button: Button? = null ! button =

    findViewById(R.id.button) as Button ! button?.setOnClickListener {...} "DUJWJUZͱ͔ͷϓϩύςΟ ຖ౓ͷpOE7JFX#Z*E ͦͯ͠Ωϟετʜ
  46. KotterKnife ࢖༻ޙ var button: Button by bindView(R.id.button) ! button.setOnClickListener {...}

  47. KotterKnife ࢖༻ޙ var button: Button by bindView(R.id.button) ! button.setOnClickListener {...}

    ϓϩύςΟ ಉ͡ CZΩʔϫʔυʹଓ͚ͯ 3JECVUUPOΛόΠϯυ pOE7JFX#Z*EෆཁͰ ී௨ʹ࢖͑ΔΑ͏ʹͳͬͯΔ
  48. Kotlin Android Extensions w"4 ͱ*OUFMMJ+*%&" ͷϓϥάΠϯ wpOE7JFX#Z*EΛۦஞ͢Δ΍ͭ wνϡʔτϦΞϧ͸ͪ͜ΒΛࢀর IUUQLPUMJOMBOHPSHEPDTUVUPSJBMTBOESPJEQMVHJOIUNM

  49. ࢖༻ྫ import kotlinx.android.synthetic.activity_main.button ! public class MainActivity: Activity() { override

    fun onCreate(savedInstaceState: Bundle?) { super.onCreate(savedInstaceState) setContentView(R.layout.activity_main) button.setOnClickListener {...}
  50. Anko w͍ΖΜͳ%4- wओʹϨΠΞ΢τΛ૊Ή%4- wܕ҆શɺ؆ܿɾϦʔμϒϧɺ෦඼Խɾ ࠶ར༻ੑ wৄࡉ͸ͪ͜Β IUUQTHJUIVCDPN+FU#SBJOTBOLP

  51. None
  52. ·ͱΊ w ,PUMJO͸ϓϩάϥϚʹ༏͍͠ʮݱ࣮ࢦ޲ʯݴޠ w "OESPJEͰ࢖͏ͱ޾ͤʹͳΕΔ͔΋

  53. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ Let’s enjoy Kotlin!