Repository navigation
fix(android): ship R8 consumer rules for digital ink recognition - #927
danielmessi13 wants to merge 1 commit into
Conversation
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>
|
The "Build Android APK" failure is unrelated to this change: Maven Central returned Could a maintainer please re-run the failed job? Thanks! Generated by Claude Code |
🤖 AI disclosureThis 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:
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 |
There was a problem hiding this comment.
do we need m=similar rules for the other packages?
There was a problem hiding this comment.
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.
📌 Overview
With AGP 9 (R8 full mode on by default), apps that depend on
google_mlkit_digital_ink_recognitioncrash 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:com.google.mlkit:digital-ink-recognition:19.0.0bringsandroidx.work:work-runtime:2.7.0. R8 strips the no-arg constructor ofWorkDatabase_Impl, which WorkManager creates by reflection, so the app dies inandroidx.startup.InitializationProviderwithFailed to create an instance of androidx.work.impl.WorkDatabase, before Flutter starts.Randomsubclass.com.google.android.gms.internal.mlkit_vision_digital_ink.zzaimextendsjava.util.Randomand only allowssetSeedwhile an internal flag is set. R8 optimizes the flag away, so thesetSeedcall fromRandom.<init>throwsUnsupportedOperationException: Setting the seed on the shared Random object is not permittedas soon asRemoteModelManageris 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'sRandomsubclass (and the rest of the SDK's internals) intact.android/build.gradle:consumerProguardFiles("consumer-rules.pro")indefaultConfig.implementation("androidx.work:work-runtime:2.10.5"): replaces the 2.7.0 brought bydigital-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):work-runtimeoverride, withgoogle_mlkit_digital_ink_recognitionoverridden to this branch viadependency_overrides(path):build/app/outputs/mapping/release/configuration.txtandwork-runtime-2.10.5is the resolved WorkManager;crashlogcat buffer is empty;MddModelManager: download: complete, status 1) and ink recognition works.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. TheRandomissue probably belongs in the ML Kit SDK itself; these rules protect Flutter users in the meantime.CHANGELOG/ version bumps in this PR, to be done at release time.Fixes #926
🤖 Generated with Claude Code