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

Let's refactoring my old app with new android tech !

Fc219a5bcf4255fb423a4e5761fc2679?s=47 hammer0802
December 16, 2021
190

Let's refactoring my old app with new android tech !

at 【YUMEMI x Sansan Tech Meetup】Android開発におけるチャレンジを語る, 2021/12/16

Fc219a5bcf4255fb423a4e5761fc2679?s=128

hammer0802

December 16, 2021
Tweet

Transcript

  1. ۦ͚ग़͠ͷࠒ࡞ͬͨAndroidΞϓϦΛ ࠷৽ٕज़ͰϦϑΝΫλϦϯάͯ͠Έͨ Takanori Sato@ YUMEMI Inc. 2021/12/16 ʲYUMEMI x Sansan

    Tech MeetupʳAndroid։ൃʹ͓͚ΔνϟϨϯδΛޠΔ
  2. ࣗݾ঺հ 2

  3. ࠤ౻ɹوൣ ✦ ͸Μ·ʔͱ͍͏HNͰ׆ಈ ✦ 2019೥8݄גࣜձࣾΏΊΈத్ೖࣾ ✦ લ৬͕ϝΠϯϑϨʔϜΤϯδχΞ(ҟۀछ͔Βస৬) ✦ AndroidΤϯδχΞྺ3೥໨ ✦

    1ࣇͷύύ👶 ✦ Twitter : @watashi_desu_82 ✦ Qiita : @hammer0802 3
  4. ࠓ೔࿩͢͜ͱ ✦ۦ͚ग़࣌͠୅ʹ࡞ͬͯ์ஔͨ͠ΞϓϦͷϦϑΝΫλϦϯάʹ νϟϨϯδͨ͠࿩ ✦ͦͷࡍʹಘͨ࠷৽ٕज़ͷ஌ݟ΍tips ✦Jetpack Compose, Dagger HiltͳͲ ✦ࠓճͷνϟϨϯδͰײͨ͜͡ͱ 4

  5. લஔ͖ 5

  6. 6 օ͞Μɺۦ͚ग़͠ΤϯδχΞͷࠒͲΜͳ ίʔυΛॻ͍͍͔ͯͨࢥ͍ग़ͤ·͔͢ʁ

  7. 7 ͲΜͳ͜ͱΛ΍͍͔ͬͯͨ͸ࢥ͍ग़ͤͯ΋ɺͲΜͳ ίʔυ͔ͩͬͨ͸खݩʹͳ͍ͱࢥ͍ग़ͤͳ͍Ͱ͢ΑͶ

  8. 8 ࢲɺΘ͔Γ·͢(ެ։ͯ͠·͢)

  9. SUBWAYγϛϡϨʔλʔ ✦Google Play Store ✦https://play.google.com/store/apps/ details?id=com.hammer.app.subwaysimulator ✦GitHub ✦https://github.com/hammer0802/SubwaySimulator ✦SUBWAYͷαϯυ΢ΟονͷϨγϐΛ࡞੒͢ΔΞϓϦ ✦લ৬(ඇϓϩάϥϚʔ৬)࣌୅ʹݸਓͰ։ൃ

    ✦ΞΠίϯ΋ؤுͬͯඳ͍ͨ 9
  10. 10 ΞϓϦެ։·Ͱ͍͚ͨ͠ɺศརػೳ΋ೖΕΒΕͨ ॳΊͯͷΞϓϦ։ൃʹͯ͠͸্ग़དྷͩΖ͏ (ࣗըࣗࢍ) ౰࣌ͷࢲ͸͜͏ࢥ͍ͬͯͨʜ

  11. ποίϛͲ͜Ζ͔͠ͳ͍ σʔλߏ଄ શ ෦xm lͰ ॻ ͘ ε τ ϩ

    ϯ ά ε λ Π ϧ Ϩ Π Ξ ΢ τ શͯͷॲཧΛߦ͏ ਆActivity apply͕ σ Χ ա ͗ Μ ͩ Ζ ʜ 11 ͦͯ͠ݱࡏ ࢲ͸ݱ࣮Λ஌ͬͨ ΞʔΩςΫνϟ͕ ଘࡏ͠ͳ͍ੈքઢ ม਺ʹ਺ࣈೖΕͪΌ͏
  12. 12 աڈͱܾผ͢ΔͨΊ ಆ͍(ϦϑΝΫλϦϯά)͸࢝·ͬͨʜ

  13. ຊ୊ 13

  14. ໰୊఺ͷચ͍ग़͠(Ұ෦) ✦ΞʔΩςΫνϟ͕ͳ͍ ✦ActivityͰը໘ॲཧҎ֎ʹ΋σʔλอଘͳͲશॲཧ΍ͬͯΔ ✦ભҠ͕શ෦startActivity ✦๲େͰ൥ࡶͳϨΠΞ΢τ ✦etc… 14

  15. ରԠࡦͷݕ౼ - ࠓͬΆͦ͏ͳ΋ͷΛยͬ୺͔Β࢖͍ͬͯ͘ ✦ΞʔΩςΫνϟ͕ͳ͍ ✦AAC(Android Architecture Component)ϥΠϒϥϦ܈Λ࢖༻ͨ͠MVVM ✦ActivityͰը໘ॲཧҎ֎ʹ΋σʔλอଘͳͲશॲཧ΍ͬͯΔ ✦ViewModel, RoomͳͲΛ࢖༻ͯ͠ॲཧΛ෼཭

    ✦DI͸Dagger HiltΛ࠾༻ ✦ભҠ͕શ෦startActivity ✦NavigationͰભҠॲཧ ✦๲େͰ൥ࡶͳϨΠΞ΢τ ✦Jetpack ComposeΛ࢖ͬͯϨΠΞ΢τߏங 15
  16. ૣ଎ϦϑΝΫλϦϯά։࢝ʂ …ͱࢥͬͨΒ 16 TargetSdkVersion্͛ͨΒ support library͔Β AndroidXʹҠߦ͠ΖͱݴΘ ΕͯΔɻϨΠΞ΢τͷྔ΋ଟ ͍͠ɺେมͦ͏ͩͳ͊ʜ

  17. ௒؆୯ͩͬͨ 17 ίʔυ΍ϨΠΞ΢τ΋ࣗಈͰAndroidXʹҠߦ ΄΅खमਖ਼ແ͠ͰϏϧυͰ͖ͨ🎉

  18. 18 σʔλߏ଄ͷमਖ਼΍σΟϨΫτϦߏ੒ͷݟ௚͠ͳ ͲɺϥΠϒϥϦʔΛ࢖Θͳ͍मਖ਼͸লུͯ͠ʜ

  19. Step1. ϨΠΞ΢τΛ࠶ߏங ✦Jetpack ComposeΛ࢖͏ ✦એݴతUI ✦ϨΠΞ΢τϑΝΠϧ(xml)ෆཁ ✦2021೥7݄ʹstable🎉 19

  20. Jetpack Composeͷجຊతͳ࢖͍ํ ✦setContent ϒϩοΫ಺ʹίϯϙʔβϒϧΛݺͼग़ ͯ͠ϨΠΞ΢τΛఆٛ͢Δ ✦onCreate಺Ҏ֎ʹ΋ɺComposeView͔Β set΋Ͱ͖Δ ✦ίϯϙʔβϒϧͱ͸ @Composable Λ͚ͭͨؔ਺

    ✦@Preview Λ͚ͭΔͱӈʹϓϨϏϡʔදࣔ ✦Modifier ͰαΠζͳͲΛௐ੔͠ɺϨΠΞ΢τΛߏ ੒͢Δ 20
  21. Ϧετදࣔ΋ʜ ౰࣌Α͘Θ͔Βͳ͍··࣮૷͍ͯͨ͠RecyclerViewॲཧ 21

  22. LazyColumnͰͱͯ΋γϯϓϧʹʂ 22

  23. ঢ়ଶΛίϯϙʔβϒϧʹ౉͢͜ͱͰঢ়ଶʹԠ͡ ͨߋ৽͕Ͱ͖Δ ✦remember Λ࢖ͬͯঢ়ଶΛ ࠶ίϯϙʔζޙ΋อଘ ✦LiveData΍FlowͳͲͷ observableͳܕ͔ΒStateΛ ࡞੒Մೳ 23

  24. AndroidViewΛ࢖ͬͯComposeͰ·ͩར༻Ͱ͖ͳ ͍UIཁૉΛදࣔͰ͖Δ ✦AdView΍MapViewͳͲ͸ ·ͩར༻Ͱ͖ͳ͍ ✦AndroidViewͷfactoryΛ ࢖ͬͯදࣔՄೳ 24

  25. Step2. ViewModelΛಋೖ ✦Dagger HiltΛ׆༻͍ͨ͠ ✦DI(Dependency Injection)ϥΠϒϥϦʔ ✦DIͷఆ൪ͩͬͨDagger2 Λ͞Βʹվྑ ✦2021೥5݄ʹstable🎉 25

  26. DIͷϋʔυϧ͕֨ஈʹԼ͕ͬͨ ✦ApplicationΫϥεʹ@HiltAndroidApp Λ͚ͭΔ ✦ݺͼग़͠ଆͷActivityͳͲͰ @AndroidEntryPointΛ͚ͭΔ ✦Inject͍ͨ͠΋ͷ͸constructorͷલʹ @InjectΛ͚ͭΔ ✦ViewModelͷ৔߹͸ߋʹ @HiltViewModelΛ͚ͭΔ ✦େମ͜ΕͰͰ͖ͪΌ͏

    26
  27. Hilt͸Jetpack ComposeͱҰॹʹ࢖͑Δ ✦viewModel()Ͱίϯϙʔβϒϧ ͷҾ਺Ͱ΋औΕΔ ✦Composeʹ͓͚ΔHilt͸ NavigationͱϥΠϒϥϦ͕౷߹ ͞Ε͍ͯΔ ✦hilt-navigation-compose 27

  28. Step3. ભҠಋઢͷܨ͗ࠐΈ ✦NavigationͰ֤ը໘ʹભҠ ✦Ҏલ͸destinationΛxml Ͱఆ͍ٛͯͨ͠ ✦ComposeͰ͸Kotlinίʔ υͰఆٛͰ͖Δ 28

  29. NavigationΛ࢖ͬͨભҠ ✦NavHostΛ࣋ͬͨίϯϙʔβϒϧΛఆٛ ✦composableͷrouteͰߦ͖ઌΛࢦఆ ✦hiltViewModelͰ@HiltViewModel ͕͍ͭͨViewModelΛऔಘͰ͖Δ ✦NavHostControllerͰߦ͖ઌ΁ navigate ✦NavHostController͕ࢀর͢Δඞཁ ͕͋Δ͢΂ͯͷίϯϙʔβϒϧ͕ΞΫ ηεͰ͖ΔҐஔʹஔ͘

    29
  30. શը໘΁ͷಋઢ͕ͭͳ͕ͬͨ🎉 30

  31. ϦϑΝΫλϦϯάνϟϨϯδ Λܦͯ 31

  32. ओʹJetpack ComposeͰײͨ͡ϝϦοτ ✦ϦετͳͲ͔ͳΓ؆ܿʹॻ͚Δ෦෼͕ଟ͔ͬͨ ✦ίʔυྔ͕શମతʹݮͬͨ(ޙड़) ✦એݴతUI͕௚ײతͰॳ৺ऀͷϋʔυϧ͕௿ͦ͏ ✦ࠓճॳΊͯ৮Ε͕ͨͱ͖ͬͭ΍͍͢ҹ৅ͩͬͨ(Flutter΍͔ͬͯͨΒ͔΋͠ Εͳ͍͚ΕͲ) ✦طଘΞϓϦͷ1ը໘͚ͩɺ·ͨ͸ը໘ͷҰ෦෼͚ͩJetpack ComposeͰϦϑΝ ΫλϦϯά͕Ͱ͖Δ

    ✦εΫϥοϓ&ϏϧυͰ1͔Β࡞Βͳͯ͘΋͍͍ 32
  33. ͲΕ͘Β͍ίʔυྔ͕ݮ͔ͬͨ ✦ϨΠΞ΢τϑΝΠϧશ෦ফ͠ ͨͷͰ͍ͩͿݮͬͨ ✦ࠓճؒʹ߹ΘͣʹҰ୴ফͨ͠ ॲཧ΋͋ΔͷͰɺͦΕΛ෮׆ ͤͨ͞Β݁ߏ૿͑Δʁ ✦͕ͩݮগΛ௒͑Δ͜ͱ͸ͳ͞ ͦ͏ 33

  34. (ݸਓతʹײͨ͡)Jetpack ComposeͷΠϚΠνͳ఺ ✦ͦͷίϯϙʔβϒϧɺͦ͜(Material Design)ʹແ͚Ε͹ແ͍Ͱ͢Ͷ ✦SpinnerͳͲੲ͸͋ͬͨ΋ͷΛࣗલͰ࣮૷͢Δඞཁ͕͋ͬͨ ✦·ͩඍົʹᙱ͍ͱ͜Ζʹಧ͔ͳ͍ ✦TextFieldʹmaxLengthͳͯ͘ڻ͍ͨ ✦Previewؔ࿈ ✦ҰʑϏϧυ͕ඞཁɺFlutterͷHot Reload͕࿀͘͠ͳͬͨ

    ✦(࣮૷ͷ͍͔ͤ΋͠Εͳ͍͕)Previewͷද͕ࣔյΕ͍ͯΔ΋ͷ͕͋ͬͨ ✦NavigationͷΞχϝʔγϣϯͳͲɺଟ͘ͷ։ൃऀ͕࢖͍͍ͨͰ͋Ζ͏ػೳ͕·ͩఏڙ͞Ε͍ͯͳ͍ ✦Accompanistͱ͍͏ิॿϥΠϒϥϦʔͰఏڙ͞ΕͯΔ͕ɺʮকདྷతʹػೳ͕ഇࢭ͞ΕΔՄೳੑ͋Γ·͢ʯͱॻ͔ Ε͍ͯͯखΛग़ͮ͠Β͍ ͱݴ͍ͭͭɺ্هͷ఺Λ௒͑ͨϝϦοτ͸ײ͡ΒΕͨʂ 34
  35. ࠓޙͷ༧ఆ ✦ؒʹ߹Θͳ͔ͬͨRoomͷಋೖ΍ɺҰ୴ফͨ͠ॲཧΛ෮׆ͤ͞ Δ ✦σβΠϯΛࠓͬΆ͍ͨ͘͠(Material YouͳͲ) ✦ࠓޙ΋͜ͷΞϓϦΛ৽ٕज़ͷ༡ͼ৔ֶशࡐྉͱͭͭ͠ϒϥο γϡΞοϓ͍͖͍ͯͨ͠ 35

  36. ࠓճͷνϟϨϯδͰײͨ͜͡ͱ ✦ੲͷίʔυΛݟͯʮμϝͩͳʙʯͱࢥ͑ΔΑ͏ʹͳͬͨࣗ෼ͷ੒ ௕ ✦ίʔυͷΫΦϦςΟͱͯ͠͸ΠϚΠνͰ΋ɺΞϓϦϦϦʔε·Ͱ ͍͚࣋ͬͯͨੲͷࣗ෼ͷ೤ҙ ✦࠷৽ٕज़ʹ৮Εͨ͜ͱʹΑΔAndroidΞϓϦ։ൃ΁ͷϞνϕʔ γϣϯΞοϓ ✦AndroidΞϓϦ։ൃ͸ɺ΍ͬͺΓָ͍͠ʂ 36

  37. 37 օ͞Μ΋ۦ͚ग़࣌͠୅ͷίʔυΛݟฦͯ͠ɺ ॳ৺ʹฦͬͯΈͯ͸͍͔͕Ͱ͠ΐ͏͔

  38. 38 Fin...?

  39. 39 ݸਓΞϓϦͰࢼ͚ͩ͢͡Όͳͯ͘ɺۀ຿Ͱ ࠷৽ٕज़Λ࢖ͬͯษڧ͍ͨ͠ͳʙ ※͜Ε͸CMͰ͢

  40. 40 ͦΕɺΏΊΈͰͰ͖·͢Αʁ ※͜Ε͸CMͰ͢

  41. ΏΊΈͰ͸Androidͷ࠷৽ٕज़Λ࢖͑Δɾֶ΂Δػձ͕ͨ͘͞Μʂ ✦ࠓճ঺հͨ͠࠷৽ٕज़Λ࠾༻ͨ͠Ҋ݅ଟ਺͋Γʂ ✦FlutterΛ׆༻ͨ͠಺੡ԽࢧԉΛຊ֨։࢝ʂ ✦֤࠷৽ٕज़ͷϫʔΩϯάάϧʔϓͰ஌ࣝΛਂ۷Γʂ ✦ຖ೔1ͭ͸ࣾ಺ษڧձΛ։࠵ʂ 41 ※͜Ε͸CMͰ͢ ͋ͳͨ΋ΏΊΈͰര଎੒௕͠·ͤΜ͔ʁ

  42. 42 Fin