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

Swift Package centered project - Build and Practice

2594ac7ce91fd7d9a3ce71ca7cc2d0c0?s=47 d_date
September 19, 2021

Swift Package centered project - Build and Practice

Swift Package中心のプロジェクト構成とその実践 - iOSDC JAPAN 2021
https://www.youtube.com/watch?v=e_T1-XrYf4A

2594ac7ce91fd7d9a3ce71ca7cc2d0c0?s=128

d_date

September 19, 2021
Tweet

Transcript

  1. Swift Packageத৺ͷ ϓϩδΣΫτߏ੒ͱͦͷ࣮ફ iOSDC JAPAN 2021 Daiki Matsudate @d_date

  2. ٻΉ! Feedback #iosdc #c

  3. 🍣 #iosdc #c

  4. લఏͷ֬ೝ Swift Package Manager Xcode Package.swift Multi Module Multi Project

    SwiftUI UIKit Bundle Test Target
  5. લఏͷ֬ೝ Swift Package Manager Xcode Package.swift Multi Module Multi Project

    SwiftUI UIKit Bundle Test Target Fundamental Advanced
  6. લఏͱͳΔ஌ࣝͷ֬ೝ

  7. લఏͱͳΔ஌ࣝͷ֬ೝ • Buildʹ͍ͭͯ • Target • Con fi guration •

    Scheme • Module / Framework • Import / Link • Static / Dynamic • XCFramework
  8. Build Source Code Object Code Executable Binary Compiler Compile Link

    Linker .o Build xcodebuild
  9. Build Object Code DerivedData/*/Build/Intermediates.noindex/*.build/<env>/*.build/Object-normal/<Arch>/

  10. Build Target • Buildʹ༻͍ΔϑΝΠϧʢInput) ΍ੜ੒෺ʢOutput)Λఆٛͨ͠΋ͷ • Input: Source code, Framework,

    Resource • Output: App, App Extension, Framework
  11. Build Configuration • Build SettingsʢϏϧυʹؔ͢Δม਺ͷू߹ʣͷϓϦηοτ

  12. Build Scheme Target Configuration X

  13. Build Scheme • Build͸5छྨ • Analyze • Test • Run

    • Pro fi le • Archive Debug Release
  14. Build Scheme Target Con fi guration Debug Release X Build

    Analyze Run Test Pro fi le Archive
  15. Module? Framework?

  16. Module import͕Ͱ͖Δ୯Ґ

  17. Module

  18. Module • Derived Dataʹ.swiftmoduleͱ͍͏Bundle͕ଘࡏ͢Δ͜ͱ
 ※Bundle … σΟϨΫτϦ͚ͩͲϑΝΠϧͷΑ͏ʹৼΔ෣͏ܗࣜ • ιʔείʔυ͕ίϯύΠϧ·Ͱ͞ΕΔͱར༻ՄೳʹͳΔ

  19. Module • Import͕Ͱ͖Δ୯Ґ (࣮ମ͸.swiftmodule) • moduleʹ͸public APIؚ͕·ΕΔ • Module Stability

    (swift 5.1~) • ҟͳΔSwiftͰੜ੒͞ΕͨϞδϡʔϧΛimportͰ͖Δʢ.swiftinterface)
  20. Framework Build TargetͱϦϯΫ͢Δ ࣮ߦՄೳόΠφϦ

  21. Framework • Build TargetͱLink͢Δ࣮ߦՄೳόΠφϦ • Framework͸Build Targetʹ͢Δ͜ͱ͕Ͱ͖ΔʢModule͸Ͱ͖ͳ͍ʣ
 →Build SchemeΛઃఆͰ͖Δ

  22. FrameworkͷLink • ίϯύΠϧ͞ΕͨObject CodeΛ૊Έ߹Θ͍ͤͯ͘࡞ۀ • ૊Έ߹ΘͤΔࡍʹɺsymbolͷॏෳΛ֬ೝ͢Δ • Linkͷ࢓ํʹ͸2छྨ • Static

    Link -> Static Framework • Dynamic Link -> Dynamic Framework
  23. Static Link Ϗϧυ࣌ʹObject codeΛͻͱͭʹ·ͱΊ͍ͯ͘ Link Link Link Link .o .o

    .o
  24. Load on Launch Dynamic Link ΞϓϦͷόΠφϦͱผʹੜ੒͠ɺΞϓϦ࣮ߦ࣌ʹಡΈࠐΉ Link Link Link .o

    .o .o
  25. Static Link vs Dynamic Link ΞϓϦͱͷLink ىಈ࣌ؒ Static Framework Ϗϧυͷͱ͖

    ୹͍ Dynamic Framework ΞϓϦىಈ࣌ ௕͍
  26. Link in Module Link͸Module୯ҐͰߦΘΕΔ Link .o .o .o .o .o

    .o .o .o .o
  27. XCFramework • ഑෍ՄೳͳBundleܗࣜ • ෳ਺ͷFramework (ϏϧυࡁΈόΠφϦʣؚ͕·ΕΔ • Platform (iOS, macOS,

    tvOS and watchOS) • Simulator / Device • Static / Dynamic • Build Architecture (x86_64, arm64, …)
  28. ͜ͷτʔΫͰ࿩͞ͳ͍͜ͱ • Objective-C੡ͷFramework • Module map • Bridging-Header • Library

    (Frameworkͱݫີʹ۠ผ͠ͳ͍ʣ • .a • .dylib
  29. https://www.youtube.com/watch?v=FZoYyAEPJ8w iOSDC Japan 2019: ϥΠϒϥϦͷΠϯϙʔτͱϦϯΫͷ࢓૊ΈΛ୳Δ / Kishikawa Katsumi See also

  30. Homework https://twitter.com/kateinoigakukun/status/1405130382656163840?s=20 A, B, Cをアプリターゲット、静的ライブラリ、動的ライブラリのいずれかとする。A, B, Cの依存関係 が、 A ->

    B, A -> C, C -> Bとなるとき、Aのリンク時にSymbolが重複するかを考える。 A, B, Cが以下の場合、それぞれSymbolが重複するかを答えなさい。 ( 1 ) A: アプリターゲット, B: 静的ライブラリ, C: 動的ライブラリ ( 2 ) A: アプリターゲット, B: 静的ライブラリ, C: 静的ライブラリ ( 3 ) A: アプリターゲット, B: 動的ライブラリ, C: 静的ライブラリ https://twitter.com/kateinoigakukun/status/1405844501680197636?s=20 ग़యɿ ղ౴ɿ A B C
  31. ͜Ε·Ͱ֬ೝͨ͜͠ͱ • Build • CompileͱLink • Build Target, Con fi

    guration, Schemeͷؔ܎ • Moduleͱimport • FrameworkͱLink • Static / Dynamic • XCFramework
  32. Swift Package Manager

  33. None
  34. Swift Package Manager • Pros • swiftʹಉࠝ͞Ε͍ͯΔʢಛʹCIͰؔΘͬͯ͘Δʣ • Xcodeʹ౷߹͞Ε͍ͯΔ •

    macOS / Linux྆ରԠ • ࣗಈͰґଘؔ܎ͷղܾɺߋ৽ • Cons • Compatible with Swift 5 and Xcode 11
  35. Swift Package Manager in Xcode

  36. None
  37. None
  38. None
  39. 8.1.1 Patch Minor Semantic Versioning Major Bug Fix Compatible Additions

    Breaking Changes https://semver.org
  40. 8.1.1 9.0.0 < Up to Next Major 8.1.1 8.2.0 <

    Up to Next Minor
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. Package.swiftͰ࢝ΊΔ ϓϩδΣΫτϑΝΠϧμΠΤοτ

  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. Package.swift • Products • Library: Build TargetͱLink͢Δ࣮ߦՄೳόΠφϦ (=Framework) • Dependencies

    • Targets • Target = Module • Sources/, Tests/ͷαϒσΟϨΫτϦ͕Ϟδϡʔϧ
  55. None
  56. AppFeature FeatureA Firebase FeatureB Link App Import Link Link Import

    AppFeature
  57. None
  58. None
  59. None
  60. None
  61. None
  62. None
  63. None
  64. None
  65. None
  66. Package.swift • 3rd partyͷϥΠϒϥϦΛ؅ཧ͢Δ͚ͩͰ͸ͳ͍ • Module΍FrameworkΛࣗࡏʹߏ੒Ͱ͖Δ → Multi Modules •

    ϓϩδΣΫτϑΝΠϧ͕ܰྔʹ → Multi Projects
  67. XCFramework in Package.swift

  68. XCFramework in Package.swift • ιʔείʔυͰ͸ͳ͘όΠφϦܗࣜͰ഑෍͍ͯ͠Δ • e.g.) ޿ࠂSDK, AnalyticsͳͲ •

    targetʹ.binaryTargetΛ࢖͏ • xcframework΋͘͠͸ͦΕΛؚΉzipϑΝΠϧ • ϩʔΧϧͰ΋ϦϞʔτͰ΋ՄʢϦϞʔτ͸checksum͕ඞཁʣ
  69. XCFramework in Package.swift

  70. Swift PMʹରԠ͍ͯ͠ͳ͍৔߹

  71. Swift PMʹରԠ͍ͯ͠ͳ͍৔߹ • Package.swift͕ͳ͍
 → ιʔείʔυ͕ެ։͞Ε͍ͯΕ͹Ϗϧυ͕ՄೳͳͷͰɺPull RequestΛग़͠ ͯPackage.swiftΛ௥Ճɻ΋͘͠͸Forkͯ͠௥Ճ • ιʔείʔυඇެ։ɺXCFramework΋ͳ͍ʢFramework͔͠ͳ͍ʣ


    → CocoaPodsΛซ༻͢Δͷ͕खͬऔΓૣ͍
 → Apple M1 (arm64) ͷ৔߹͸ Intel Mac (x86_64) ͰϏϧυ͞ΕͨFramework ͸ѻ͑ͳ͍ͷͰ޻෉͢Δʢޙ൒ʹ঺հʣ
  72. Resources in Swift PM

  73. Resources in Swift PM • Swift PMͰ͸Assets΍Localizable.string΋औΓѻ͑Δ • Localize͢Δ৔߹͸Package.swiftʹdefaultLocalizationΛ͚ͭΔ

  74. Resources in Swift PM • Bundle.moduleΛ࢖ͬͯΞΫηε͢Δ

  75. resource_bundle_accessor.swift

  76. Localization in Swift PM Localization for Xcode Preview

  77. Localization in Swift PM Localization for Xcode Preview

  78. Localization in Swift PM Localization for Xcode Preview

  79. Localization in Swift PM Localization for Build Target

  80. Resources in Swift PM Best Practice ✅Ϧιʔε͸Ϟδϡʔϧຖʹ؅ཧ͢Δ • Resource Bundle

    Accessor (Bundle.module) ͸ಉ͡Ϟδϡʔϧ಺ͷϦιʔεʹର ͯ͠༗ޮ ⚠ ΋͠ଞͷϞδϡʔϧʹఏڙ͢Δඞཁ͕͋Δ৔߹͸ݸʑͷϦιʔεʹରͯ͠Typed Public AccessorΛఏڙ͢Δ • Ϧιʔεͷ໊લ͕֎෦ͱͷґଘؔ܎ͱͳͬͯ͠·͏ͨΊਪ঑͞Εͳ͍ 🚫 Resource Bundle΁ͷΞΫηεΛAPIʹ͠ͳ͍ https://developer.apple.com/videos/play/wwdc2020/10169
  81. Resources in Swift PM Best Practice https://developer.apple.com/videos/play/wwdc2020/10169

  82. Resources in Swift PM Bundle͸Ͳ͜΁ʁ

  83. Swift PM with UIKit

  84. Swift PM with UIKit • Xib, Storyboard͕Ϧιʔεѻ͍ʹͳΔ • Xib, Storyboard಺ͰModuleΛࢦఆ͢Δ

    • ࢦఆ͠ͳ͍ͱΫϥογϡ͢Δ • xib΍Storyboard͔ΒViewΛੜ੒͢ΔςετΛॻ͘ • IBLinterΛ࢖͏
  85. Swift PM with UIKit Use IBLinter https://github.com/IBDecodable/IBLinter

  86. Multi module in Swift PM

  87. Multi module in Swift PM Pros • ίϯύΠϧ࣌ؒͷ୹ॖ • Xcode

    Previewͷ҆ఆԽ • App ClipͷΑ͏ͳ༰ྔ੍ݶ • Mini-Application
  88. Mini-Application • ಛఆͷػೳModuleΛબͼɺFrameworkΛߏ੒ • Build TargetʹFrameworkΛ௥Ճ͢Δ • ೝূը໘΍OnboardͳͲɺදࣔ৚͕݅͋Δ΋ͷʹಛʹ༗ޮ

  89. Multi module in Swift PM Best Practice • Common /

    Misc moduleΛ࡞Βͳ͍ • ࡞Γͨ͘ͳͬͨΒͰ͖Δ͚ͩ࠷খ୯Ґʹ෼ׂ͢Δ • ResourceͷॏෳΛ͋·ΓڪΕͳ͍ • େ͖ͳը૾Λ৭ΜͳϞδϡʔϧͰ࢖͍͍ͨ৔߹͸ͦΕࣗମΛϞδϡʔϧʹ͢ Δ
  90. Multi projects in Swift PM

  91. Multi projects in Swift PM • ϓϩδΣΫτͷ໾ׂ͕ബ͘ͳͬͨͷͰEnvironmentͷઃఆʹશৼΓͰ͖Δ • Staging.xcodeproj •

    Production.xcodeproj • (Staging-M1.xcodeproj)
  92. Multi projects in Swift PM Build Con fi guration vs

    Multi Project • Build Con fi gurationͰ؅ཧͰ͖Δͷ͸Build Settingsͷม਺
 Build SettingsͷUser-De fi nedʹΧελϜͳม਺ΛೖΕΔ • Info.plist͔ΒऔΓग़ͤΔΑ͏ʹ͢Δ
  93. Multi projects in Swift PM Build Con fi guration vs

    Multi Project • Xcode ProjectͰ؅ཧ͢ΔϑΝΠϧ͸͔͔ͨͩ਺छྨ • ࢀর͢ΔϑΝΠϧ͸ಉ͡Ͱ΋ҧͬͯ΋Α͍
 • Project͕ҧ͏ͱϑΝΠϧ໊͸ॏෳͯ͠΋େৎ෉ • e.g.) GoogleService-info.plist • Build Con fi guration΋ซ༻Ͱ͖Δ
  94. Multi Projects Case Study • ARM64༻Xcode ProjectΛผͰ࡞Δ • CocoaPodsΛ͜ͷϓϩδΣΫτ͚ͩೖΕͳ͍Α͏ʹ͢Δ •

    CocoaPodsʹೖ͍ͬͯΔϥΠϒϥϦ͸importͰ͖ͳ͘ͳΔͷͰ
 #if canImportͰճආ͢Δ
  95. ࠓ೔ͷ·ͱΊ • ݴ༿ͷఆٛΛઆ໌Ͱ͖ΔΑ͏ʹ • PackageͰιʔείʔυΛ؅ཧ͠ɺϓϩδΣΫτϑΝΠϧΛܰ͘͢Δ • ϚϧνϞδϡʔϧͳߏ੒ͰϏϧυ࣌ؒͷ୹ॖ΍Mini Application • ෳ਺ͷϓϩδΣΫτϑΝΠϧͷӡ༻Ͱ؀ڥઃఆ͕؆୯ʹ

  96. Case Study

  97. GANMA! / FLINTERS, Inc. • UIKit based • CocoaPods /

    Carthageͷซ༻ → CocoaPods / SwiftPMͷซ༻ • Ұ෦ͰXCFrameworkʹରԠ͍ͯ͠ͳ͍Pre built binary • Embedded FrameworkΛ΍ΊͨͷͰϓϩδΣΫτϑΝΠϧ͕খ͘͞ͳͬ ͨ • ΞϓϦαΠζ: 23.5MB → 20.6MB Case Study
  98. Timee / Timee, inc. • UIKit based • CocoaPods /

    Carthageͷซ༻ → CocoaPods / SwiftPMͷซ༻ • Ұ෦ͰXCFrameworkʹରԠ͍ͯ͠ͳ͍Pre built binary • ϓϩδΣΫτϑΝΠϧ͕খ͘͞ͳΓɺXcodeGenΛണ͕ͨ͠ • ηοτΞοϓ͕༰қʹ • ΞϓϦαΠζ: 30.3 MB → 26.9MB Case Study
  99. NOT A HOTEL, Inc. • SwiftUI • Multi modules (16

    → 32 and more !) • Preview Apps • Multi projects • Staging • Production • (Staging-M1 Archived) Case Study
  100. Case Study And you !!

  101. Resources • https://github.com/pointfreeco/isowords • https://www.pointfree.co/episodes/ep142-a-tour-of-isowords-part-1
 ʢpart 4·Ͱ͋Δʣ • https://date.notion.site/isowords-8f8982eb3a9a4665b2fa688b06791b70 •

    https://date.notion.site/Swift-PM-Build- Con fi guration-4f14ceac795a4338a5a44748adfeaa40
  102. ٻΉ! Feedback

  103. Swift Packageத৺ͷ ϓϩδΣΫτߏ੒ͱͦͷ࣮ફ iOSDC JAPAN 2021 Daiki Matsudate @d_date