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

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

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

2017/07/12 Kotlin 開発 Tech Talks

Ryutaro Miyashita

July 12, 2017
Tweet

More Decks by Ryutaro Miyashita

Other Decks in Programming

Transcript

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

     "OESPJEط؎ذ؍ـ晛׾ٔٔ٦أ  䎃  せ̔せ⡤ⵖח  䎃剢  ,PUMJO醡ך傈㜠،فٔ׾爡ⰻ麊欽Ꟛ㨣  䎃剢  ,PUMJO׾فٗتؙءّٝח㼪Ⰵ
  2. ׉׸ד׮,PUMJO׾㼪Ⰵׅ׷椚歋  ؒٝآص،涸ז嚂׃ׁ  倜׃ְ׮ךח鍗׸׵׸׷ٌثك٦ءّٝ  ؒٝسِ٦ؠ٦ךًٔحزח׮ז׷鎉铂堣腉  Ꟛ涪ח꧊⚥׃װְׅ 

    ن؎ٓ٦فٖ٦ز׾㼰זֻדֹ׷  膷ⰻךⳢ椚؎ً٦آ׾؝٦سח⿾僥׃װְׅ  "OESPJEꟚ涪鎉铂ה׃גⰕ䒭ח؟ه٦ز  Ꟛ涪ך䩛ָ姺ת׏ג׃תֲ〳腉䚍ָ嶊ִ׋
  3. ن؎ٓ٦فٖ٦ز׾幾׵ֿׅהָדֹ׷  -BNCEB䒭  ًاحس׾א׌ֽ䭯א؎ٝة٦ؿؑ٦أ  4JOHMF"CTUSBDU.FUIPE5ZQF  -BNCEB䒭׾⢪׏גⱔꞿז鎸鶢ָ♶銲חז׷ 

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

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

    business logic }); "OESPJE+BWB -BNCEB 4JOHMF"CTUSBDU.FUIPEכ-BNCEB䒭׾⢪ֲֿהדأحؗٔה剅ֽ׷
  6. 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˒ؗ٦ٙ٦سד⿫撑 ׅ׷ֿה׮דֹ׷
  7. 䙼罋׾⿾僥׃װְׅ鎉铂堣腉  أ؝٦فꟼ侧  知患ז鎸鶢ך䩛⸔ֽ׾׃גֻ׸׷  ؎ً٦آ鸐׶ך؎ٝأةٝأך欰䧭ח⤑ⵃ  䭁䓸ꟼ侧 

    ְא׮ךⳢ椚׾ًاحسך״ֲח䪔ִ׷  傀㶷ךؙٓأח،سنحؙחًاحس׾鷄⸇  JG䒭 USZ䒭 XIFO䒭  ⦼׾鵤ֿׅהָדֹ׷
  8. 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 葺ֻ֮׷ؿ؋ؙزٔػة٦ٝ
  9. ,PUMJO أ؝٦فꟼ侧 fun newInstance(str: String) = MyFragment().apply { arguments =

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

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

    { return text == null || text.isEmpty(); } } OVMMת׋כ瑞俑㶵⴨ַוֲַ׾ثؑحؙ׃׋ְ
  12. ,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˒׾⢪׏גءؚٕٝزٝד㹀纏ׅ׷
  13. ,PUMJO 䭁䓸ꟼ侧 fun String.isNothing(): Boolean = this == null ||

    this.isEmpty() var test: String? = null test.isNothing() // true 㢌侧ך⚥魦ָˑOVMM˒ד׮/1&搀׃חثؑحָؙדֹ׷
  14. ת׋כծؽٕز؎ׁٝ׸גְ׷׮ך׾⢪ֲ @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ת׋כˑ瑞涯˒俑㶵ثؑحָؙדֹ׷
  15. +BWB JG俑 String text = "This is java"; String result;

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

    = if (text.length % 2 == 0) { "Even" } else { "Odd" } JGˑ䒭˒ה׃ג⦼׾鵤⽱ׅ׷ֿהָדֹ׷ךד 㢌侧㹑鎉儗ח⦼׾然㹀ׇׁ׵׸׷ JGFMTFぐـٗحؙך剑䖓ך遤ָ鐰⣣ׁ׸׷
  17. +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俑דכ⦼׾鵤ֿׅהָדֹזְךד؝٦سָ撕꧟חז׶װְׅ
  18. ,PUMJO XIFO䒭 fun onOptionsItemSelected(item: MenuItem?): Boolean = when(item?.itemId) { android.R.id.home

    -> { finish() true } else -> super.onOptionsItemSelected() } ,PUMJOחTXJUDI俑כזֻծ➿׻׶חXIFO䒭׾⢪ֲ
 XIFO䒭כ⽃秪ז瘝⣣嫰鯰⟃㢩ח׮侧⦼ךٖٝآ䭷㹀װ㘗ثؑحؙ׮〳腉
  19. ,PUMJOח׮蕱ְהֿ׹ָ֮׷  ًاحس侧  ًاحسקו㟓ִ׷  Lًاحسⵖꣲחעאַ׷〳腉䚍ָ넝ת׷  "OOPUBUJPO1SPDFTTPS 

    BQQCVJMEHSBEMF  BOOPUBUJPO1SPDFTTPS̔LBQUח㢌ִ׷  LBQUָ欰䧭ׅ׷؝٦سח㉏겗ָ֮׷ֿה׮֮׷
  20. וַֿ׵,PUMJO׾⢪ְ㨣׭׷ַ  Ⰻגך؝٦س׾,PUMJOחׅ׷ךכ⯓ָ黅ְ  鿇ⴓ涸ח,PUMJO⻉׃גְֻ  $IBU8PSLדכאך%JBMPH'SBHNFOUַ׵  הג׮⽃秪זت؎،ؚٗ 

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

    فؙٕٔؒأزח铡僇؝ًٝز׾♨㻝ח鎸鶢  +BWBדְֲהֿ׹ך⡦ַ  ,PUMJOך؎ر؍ؔيך铡僇  ٖؽُ٦䖓ثٍحزד椚鍑דֹ׋ַ然钠
  22. ׉׸ד׮  ث٦يⰋ㆞ָ,PUMJO׾غٔغٔ剅ֽגכְזְ  植㖈䩛ָֽגְ׷鿇ⴓך麩ְ׮֮׷ֽו׮  ⡦׾וֿתד,PUMJOד剅ַֻכ㹀ת׏גְזְ  ꬊず劍Ⳣ椚דBTZODBXBJU׾⢪׏גְַֻהַ 

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

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