iT邦幫忙

2024 iThome 鐵人賽

0
Mobile Development

Flutter 開發實戰 - 30 天逃離新手村系列 第 36

Shorebird - Flutter 版 Code Push

  • 分享至 

  • xImage
  •  

為了更加全面的評估分析實務上使用的需求,這裡我們進一步介紹 Flutter 的 Code Push。

首先關於「Code Push」 這個術語的起源最初我是因為 React Native 認識的。2015 年左右 Microsoft 為 React Native 所創造,功能是可以動態更新應用程式。整體概念其實類似於 Over the air update (OTA) 。一開始 Code Push 的核心功能是可以繞過應用商店的審核過程,快速推送更新,但使用這些技術的時候還是需要注意遵守商店的政策。

回到「Shorebird」範圍,這裡 Code Push 我們指的是一種工具,可一讓我們快速的更新 Flutter 應用程式,而不用通過完整的審核流程。沒錯!和 React Native 中的 Code Push 概念是類似的。

當我們使用 Shorebird ,會包含一個修改過的 Flutter 引擎,該引擎在啟動時會檢查 Dart 程式碼是否有更新。若有可用的更新,引擎會下載,然後下次重啟應用時會套用更新。

在 Flutter 一般 Code Push 的工作流程概略如下:

  • 使用 Shorebird 指令建立新版本應用程式。
  • 通過 App Store 和 Google Play 發佈新版本。
  • 更改應用程式的 Dart 程式碼。可能是錯誤修復、新功能或其他任何內容,也可以包括更新的 Dart 相依函式庫。
  • 使用 Shorebird 指令建立 patch 補丁到第一步建立的版本應用程式
  • 下一次使用者重新開啟應用程式的時候會自動更新

Patching

Patching 補丁是更新應用程式程式碼而不用從 App Store 或 Google Play 下載新版本的一個過程。通過建立 Patch 我們可以即時更新程式碼。

那麼什麼類型的變更可以使用 Patch? 基本上可以包含任何 Dart 程式碼變更例如應用程式的 Dart 程式碼、產生的程式碼,pubspec.yaml 但是不能包含原生程式碼的變更或設定。

而不能包含的東西例如:資源檔案如圖片,字體,上面提到的原生程式碼 Kotlin、Swift 等。Flutter 引擎的變更例如不能更新 Flutter 的版本。

術語

Application 應用程式

一個應用程式即 flutter create [app_name] 建立的專案並且對應 App Store 或 Google Play 的項目。每一個應用程式應有一個唯一的 app_id 您可以在執行 shorebird init 時設定,通常可以在 shorebird.yaml 中找到。

Release 版本

Release 發佈版本通常指的是應用程式的特定版本,通過版本號嗎和建置號碼例如 1.0.0+1 。雖然 Code Push 可以在 App Store 和 Google Play 之外派送應用程式,但是其發佈版本通常應該對應到發佈到 App Store 和 Google Play 的特定版本。

一個發佈版本可以有多個 Patch 。一個發佈版本通過 shorebird release [platform] 建立,[platform] 可以是 android aarios

Patch 補丁

補丁是對特定發佈版本的修改,通過 OTA 的方式更新。補丁可以是錯誤修正貨新功能。對於特定發佈版本可以有多個補丁,但是一次只能有一個補丁可以被生效。補丁通過關聯的發佈版本和補丁號碼識別,補丁號碼是一個自動遞增的整數。

當應用程式啟動的時候,它會檢查可用的補丁並套用最新的一個。下一次啟用時生效。通過 shorebird patch [platform] 建立補丁。

Artifact

Artifact 是編譯建置或建立補丁操作的輸出結果例如:

  • shorebird release android 會產生特定的 libapp.so.aab 檔案,這些稱為 Release Artifact。
  • shorebird patch android 會產生和上傳差異檔案,這些檔案擷取了補丁和對應版本程式碼的差異稱為 Patch Artifact。

快速入門

首先,我們需要到 Shorebird Console 註冊,你可以使用 Google 或微軟帳號登入。

接著介面會提供你安裝指令

# macOS
$ curl --proto '=https' --tlsv1.2 https://raw.githubusercontent.com/shorebirdtech/install/main/install.sh -sSf | bash

# windwos
$ Set-ExecutionPolicy RemoteSigned -scope CurrentUser # Needed to execute remote scripts
iwr -UseBasicParsing 'https://raw.githubusercontent.com/shorebirdtech/install/main/install.ps1'|iex

根據你的作業系統安裝對應的 Shorebird CLI ,預設以 macOS 來說會安裝到 ~/.shorebird/bin 並且加入您的 PATH 。同時也會安裝 Flutter 和 Dart 副本到 ~/.shorebird/bin/cache/flutter ,這個副本小小的改造了,主要是加入 Shorebird Code Push ,但我們開發時我們依舊是使用我們自己安裝的版本。

