開発 4 年目のアプリへの Kotlin 導入

開発 4 年目のアプリへの Kotlin 導入

2017/07/12 Kotlin 開発 Tech Talks

Dd17014e380450cfed5d55ff20fa3dfd?s=128

Ryutaro Miyashita

July 12, 2017
Tweet

Transcript

  1. Ꟛ涪䎃湡ך،فٔפך,PUMJO㼪Ⰵ

  2. 㹧♴畆㣐龤 $IBU8PSL吳䒭⠓爡 ،فٔ؛٦ءّٝꟚ涪鿇ث٦ئ٦ت٦ !SZVHPP@

  3. $IBU8PSLחאְג

  4. $IBU8PSL  㕂欵ךؽآطأثٍحز؟٦ؽأ  䎃剢ַ׵䲿⣘Ꟛ㨣  㼪Ⰵ爡侧 爡  䎃剢劣傈儗挿

     䎃劣ח4DBMB׾㼪Ⰵ  1)1׮⢘欽׃גְֹתׅ
  5. ؒٝآص،⹫꧊⚥  J04"OESPJEؒٝآص،⹫꧊⚥  匌❨ؔؿ؍أ㣐ꢻؔؿ؍أ  匌❨ؔؿ؍أכ剢ח匌❨ةٙ٦׉לח獳鯄  IUUQDPSQDIBUXPSLDPNKBSFDSVJUFOHJOFFS

  6. $IBU8PSLךٌغ؎ٕ،فٔ  剑ⴱכ5JUBOJVN醡  +BWB4DSJQUדط؎ذ؍ـ6*׾䭯א،فָٔ⡲׸׷  3FBDU/BUJWF׫׋ְזװאך饥׶  "OESPJE晛 

    䎃剢ַ׵ط؎ذ؍ـ⻉  J04晛  䎃剢ַ׵ط؎ذ؍ـ⻉
  7.  䎃剢  ➂ד"OESPJEط؎ذ؍ـ晛ךꟚ涪׾Ꟛ㨣  䎃剢  ,PUMJO⹈䓼⠓ח겣׾⳿ׅ  䎃剢

     "OESPJEط؎ذ؍ـ晛׾ٔٔ٦أ  䎃  せ̔せ⡤ⵖח  䎃剢  ,PUMJO醡ך傈㜠،فٔ׾爡ⰻ麊欽Ꟛ㨣  䎃剢  ,PUMJO׾فٗتؙءّٝח㼪Ⰵ
  8. ז׈,PUMJO׾㼪Ⰵׅ׷ךַ

  9. ,PUMJO㼪Ⰵ׾䚈ּ䗳銲כ֮׷ַ  䚈ָזֽ׸לז׵זְ椚歋כזְ  ,PUMJOׄׯזְה"OESPJE،فָٔ⡲׸זֻז׷
 ˘˘׻ֽדכזְ  ֻ֮תד׮Ⱅ䒭؟ه٦ز鎉铂ָ㟓ִ׋׌ֽ  ؟ه٦زָ竰竲ׁ׸׷㸜䗰䠬׾䩛חⰅ׸׋

  10. ׉׸ד׮,PUMJO׾㼪Ⰵׅ׷椚歋  ؒٝآص،涸ז嚂׃ׁ  倜׃ְ׮ךח鍗׸׵׸׷ٌثك٦ءّٝ  ؒٝسِ٦ؠ٦ךًٔحزח׮ז׷鎉铂堣腉  Ꟛ涪ח꧊⚥׃װְׅ 

    ن؎ٓ٦فٖ٦ز׾㼰זֻדֹ׷  膷ⰻךⳢ椚؎ً٦آ׾؝٦سח⿾僥׃װְׅ  "OESPJEꟚ涪鎉铂ה׃גⰕ䒭ח؟ه٦ز  Ꟛ涪ך䩛ָ姺ת׏ג׃תֲ〳腉䚍ָ嶊ִ׋
  11. ,PUMJOך㴍׃ְהֿ׹

  12. ن؎ٓ٦فٖ٦ز׾幾׵ֿׅהָדֹ׷  -BNCEB䒭  ًاحس׾א׌ֽ䭯א؎ٝة٦ؿؑ٦أ  4JOHMF"CTUSBDU.FUIPE5ZQF  -BNCEB䒭׾⢪׏גⱔꞿז鎸鶢ָ♶銲חז׷ 

    +BWBך׮ךה嫰鯰׃ג  䒷侧ָאד♶銲ז㜥さכ剅ַזֻג׮葺ְ  ⢪׻זְ䒷侧כˑ@˒ד邌植  وآحؙؗ٦ٙ٦سˑJU˒
  13. Button b = (Button) findViewById(R.id.button); b.setOnClickListener(new View.OnClickListener() { @Override public

    void onClick(View view) { // Your business logic } }); "OESPJE+BWB %FGBVMU ⼡せؙٓأ׾⢪׏גٔأش٦׾إحزׅ׷ךדن؎ٓ٦فٖ٦زָ㢳ְ
  14. Button b = (Button) findViewById(R.id.button); b.setOnClickListener(view —> { // Your

    business logic }); "OESPJE+BWB -BNCEB 4JOHMF"CTUSBDU.FUIPEכ-BNCEB䒭׾⢪ֲֿהדأحؗٔה剅ֽ׷
  15. val b = findViewById(R.id.button) as Button b.setOnClickListener { view ->

    // Your business logic } b.setOnClickListener { _ -> // No use args } b.setOnClickListener { //“it” is View } ,PUMJO ,PUMJOד׮ずׄ״ֲח剅ֽ׷ ⢪׻זְ䒷侧כ@ד邌ׅ 䒷侧ָאז׵ל ˑJU˒ؗ٦ٙ٦سד⿫撑 ׅ׷ֿה׮דֹ׷
  16. 䙼罋׾⿾僥׃װְׅ鎉铂堣腉  أ؝٦فꟼ侧  知患ז鎸鶢ך䩛⸔ֽ׾׃גֻ׸׷  ؎ً٦آ鸐׶ך؎ٝأةٝأך欰䧭ח⤑ⵃ  䭁䓸ꟼ侧 

    ְא׮ךⳢ椚׾ًاحسך״ֲח䪔ִ׷  傀㶷ךؙٓأח،سنحؙחًاحس׾鷄⸇  JG䒭 USZ䒭 XIFO䒭  ⦼׾鵤ֿׅהָדֹ׷
  17. public static MyFragment newInstance(String str) { MyFragment fragment = new

    MyFragment(); Bundle args = fragment.getArguments(); if (args == null) { args = new Bundle(); } args.putString(KEY_TEXT, str); fragment.setArguments(args); return fragment; } +BWB 葺ֻ֮׷ؿ؋ؙزٔػة٦ٝ
  18. ,PUMJO أ؝٦فꟼ侧 fun newInstance(str: String) = MyFragment().apply { arguments =

    (arguments ?: Bundle()).apply { putString(KEY_TEXT, str) } } ˑBQQMZ˒כٖء٦غ׾「ֽ《׏גٖء٦غ׾鵤ׅ ➭ח׮MFU SVO XJUI BMTPָ֮׷׉׸׊׸䕵ⶴָ殯ז׷
  19. ,PUMJO أ؝٦فꟼ侧 var test: String? = null var result: String

    = text ?: run { // Some processing "Hello, world" } ٕؒؽأ怴皾㶨ה穈׫さ׻ׇג䊩鴟ָOVMMך㜥さך⦼׾ ⡦׵ַךⳢ椚ה穈׫さ׻ׇג《䖤דֹ׷ ˟+BWB4DSJQUך⽯儗ꟼ侧䠬鋙
  20. +BWB public class StringUtils { public static boolean isNothing(String text)

    { return text == null || text.isEmpty(); } } OVMMת׋כ瑞俑㶵⴨ַוֲַ׾ثؑحؙ׃׋ְ
  21. ,PUMJO DMBTT PCKFDU class StringUtils { companion object { fun

    isNothing(text: String?): Boolean { return text == null || text.isEmpty() } } } object StringUtils { fun isNothing(text: String?): Boolean { return text == null || text.isEmpty() } } TUBUJDًاحسך״ֲח鋅ׇ׷ ˑPCKFDU˒׾⢪׏גءؚٕٝزٝד㹀纏ׅ׷
  22. ,PUMJO 䭁䓸ꟼ侧 fun String.isNothing(): Boolean = this == null ||

    this.isEmpty() var test: String? = null test.isNothing() // true 㢌侧ך⚥魦ָˑOVMM˒ד׮/1&搀׃חثؑحָؙדֹ׷
  23. ת׋כծؽٕز؎ׁٝ׸גְ׷׮ך׾⢪ֲ @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean = this ==

    null || this.length == 0 俑㶵⴨ךOVMMת׋כˑ瑞˒俑㶵ثؑحָؙדֹ׷ @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrBlank(): Boolean = this == null || this.isBlank() 俑㶵⴨ךOVMMת׋כˑ瑞涯˒俑㶵ثؑحָؙדֹ׷
  24. +BWB JG俑 String text = "This is java"; String result;

    if (text.length % 2 == 0) { result = "Even"; } else { result = "Odd"; } JGˑ俑˒ 㢌侧㹀纏ךة؎ىؚٝד ⦼׾然㹀דֹזְ
  25. ,PUMJO JG䒭 val text = "This is Kotlin" val result

    = if (text.length % 2 == 0) { "Even" } else { "Odd" } JGˑ䒭˒ה׃ג⦼׾鵤⽱ׅ׷ֿהָדֹ׷ךד 㢌侧㹑鎉儗ח⦼׾然㹀ׇׁ׵׸׷ JGFMTFぐـٗحؙך剑䖓ך遤ָ鐰⣣ׁ׸׷
  26. +BWB TXJUDI俑 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

    case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(); } } ⢽ִלًصُ٦鼅䫛儗ךⳢ椚כ+BWBדכTXJUDI俑ד剅ֻֿהָ㢳ְ TXJUDI俑דכ⦼׾鵤ֿׅהָדֹזְךד؝٦سָ撕꧟חז׶װְׅ
  27. ,PUMJO XIFO䒭 fun onOptionsItemSelected(item: MenuItem?): Boolean = when(item?.itemId) { android.R.id.home

    -> { finish() true } else -> super.onOptionsItemSelected() } ,PUMJOחTXJUDI俑כזֻծ➿׻׶חXIFO䒭׾⢪ֲ
 XIFO䒭כ⽃秪ז瘝⣣嫰鯰⟃㢩ח׮侧⦼ךٖٝآ䭷㹀װ㘗ثؑحؙ׮〳腉
  28. 㴍׃ְהֿ׹ָˑث٦ي˒חוֲ⸬ְגֻ׷ַ  ِ٦أ؛٦أ׾邌׃׋؝٦سחז׶װְׅ  搀꼽ָזְךד؝٦سٖؽُ٦ָװ׶װְׅ  然钠ה䭷䶯ך怩׸ָ㼰זֻז׷  Ꟛ涪ה然钠ךٕ٦ف׾넝鸞⻉ׅ׷ֿהָדֹ׷ 

    ِ٦ؠ٦פך⣣⦼䲿⣘ח꧊⚥דֹ׷
  29. 孡׾אֽ׋ְהֿ׹

  30. ,PUMJOח׮蕱ְהֿ׹ָ֮׷  ًاحس侧  ًاحسקו㟓ִ׷  Lًاحسⵖꣲחעאַ׷〳腉䚍ָ넝ת׷  "OOPUBUJPO1SPDFTTPS 

    BQQCVJMEHSBEMF  BOOPUBUJPO1SPDFTTPS̔LBQUח㢌ִ׷  LBQUָ欰䧭ׅ׷؝٦سח㉏겗ָ֮׷ֿה׮֮׷
  31. וֲ㼪Ⰵ׾鹌׭גְַֻ

  32. $IBU8PSL،فٔך植朐  ،فٔך؝٦سك٦أכ䎃湡ח瑱Ⰵ  ⢪׏גְ׷⚺זٓ؎ـٓٔ䪮遭  3FUSPMBNCEB  3Y+BWB 

    -JHIUXFJHIU4USFBN"1*  "OOPUBUJPO1SPDFTTPS  .VMUJEFY
  33. וַֿ׵,PUMJO׾⢪ְ㨣׭׷ַ  Ⰻגך؝٦س׾,PUMJOחׅ׷ךכ⯓ָ黅ְ  鿇ⴓ涸ח,PUMJO⻉׃גְֻ  $IBU8PSLדכאך%JBMPH'SBHNFOUַ׵  הג׮⽃秪זت؎،ؚٗ 

    如ח傀㶷ך؝٦س׾א,PUMJO⻉  ثٍحزٕ٦يך⡲䧭٥刿倜ך"DUJWJUZ  ׁ׵ח׉ך䖓ծ倜鋉堣腉؝٦س׾,PUMJOד鷄⸇  醱꧟זٗآحؙ׮Ⰵ׏גְ׷
  34. וֲװ׏גث٦يח嵴鷲ׇׁגְַֻ  ,PUMJOأة٦زـحؙ  ˑ鸐獥饔ץֿ劤˒׾Ⰻ㆞飑Ⰵ׃ג䩛⯋ח縧ֻ  爡ⰻך,PUMJOثٍحزד㉔褏崞⹛  +BWBה,PUMJOד剅ְ׋㜥さך䊴׾䫎珲 

    فؙٕٔؒأزח铡僇؝ًٝز׾♨㻝ח鎸鶢  +BWBדְֲהֿ׹ך⡦ַ  ,PUMJOך؎ر؍ؔيך铡僇  ٖؽُ٦䖓ثٍحزד椚鍑דֹ׋ַ然钠
  35. None
  36. None
  37. ث٦ي㼪Ⰵך铬겗

  38. ث٦يך稆㖑ה׃ג  כׄ׭ַ׵+BWBד؝٦س׾剅ְגְ׋  -BNCEB䒭׾䫺䫑זֻ「ֽⰅ׸׷ֿהָדֹ׋  -JHIUXFJHIU4USFBN"1*׾⢪׏גְ׋  0QUJPOBMך罋ִ倯ָ㕼〴ח֮׏׋ךדծ
 /VMMBCMFUZQFT

    /PO/VMMUZQFT׾
 أحה麩ㄤ䠬זֻ《׶鴥׬ֿהָדֹ׋  穠卓ה׃ג  ,PUMJOך؝٦سٖؽُ٦כ㉏겗זַ׏׋
  39. ׉׸ד׮  ث٦يⰋ㆞ָ,PUMJO׾غٔغٔ剅ֽגכְזְ  植㖈䩛ָֽגְ׷鿇ⴓך麩ְ׮֮׷ֽו׮  ⡦׾וֿתד,PUMJOד剅ַֻכ㹀ת׏גְזְ  ꬊず劍Ⳣ椚דBTZODBXBJU׾⢪׏גְַֻהַ 

    Ⰻ㆞ָ駈⚛׫׾䲧ִ׵׸׷ַוֲַ  铩ַ➂׌ָֽ,PUMJOח枠鹌׃גְֻ朐屣ָ
 ث٦يחה׏גծמְגכِ٦ؠ٦ך⣣⦼חז׷ַ  ,PUMJOך؝٦س׾頾⫈ח׃גכְֽזְ
  40. 䩛ָֽכׄ׭ה׃ג  ,PUMJO㼪Ⰵך⵸ח+BWBַ׵㨣׭ג׮葺ְַ׮  "OESPJE4UVEJPַ׵כ㢩鿇خ٦ָٕ♶銲חז׷  "OESPJE(SBEMF1MVHJO  EFTVHBSخ٦ٕثؑ٦ٝ 

    -BNCEB䒭װًاحسٔؿ؋ٖٝأָ⢪ִ׷  傀㶷فٗآؙؑزח,PUMJO׾㼪Ⰵׅ׷ז׵ל  㼭ֻׁ㨣׭׷  +BWBך؝٦سָ؎ً٦آדֹ׷ּ׵ְ㼭ׁז׮ך
  41. ֶ׃תְ