Skip to main content

flutter_hotpatch 0.1.1 copy "flutter_hotpatch: ^0.1.1" to clipboard
flutter_hotpatch: ^0.1.1 copied to clipboard

PlatformAndroid

Signed over-the-air Dart code patches for Flutter apps on Android, with staged rollouts and automatic rollback of patches that fail to boot.

flutter_hotpatch #

Signed over-the-air Dart code patches for Flutter apps on Android.

Fix a Dart bug in a shipped app without a Play Store release. A patch is the app's recompiled Dart code (libapp.so). It is signed with your private key, downloaded by the app, checked, and loaded at the next cold start. If a patch fails to boot, the device rolls back by itself.

iOS is not supported: iOS does not allow loading downloaded compiled code.

Status: early (0.1). The full flow (patch, automatic rollback, remote rollback) is tested end to end on Android, but test each patch on a device before rolling it out widely. You host the patches yourself (S3, or any static HTTPS host); there is no hosted service.

How it works #

  1. At startup. Before Application.onCreate, a small content provider picks the newest patch that is safe to run. It passes that patch to Flutter's loader with --aot-shared-library-name, a stock Flutter option that accepts a .so file inside the app's private files folder. No fork of Flutter is involved.
  2. Checking for updates. Hotpatch.checkForUpdate() downloads a signed manifest for the app's exact version code. It checks the Ed25519 signature and the sequence number, downloads the compressed libapp.so for the device's CPU type, verifies both SHA-256 hashes, and stages it for the next launch.
  3. Confirming a boot. A patch counts as working once the patched Dart code reports in and either the first frame appears or 10 seconds pass.
    • A new patch that never gets there is dropped after one failed launch. A patch that worked before is dropped after two. Either way the device returns to its last good patch, or to the store build.
    • If Flutter silently fell back to the bundled code, the patch is also marked bad.
  4. Store updates. When a new version arrives from the Play Store, all old patches are deleted.

Limits #

  • Patches change Dart code only. They cannot change plugins, native Kotlin or Java code, assets, or the Flutter version. hotpatch patch refuses to publish if any of these differ from the store build.
  • A patch applies on the next cold start, not live.
  • Each patch is the full compressed libapp.so, a few MB per CPU type. Diff patches are not done yet.
  • Store policy: Google Play bans apps from downloading executable code such as .so files, except "code that runs in a virtual machine or an interpreter". Patches are Dart code running in the Dart VM; Shorebird relies on the same reading, and Google has not confirmed it. Decide for yourself whether that risk is acceptable for your app.

Setup in an app #

1. Depend on the package:

dependencies:
  flutter_hotpatch: ^0.1.1

2. Create a signing key on the machine that publishes patches. Keep it out of the repository:

dart run flutter_hotpatch:hotpatch keygen --out ~/.hotpatch/keys/myapp.key

3. Check for patches once per launch:

import 'package:flutter_hotpatch/flutter_hotpatch.dart';

const hotpatchConfig = HotpatchConfig(
  baseUrl: 'https://dxxxxxxxx.cloudfront.net',
  appId: 'com.example.myapp',
  publicKeys: {'0591ba63ee956dbd': 'QEOw4aJu...='},   // from keygen
);

// For example in the first screen's initState, after the first frame:
Hotpatch.checkForUpdate(hotpatchConfig);

4. Add hotpatch.yaml in the app's root folder:

app_id: com.example.myapp
store: s3://my-bucket/hotpatch     # or a local folder for testing
key_file: ~/.hotpatch/keys/myapp.key
flutter: flutter                   # path to the exact Flutter used for store builds
build_args:                        # must match the store build exactly
  - --dart-define-from-file=env/prod.json

Uploads to s3:// go through the AWS CLI. Serve the bucket over HTTPS, for example through CloudFront. Manifests are uploaded with max-age=60; patch files are immutable.

Releasing and patching #

# After building the store version, register it (commit first):
flutter build appbundle --release --dart-define-from-file=env/prod.json
dart run flutter_hotpatch:hotpatch release --artifact build/app/outputs/bundle/release/app-release.aab --version-code 42

# Later: fix Dart code, commit, then publish a patch for that store version:
dart run flutter_hotpatch:hotpatch patch --version-code 42 --rollout 10 --notes "fix checkout total"
dart run flutter_hotpatch:hotpatch promote --version-code 42 --rollout 100
dart run flutter_hotpatch:hotpatch status --version-code 42

# Undo:
dart run flutter_hotpatch:hotpatch rollback --version-code 42 --to 3     # an earlier patch
dart run flutter_hotpatch:hotpatch rollback --version-code 42 --base     # the store build

Before publishing, hotpatch patch checks that:

  • the working tree is committed;
  • Flutter, the engine and Dart match the store build exactly;
  • plugins, the app's Android code, native libraries and Flutter assets are unchanged.

It then builds the app with --dart-define=HOTPATCH_PATCH_NUMBER=<n>, so the running code can prove which patch it is.

Security #

  • Devices only run manifests signed by a key listed in publicKeys. The private key stays on the publishing machine, so the hosting does not need to be trusted.
  • Every download is checked by size and SHA-256, before and after decompression. Patch files are stored read-only in the app's private folder.
  • A manifest with a lower sequence number than one already seen is rejected. This stops an old, buggy patch from being replayed.
  • publicKeys can list several keys, so keys can be rotated.

Apps that start without UI #

The Android side confirms boots from the first frame or from Dart after 10 seconds. If the app is usually started by a background service, call Hotpatch.confirmBoot() once that service is running.

Development #

flutter test                                              # Dart unit tests
cd example/android && ./gradlew :hotpatch:testReleaseUnitTest   # Kotlin unit tests
FLUTTER=/path/to/flutter example/tool/e2e.sh              # end-to-end on a device

The end-to-end script checks four things on a real emulator or device:

  • a store build runs with no patch;
  • a patch changes the Dart code;
  • a patch that crashes on start rolls back on its own;
  • a remote rollback returns devices to the store build.
1
likes
160
points
0
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Signed over-the-air Dart code patches for Flutter apps on Android, with staged rollouts and automatic rollback of patches that fail to boot.

Repository (GitHub)
View/report issues

Topics

#code-push #ota #updates #android

License

MIT (license)

Dependencies

args, crypto, cryptography, flutter, path, yaml

More

Packages that depend on flutter_hotpatch

Packages that implement flutter_hotpatch