如果發生任何錯誤可以使用下面指令進行排查

$ shorebird doctor

如果安裝沒有任何問題我們可以使用指令進行登入

$ shorebird login

登入後我們就可以初始化設定,先到我們的專案下執行

$ shorebird init

這個指令會建立一個 shorebird.yaml 檔案,其中會包含 Shorebird 的 app_id

然後我們就可以根據需要的平台編譯 release

$ shorebird release android
✓ Downloading patch... (0.4s)
✓ Extracting patch... (0.8s)
✓ Downloading bundletool.jar... (1.9s)
✓ Extracting bundletool.jar... (1.4s)
✓ Downloading aot-tools.dill... (0.2s)
✓ Extracting aot-tools.dill... (2.1s)
✓ Fetching apps (0.3s)
✓ Building app bundle with Flutter 3.24.2 (9239d8659d) (79.3s)
✓ Release version: 1.0.0+1 (1.2s)
✓ Fetching releases (0.3s)

🚀 Ready to create a new release!

📱 App: demo (4dec4252-761c-4ea6-ac24-c2096df98287)
📦 Release Version: 1.0.0+1
🕹️  Platform: android
🐦 Flutter Version: 3.24.2 (9239d8659d)

Would you like to continue? (y/N) Yes
✓ Fetching releases (4.1s)
✓ Creating release (0.6s)
✓ Updating release status (0.3s)
✓ Creating artifacts (9.5s)
✓ Updating release status (2.0s)

✅ Published Release 1.0.0+1!
Your next step is to upload the app bundle to the Play Store:
/demo/build/app/outputs/bundle/release/app-release.aab

For information on uploading to the Play Store, see:
https://support.google.com/googleplay/android-developer/answer/9859152?hl=en

然後這裡舉例我們就可以先開啟 Android Emulator 或者連接實體裝置進行測試

$ shorebird preview
✓ Fetching releases (0.4s)
✓ Fetching releases (0.3s)
Which release would you like to preview?
Which release would you like to preview? 1.0.0+1
✓ Fetching aab artifact (0.3s)
✓ Downloading release (0.3s)
✓ Using production track (0.4s)
✓ Extracting metadata (0.6s)
✓ Building apks (3.0s)
✓ Installing apks (2.0s)
✓ Starting app (1.4s)

整個流程的 DX 實在是非常優秀和方便。接著,就是我們關注的重點如何建立 Patch,假如我們先修改一點程式碼。

$ shorebird patch android
✓ Fetching apps (0.3s)
✓ Fetching releases (0.3s)
Which release would you like to patch?
Which release would you like to patch? 1.0.0+1
✓ Fetching aab artifact (0.3s)
✓ Downloading aab (2.3s)
✓ Building patch with Flutter 3.24.2 (9239d8659x) (27.6s)
✓ Verifying patch can be applied to release (21ms)
✓ Fetching release artifacts (0.8s)
✓ Downloading release artifacts (4.3s)
✓ Creating patch artifacts (5.5s)

🚀 Ready to publish a new patch!

📱 App: demo (4dec4252-761c-4ea6)
📦 Release Version: 1.0.0+1
🕹️  Platform: android [arm32 (161.81 KB), arm64 (158.07 KB), x86_64 (153.48 KB)]
🟢 Track: Production

Would you like to continue? (y/N) Yes
✓ Creating patch (1.8s)
✓ Uploading artifacts (3.3s)
✓ Fetching channels (0.3s)
✓ Promoting patch to stable (0.3s)

✅ Published Patch 1!

下次重新開啟應用程式就會套用我們的 Patch。對於正式環境,我們需要將初始版本的 AAB 上傳到 Google Play。之後,當我們使用 shorebird patch android 建立新的 Patch 時,這個 Patch 會自動推送給使用者。建立 Patch 後,Shorebird 會將其發布到他們的伺服器。安裝了你的應用程式的設備會定期檢查是否有可用的更新。如果有新的 Patch 可用,設備會在背景下載。

其他資訊:

Visual Studio App Center 平台(包括其 CodePush 服務)計劃在 2025 年 3 月 31 日停止服務。雖然 Microsoft 意識到到 CodePush 對許多開發者的重要性,準備了一個特殊的 CodePush 版本。但是我們需要主動聯繫 App Center 支援團隊(support@appcenter.ms)來取得獨立版本,並進一步自己架設服務來運行這個獨立版本,我們需要自己管理 CodePush 更新。


上一篇
了解 Composited Layer 進行應用程式效能優化
系列文
Flutter 開發實戰 - 30 天逃離新手村36
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言