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

Xamarin.Android で始めるクロスプラットフォームモバイルアプリ開発 #jxug

80d2fc532ffcaba52ff614bb12a30336?s=47 amay077
April 07, 2017

Xamarin.Android で始めるクロスプラットフォームモバイルアプリ開発 #jxug

2017/04/08 - Xamarin 勉強会 in 名古屋 with JXUG, MSP, JXUG学生支部 https://jxug.connpass.com/event/52683/ の発表資料です。タイトル以外は DroidKaigi2017 の資料とまったく同じです。

80d2fc532ffcaba52ff614bb12a30336?s=128

amay077

April 07, 2017
Tweet

Transcript

  1. Xamarin.AndroidͰ࢝ΊΔ
 ΫϩεϓϥοτϑΥʔϜ
 ϞόΠϧΞϓϦ։ൃ 2017.4.8 Xamarin ษڧձ in ໊ݹ԰ with JXUGֶੜࢧ෦,

    MSP @amay077
  2. About me • Ԟࢁ ༟ਈ - @amay077ʢ͋Ί͍ʣ • ۀ຿ܥϓϩάϥϚ+α •

    Ѫ஌ݝࡏॅͷϑϧϦϞʔτϫʔΧʔ • ஍ཧ৘ใγεςϜɺҐஔ৘ใϓϩάϥϛϯά • Android, iOS, Xamarin, Java, C#, Salesforce 
  3. Work for  • B2B޲͚PaaSɺ૔ݿ؅ཧɾӡૹۀ޲͚SaaS • AWS, Elasticsearch, Cassandra, Lambda,

    etc • શһ͕ϑϧϦϞʔτϫʔΧʔ ͓࢓ࣄ಺༰΍ϦϞʔτͰͷಇ͖ํͳͲڵຯ͋Δํɺؾܰʹฉ͍͍ͯͩ͘͞
  4. Target • Androidॳʙதڃऀ • iOSΞϓϦ΋։ൃͯ͠Δʗͦ͠͏ͳਓ • Xamarinͬͯฉ͍ͨ͜ͱ͋Δ͚Ͳ࣮ࡍԿͳͷʁ ͱࢥͬͯΔਓ • .NETܥͷ։ൃ͸͋·Γͨ͠ࣄ͕ͳ͍ਓ

    
  5. Agenda 1. Xamarinͱ͸ʁ 2. ௨ৗͷAndroid։ൃͱͷҧ͍ 3. C#ͷར఺ 4. X-PlatΞϓϦ։ൃͱίʔυͷڞ༗ 5.

    Xamarinͷ࢖͍Ͳ͜Ζ 
  6. Xamarinͱ͸ʁ ⏰ 45

  7. Xamarinͱ͸ʁ  /&5ϑϨʔϜϫʔΫʢ.POPʣ 9BNBSJO"OESPJE 9BNBSJOJ04 9BNBSJO .BD 9BNBSJO'PSNT Mono(.NETϑϨʔϜϫʔΫͷOSS࣮૷)Λ ج൫ͱͨ͠։ൃπʔϧΩοτ܈

  8. Xamarin.Androidͱ͸ʁ • C#͔ΒAndroid API, Java API͕ݺͼग़ͤΔ ʮബ͍ϥούʔʯ • Activity, Intent,

    ϦιʔεXML౳Λී௨ʹ࢖͑Δ • தؒίʔυ(MSIL)ͱMono VMΛΞϓϦ(apk) ʹ ಉࠝɺJava VMͱฏߦՔಇ 
  9. Xamarin.AndroidͷHello World  [Activity(Label = "AppTitle", MainLauncher = true, Icon

    = "@mipmap/icon")] public class MainActivity : Activity { protected override void OnCreate(Bundle state) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Main); var btn = FindViewById<Button>(Resource.Id.btn); btn.Click += (s, e) => btn.Text = "Hello World!"; } }
  10. Xamarin.Androidͷ࣮ߦϞσϧ  "OESPJE04 +BWB7. .POP7. 9BNBSJO"OESPJE.POP ΞϓϦ $  "OESPJE04

    +BWB7. "OESPJE4%, ΞϓϦ +BWB  +BWB4%, AndroidωΠςΟϒ Xamarin.Android
  11. Xamarin.iOSͱ͸ʁ • iOS SDK(CocoaTouch)͕C#͔Βݺͼ(ry • ViewController, Storyboard౳Λී௨ʹ࢖͑Δ • ࣄલ(AOT)ίϯύΠϥͰϚγϯޠΛੜ੒͠Ξϓ Ϧ(ipa)

    ʹಉࠝ 
  12. Xamarin.iOSͷ࣮ߦϞσϧ  "3. "3.Ϛγϯޠ $PDPB5PVDI 9BNBSJOJ04.POP ΞϓϦέʔγϣϯ $  .POP"05ίϯύΠϥ

    ࣮ػ Xamarin.iOS "3. "3.Ϛγϯޠ $PDPB5PVDI ΞϓϦέʔγϣϯ 4XJGU  --7.ίϯύΠϥ ࣮ػ iOSωΠςΟϒ
  13. Xamarin.Formsͱ͸ʁ • Android, iOS, Win, ͦͷଞͰUIͷ࣮૷Λڞ௨Խ ͢ΔϑϨʔϜϫʔΫ • ωΠςΟϒͷUI͕࢖༻͞ΕΔ •

    .NETͷσεΫτοϓΞϓϦ։ൃख๏ʹ͍ۙ (XAML<βϜϧ>Ͱը໘Λهड़ͳͲ) • ϚΠΫϩιϑτ͸͜Εਪ͠ 
  14. 

  15. ௨ৗͷAndroid։ൃͱ Xamarin.Androidͷҧ͍

  16. ౷߹։ൃ؀ڥ • Visual Studio 2017ʗfor Mac Λ࢖͏ • AndroidΞϓϦ։ൃʹಛԽͨ͠Android Studio

    ΑΓ͸एׯޮ཰͕མͪΔ(ओ؍) • UIσβΠφ • XMLϦιʔεͷΠϯϥΠϯදࣔɺ౳ • JetBrains Riderͱ͍͏৽IDE΋ʂ 
  17. ϏϧυγεςϜ • Gradle͸࢖༻Ͱ͖ͳ͍ • annotation processorΛ࢖͏πʔϧ΍ϥΠϒϥ Ϧ͸࢖༻Ͱ͖ͳ͍ • AndroidσʔλόΠϯσΟϯά •

    Orma, PermissionsDispatcherͱ͔ • .NETͷϏϧυγεςϜʮMSBuildʯΛ࢖͏ 
  18. ύοέʔδ؅ཧγεςϜ • maven, JitPackͳͲ͸࢖͑ͳ͍ • nuget(ʹΎʔ͛ͬͱ)Λ࢖͏ • ׂͱ؆୯ʹ୭Ͱ΋ϥΠϒϥϦΛొ࿥Մೳ 

  19. JavaͷϥΠϒϥϦ͸࢖͑Δʁ • “BindingϥΠϒϥϦ”Λ࡞ΔࣄͰ࢖༻Մೳ • “BindingϓϩδΣΫτ”ʹjarΛ์ΓࠐΉ͚ͩ • ෳࡶͳjarͩͱΧελϜϚοϐϯά͕ඞཁ • ஶ໊ͳJavaϥΠϒϥϦ͸طʹଘࡏ͢Δ •

    Google Play services, Support Libs • Picasso, Glide, OkHttp, LeakCanary, Lottie 
  20. C#ͷར఺ ⏰ 40

  21. ඇಉظॲཧ(async/await) • ΍ͬͺ͜Ε • Task, Task<T>Ϋϥε • async/awaitߏจ • Rxͱ૬ޓม׵Մೳ

    
  22. ඇಉظॲཧͷ࿈࠯(Java)  void sendZip(String src, String dest) // 1. σʔλΛμ΢ϯϩʔυͯ͠

    downloadAsync(src, data -> // 2. ZIPѹॖͯ͠ zipAsync(data, zipped -> // 3. ผͳͱ͜Ζʹૹ৴ sendAsync(zipped, dest, () -> activity.runOnUiThread(() -> // ૹ৴׬ྃ(UIεϨου) )))); } ωετͭΒ͍ɾɾɾ
  23. ඇಉظॲཧͷ࿈࠯(C#)  async void SendZip(string src, string dest) { //

    1. σʔλΛμ΢ϯϩʔυͯ͠ var data = await DownloadAsync(src); // 2. ZIPѹॖͯ͠ var zipped = await ZipAsync(data); // 3. ผͳͱ͜Ζʹૹ৴ await SendAsync(zipped, dest); // ૹ৴׬ྃ(UIεϨου) } ϑϥοτʂ
  24. ϥϜμࣜɺLINQ to Objects  var list=new[]{0,1,2,3,4,5,6,7,8,9}// 0ʙ9Λ .Where(x => x

    % 2 == 0) // ۮ਺ͷΈ .OrderByDescending(x => x) // ߱ॱ .Select(x => x * 10); // 10ഒ // ݁Ռ: 80 60 40 20 0 // ↑ͷ݁Ռͱ [40, 20, 10] ͱͷੵू߹Λऔಘ͢Δ var ret = list.Intersect(new[]{40,20,10}); // ݁Ռ: 40 20 ֎෦ϥΠϒϥϦʹཔΒͣͰ͖Δͷ͕ڧ͍ʂ
  25. ͪΐͬͱnull҆શʁ  // hoge ͕ null ͳΒ࣮ߦ͠ͳ͍ hoge?.DoSomething(); // hoge,

    fuga ͕ null ͳΒ “none” Λฦ͢ var text = hoge?.fuga?.ToString() ?? “none"; int count = null; // ίϯύΠϧΤϥʔ int? count = null; // null ୅ೖՄೳ ൥ࡶͳnullνΣοΫΛ؆ུԽͰ͖Δ
  26. vs Kotlin • ηϛίϩϯϨεͳͲͷ؆ܿͳߏจ • ڧྗͳnull҆શ • ܕਪ࿦ɺϥϜμࣜ • ύλʔϯϚον

    • ίϧʔνϯ(async/await)  C#͸.NETϑϨʔϜϫʔΫͱڞʹਐԽͰ͖ΔڧΈ ݴޠͷϞμϯͩ͞ͱKotlinͷউͪ
  27. X-PlatΞϓϦ։ൃͱ ίʔυͷڞ༗ ⏰ 30

  28. .NETϑϨʔϜϫʔΫͰڞ௨Խ • .NETͷΫϥεϥΠϒϥϦΛ࢖ͬͨίʔυ͸ AndroidɺiOSͰڞ༗Մೳ • ྫ:JavaͷArrayList<T> → .NETͷList<T>ɺ JavaͷCalendar →

    .NETͷDateTime  /&5ϑϨʔϜϫʔΫʢ.POPʣ 9BNBSJO"OESPJE 9BNBSJOJ04 9BNBSJO .BD
  29. .NET੡ϥΠϒϥϦΛ࢖͓͏ • ετϨʔδ - PCLStorage, Stream • σʔλϕʔε - SQLite-net,

    Realm Xamarin • ௨৴ - HttpClient • JSON - Json.NET • mBaaS - Azure, AWS, Firebase 
  30. MVVM+RxʹΑΔ ϞμϯͳΞϓϦ։ൃ

  31. MVVMύλʔϯ • View: UIύʔπͷ഑ஔͱόΠϯσΟϯά • ViewModel: ViewͷͨΊͷσʔλͱॲཧ • Model: ্هҎ֎શͯ

     7JFX 7JFX .PEFM .PEFM σʔλόΠϯσΟϯά Method Call Notify
  32. RxͱReactive Programing • Rx = Reactive Extensions • LINQͷඇಉظόʔδϣϯ •

    RxJava, RxSwiftͳͲ΁Ҡ২͞Εͨ • RxJavaͷਐԽ͸RxΑΓૣ͍ • ํݴ͸͋ΕͲɺجຊ͸ಉ͡ • Reactive Programing͸ࠓ΍ඞਢεΩϧ 
  33. XamarinʹΑΔ “X-Plat” MVVM+RxΞϓϦ։ൃ ⏰ 30

  34. Xamarin͸ MVVM΍Rxͱ૬ੑ͕Α͍ʂ • Rx͸ɺϚΠΫϩιϑτൃͷOSS͔Βීٴ • MVVM͸ɺ௕೥ͷσεΫτοϓΞϓϦΞʔΩ ςΫνϟͷ੒Ռ(Windows.Forms → WPF) •

    .NETʹ͸ɺ஌ݟΛ࣋ͬͨਓ΍ؔ࿈ϑϨʔϜ ϫʔΫ͕๛෋ʹଘࡏ 
  35. MVVMϑϨʔϜϫʔΫ • Prism.Forms
 ࠷΋Hotͳਖ਼౷೿ϑϧελοΫϑϨʔϜϫʔΫ • MvvmCross
 ݹ͔͘ΒX-Plat MVVMΛࢧ͖͑ͯͨ
 NHKߚനΞϓϦͰ࠾༻ •

    ReactiveUI
 MVVMʹRxΛ࣋ͪࠐΜͩ࠷ॳͷϑϨʔϜϫʔΫ 
  36. ReactiveProperty • ModelͷStreamΛViewʹ”ܨ͙”ϥΠϒϥϦ • ॳظ։ൃऀ͸UniRxͷ࡞ऀ(a.k.a “Rxਆ”) • JavaʹҠ২ͨ͠ “RxProperty Android”

    ΋͋ΔΑ 
  37. MVVM+RxͰ࡞ͬͯΈͨ ͓୊: GPSड৴ΞϓϦ  (14ͷҢ౓ܦ౓Λ ඵ͓͖ʹߋ৽ ։࢝ͱఀࢭ ʮ౓ʯ͔ʮ౓෼ඵʯʹ ੾Γସ͑

  38. Android-JavaͰͷ࣮૷ύλʔϯ  .BJO"DUJWJUZ .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ 5FYU7JFX

    දࣔܗࣜ 4XJUDI TUBSU 3Y+BWB MPDBUJPO 0CTFSWBCMF MPDBUJPO ։࢝ #VUUPO PO$IFDLFE PO$MJDL "OESPJEσʔλόΠϯσΟϯά -PDBUJPO"QJ -PDBUJPO.BOBHFS 'VTFE-PDBUJPO"QJ
  39. iOS-SwiftͰͷ࣮૷ύλʔϯ  .BJO7JFX$POUSPMMFS .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ 6*5FYU'JFME

    දࣔܗࣜ 6*4XJUDI TUBSU 3Y4XJGU MPDBUJPO 0CTFSWBCMF MPDBUJPO ։࢝ 6*#VUUPO PO4XJUDI TUBSU 4XJGU#POE -PDBUJPO"QJ $--PDBUJPO.BOBHFS
  40. Xamarin.FormsͰͷ࣮૷ύλʔϯ  .BJO1BHF .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ -BCFM

    දࣔܗࣜ 4XJUDI 4UBSU 3FBDUJWF&YUFOTJPOT -PDBUJPO 0CTFSWBCMF -PDBUJPO ։࢝ #VUUPO *T%NT'PSNBU 4UBSU 9BNBSJO'PSNTσʔλόΠϯσΟϯά -PDBUJPO"QJ -PDBUJPO.BOBHFS <Inject> -PDBUJPO"QJ $--PDBUJPO.BOBHFS
  41.  .BJO1BHF .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ -BCFM දࣔܗࣜ

    4XJUDI 4UBSU 3FBDUJWF&YUFOTJPOT -PDBUJPO 0CTFSWBCMF -PDBUJPO ։࢝ #VUUPO *T%NT'PSNBU 4UBSU 9BNBSJO'PSNTσʔλόΠϯσΟϯά -PDBUJPO"QJ -PDBUJPO.BOBHFS <Inject> -PDBUJPO"QJ $--PDBUJPO.BOBHFS AndroidσʔλόΠϯσΟϯά SwiftBond ↓ Xamarin.Forms σʔλόΠϯσΟϯά Xamarin.FormsͰͷ࣮૷ύλʔϯ
  42.  .BJO1BHF .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ -BCFM දࣔܗࣜ

    4XJUDI 4UBSU 3FBDUJWF&YUFOTJPOT -PDBUJPO 0CTFSWBCMF -PDBUJPO ։࢝ #VUUPO *T%NT'PSNBU 4UBSU 9BNBSJO'PSNTσʔλόΠϯσΟϯά -PDBUJPO"QJ -PDBUJPO.BOBHFS <Inject> -PDBUJPO"QJ $--PDBUJPO.BOBHFS RxJava RxSwift ↓ Reactive Extensions Xamarin.FormsͰͷ࣮૷ύλʔϯ
  43.  .BJO1BHF .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ -BCFM දࣔܗࣜ

    4XJUDI 4UBSU 3FBDUJWF&YUFOTJPOT -PDBUJPO 0CTFSWBCMF -PDBUJPO ։࢝ #VUUPO *T%NT'PSNBU 4UBSU 9BNBSJO'PSNTσʔλόΠϯσΟϯά -PDBUJPO"QJ -PDBUJPO.BOBHFS <Inject> -PDBUJPO"QJ $--PDBUJPO.BOBHFS andoid:LocationaManager ios:CLLocationManager ↓ ϓϥοτϑΥʔϜݻ༗APIΛ ࢖࣮ͬͨ૷ΛDI (Dependency Injection) Xamarin.FormsͰͷ࣮૷ύλʔϯ
  44.  .BJO1BHF .BJO7JFX.PEFM ViewModel View Model -PDBUJPO6TFDBTF Ң౓ܦ౓ -BCFM දࣔܗࣜ

    4XJUDI 4UBSU 3FBDUJWF&YUFOTJPOT -PDBUJPO 0CTFSWBCMF -PDBUJPO ։࢝ #VUUPO *T%NT'PSNBU 4UBSU 9BNBSJO'PSNTσʔλόΠϯσΟϯά -PDBUJPO"QJ -PDBUJPO.BOBHFS <Inject> -PDBUJPO"QJ $--PDBUJPO.BOBHFS ڞ௨ԽͰ͖ͳ͍ͷ͸͚ͩ͜͜ Xamarin.FormsͰͷ࣮૷ύλʔϯ
  45. X-Platڞ௨ԽͰ͖ΔʁͰ͖ͳ͍ʁ • ViewModel૚ɿ׬શʹڞ௨ԽͰ͖Δ • Model૚ • Usecaseɿ׬શʹڞ௨ԽͰ͖Δ • APIɿҰ෦ڞ௨ԽͰ͖ΔɺͰ͖ͳ͚Ε͹DI •

    View૚ɿҰ෦ڞ௨ԽͰ͖ΔɺͰ͖ͳ͚Ε͹DI  ⏰ 20
  46. ڞ௨Խࣄྫ1: B2B޲͚ ं᫑ӡߦ؅ཧΞϓϦ  • ໿20ը໘ • ڞ௨Ͱͳ͍ॲཧ: GPS, Push௨஌,

    Ի੠࠶ੜ, Toast౳ • ૯ίʔυߦ਺: 22,816 Android:9% 2,134ߦ iOS:6% 1,407ߦ ڞ௨:85%
 19,275ߦ
  47. ڞ௨Խࣄྫ2: ΤΠνɾΤεূ݊ εϚג  • ໿70ը໘ • View͸C#ɺViewModelͱModel͸ F# Λ࢖༻

    • ૯ίʔυߦ਺: 72,445 Android:3% 2,130ߦ iOS:4% 2,897ߦ ڞ௨:93% 67,418ߦ JXUG#17ʮূ݊औҾΞϓϦͱNote app࡞ͬͯΈͨʯΑΓ
  48. Open Xamarin, ɹɹOpen Microsoft • Xamarin.Android .iOS .Forms ͸શͯOSS •

    .NET Core • .NETϑϨʔϜϫʔΫ(ͷҰ෦)ΛOSS&X-PlatԽ • AWS Lambda΍GCP͕.NET/C#ʹରԠ  ϚΠΫϩιϑτ = ϓϩϓϥΠΤλϦ .NET = Windows ͸΋͸΍ඇৗࣝʂ
  49. Xamarinͷ࢖͍Ͳ͜Ζ ʙۜͷ஄ؙ͸ͳ͍ΑͶʙ

  50. Xamarin͕޲͍͍ͯͳ͍έʔε • AndroidɺiOSͷτοϓΤϯδχΞ͕ډΔ
 ˠAndroidͷΤʔεʹXamarin͸ෆཁ • ΞϓϦͷ഑෍αΠζΛؾʹ͢ΔϓϩμΫτ
 ˠ.NETϥϯλΠϜΛಉࠝ͢ΔͷͰ༰ྔ૿͑Δ • फڭ্ͷཧ༝
 →͏Μ͏ΜͦΕ΋͋ΔΑͶ

    
  51. XamarinΛ࢖͏΂͖έʔε • B2BͳϞόΠϧɾλϒϨοτΞϓϦ • Win, Android, iOSͰڞ௨ϩδοΫΛ࠷େԽ • ӡ༻อकίετ㽊ɺཁһ֬อ •

    ελʔτΞοϓاۀ • ϏδωεϞσϧݕূ༻ʹαΫοͱ։ൃ • AndroidͱiOSࢢ৔ʹಉ࣌౤ೖ • ϑΟʔυόοΫɾϧʔϓΛૉૣ͘ճͤΔ 
  52. ·ͱΊ

  53. Xamarin.AndroidͰ࢝ΊΔ
 ɹɹɹɹX-PlatϞόΠϧΞϓϦ։ൃ 1. JavaͷAndroidΞϓϦΛɺXamarin.Android ʹҠ২ͯ͠ΈΑ͏ 2. iOSΞϓϦΛXamarin.iOSͰ࡞ͬͯΈΑ͏ 3. ڞ௨ϩδοΫΛ.NETͰॻ͖௚ͯ͠ΈΑ͏ 4.

    Xamarin.FormsͰը໘΋ڞ௨ʹͯ͠ΈΑ͏ 5. MVVM΍RxΛద༻ͯ͠ΈΑ͏ 
  54. Android ΋ iOS ΋ .NET ΋ͥΜͿֶΜͰ C# Ͱ D.R.Y ͢Δͷ͕

    Xamarin
  55. ଓ͖͸ʮٕज़ॻయ̎ʯͰʂ • ͏-13ʮXamaritansʢ͟·ΓͨΜͣʣʯ • Xamarinͷ࠷ઌ୺৘ใΛ·ͱΊͨຊΛ഑෍͠·͢ • ௒ٕज़ॻయ(in χίχί௒ձٞ2017)΋ʂ 

  56. ͝ਗ਼ௌ ͋Γ͕ͱ͏͍͟͝·ͨ͠$