Skip to content

fix(android): ship R8 consumer rules for digital ink recognition - #927

Open
danielmessi13 wants to merge 1 commit into
flutter-ml:developfrom
danielmessi13:fix/digital-ink-r8-release-crash
Open

danielmessi13 wants to merge 1 commit into
flutter-ml:developfrom
danielmessi13:fix/digital-ink-r8-release-crash

Conversation

@danielmessi13

@danielmessi13 danielmessi13 commented Nov 25, 2026 •

Copy link
Copy Markdown

📌 Overview

With AGP 9 (R8 full mode on by default), apps that depend on google_mlkit_digital_ink_recognition crash on launch in release builds (#926). Debug builds are fine because R8 does not run there. There are two separate crashes; the second only appears once the first is fixed:

  1. WorkManager database. com.google.mlkit:digital-ink-recognition:19.0.0 brings androidx.work:work-runtime:2.7.0. R8 strips the no-arg constructor of WorkDatabase_Impl, which WorkManager creates by reflection, so the app dies in androidx.startup.InitializationProvider with Failed to create an instance of androidx.work.impl.WorkDatabase, before Flutter starts.
  2. ML Kit internal Random subclass. com.google.android.gms.internal.mlkit_vision_digital_ink.zzaim extends java.util.Random and only allows setSeed while an internal flag is set. R8 optimizes the flag away, so the setSeed call from Random.<init> throws UnsupportedOperationException: Setting the seed on the shared Random object is not permitted as soon as RemoteModelManager is initialized.

This PR ships consumer R8 rules with the plugin and bumps WorkManager, so host apps no longer need their own keep rules.


🛠 Key Changes

Android (google_mlkit_digital_ink_recognition)

  • 🟢 android/consumer-rules.pro: keep rules applied automatically to host apps:
    • -keep class * extends androidx.room.RoomDatabase { <init>(); } and -keep class androidx.work.impl.WorkDatabase_Impl { <init>(); }: the generated Room / WorkManager databases are created by reflection.
    • -keep class com.google.android.gms.internal.mlkit_vision_digital_ink.** { *; } and -keep class com.google.mlkit.** { *; }: keeps the flag field of ML Kit's Random subclass (and the rest of the SDK's internals) intact.
  • 🟡 android/build.gradle:
    • consumerProguardFiles("consumer-rules.pro") in defaultConfig.
    • implementation("androidx.work:work-runtime:2.10.5"): replaces the 2.7.0 brought by digital-ink-recognition:19.0.0, which crashes under R8 full mode.

🧪 Verification & Testing

Tested with Flutter 3.47.4 (stable), AGP 9.3.1, on an Android emulator (flutter build apk --release):

  • Without this change and without app-side keep rules, the release build crashes on launch (crash 1 above).
  • Same app, no app-side keep rules and no app-side work-runtime override, with google_mlkit_digital_ink_recognition overridden to this branch via dependency_overrides (path):
    • the plugin's rules show up in build/app/outputs/mapping/release/configuration.txt and work-runtime-2.10.5 is the resolved WorkManager;
    • after 30 s the app process is alive and the crash logcat buffer is empty;
    • the model download completes (MddModelManager: download: complete, status 1) and ink recognition works.

⚠️ Notes & Risk Assessment

  • Breaking API Changes: None. Dart API and platform channel untouched.
  • Native Dependencies (Android): adds androidx.work:work-runtime:2.10.5 (Gradle picks it over the transitive 2.7.0). The -keep com.google.mlkit.** rule keeps ML Kit classes from being shrunk, which slightly increases APK size in exchange for a working release build. The Random issue probably belongs in the ML Kit SDK itself; these rules protect Flutter users in the meantime.
  • No CHANGELOG / version bumps in this PR, to be done at release time.

Fixes #926

🤖 Generated with Claude Code

Release builds with R8 full mode (default since AGP 9) crash on launch:
WorkManager 2.7.0 (from digital-ink-recognition 19.0.0) loses the
WorkDatabase_Impl constructor, and ML Kit's internal Random subclass
throws from setSeed once R8 removes its flag field.

Add consumer-rules.pro with the needed keep rules and depend on
androidx.work:work-runtime 2.10.5.

Fixes flutter-ml#926

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danielmessi13

Copy link
Copy Markdown
Author

The "Build Android APK" failure is unrelated to this change: Maven Central returned 429 Too Many Requests while Gradle was resolving dev.flutter.flutter-plugin-loader in packages/example/android/settings.gradle.kts, before any plugin module was evaluated. The same job passes on develop, and lint/unit tests pass here.

Could a maintainer please re-run the failed job? Thanks!


Generated by Claude Code

@danielmessi13

Copy link
Copy Markdown
Author

🤖 AI disclosure

This change was prepared with the help of Claude Code (an AI coding agent), including the commit and the PR description. I reviewed the diff and I'm accountable for it.

It was tested on a real app that hit this crash in production (Google Play internal testing), not only on the example:

  • Flutter 3.47.4, AGP 9.3.1 (R8 full mode), release build
  • Without any app-side ProGuard rules or WorkManager override, only a dependency_overrides path to this branch
  • Android emulator: app launches and stays alive, crash buffer empty, the digital ink model downloads (MddModelManager: download: complete) and handwriting is recognized

I'm aware the repo's AGENTS.md asks agents not to commit or push. I opened this anyway because the fix is small and was verified end to end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need m=similar rules for the other packages?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I'm honestly not 100% sure, since I only use the digital ink recognition package in my app, so that's the only one I could reproduce and test.

That said, I looked at the native dependencies of the other plugins for the two root causes (androidx.work:work-runtime:2.7.0 and the internal java.util.Random subclass that throws "Setting the seed on the shared Random object is not permitted" under R8). From that, it looks like google_mlkit_entity_extraction would need the same rules too: entity-extraction:16.0.0-beta6 also pulls in work-runtime:2.7.0 and ships the same Random subclass in com.google.android.gms.internal.mlkit_entity_extraction. I didn't find either of them in the other packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants