Repository navigation
Commit 4504953
committed
fix(android): restore compile-time selective install for text recognition and barcode scanning
The v2 hardening PR fixed a real compile failure by making
com.google.mlkit:text-recognition and com.google.mlkit:barcode-scanning
unconditional dependencies, defeating the library's selective-install
purpose: a consumer who only wants barcode scanning still got the
full text-recognition SDK bundled, and vice versa.
Root cause: Kotlin has no #if-equivalent preprocessor. Six Kotlin files
(TextRecognitionServiceFactory/MLKitTextRecognitionService/MLKitTextAdapter
and their barcode equivalents) import concrete MLKit types at file
scope, so they fail to compile whenever the corresponding artifact is
missing from the classpath - regardless of whether the runtime feature
flag ever lets that code execute. iOS never had this problem since its
Swift equivalents use #if MLKIT_TEXT_RECOGNITION_ANY / #if
MLKIT_BARCODE_SCANNING, which genuinely elides the guarded code (and
its imports) at compile time.
Fixed by giving Kotlin the same guarantee via Gradle conditional
source sets instead of reflection (MLKit's real client APIs return
async Task<T> objects with listener callbacks, which would need a
java.lang.reflect.Proxy to invoke reflectively - fragile under R8 and
adds reflection overhead to code that runs every camera frame):
- Moved the 6 MLKit-coupled files into textRecognitionEnabled/
barcodeScanningEnabled source sets (content unchanged, path only).
- Added tiny stub factories in textRecognitionDisabled/
barcodeScanningDisabled that throw if ever reached (they aren't -
HybridVisionCameraMLKit already gates construction before this
point), existing purely so the always-compiled Hybrid* classes have
something to compile against.
- android/build.gradle picks the enabled/disabled source dir per flag,
and re-gates the two base MLKit SDKs behind their flags again.
- Added com.google.mlkit:vision-common as an explicit, always-on
dependency: three files (ProcessedImage, ImagePreprocessor,
HybridFrameSpec+toProcessedImage) use its InputImage type
unconditionally for the shared image-preprocessing pipeline. This
isn't a version bump - it's already resolved transitively today at
the same 17.3.0.
- Updated the README caveat that documented this exact limitation,
since it's no longer true.
Verified locally (not just "it compiles"): with barcodeScanning:false,
com.google.mlkit:barcode-scanning is fully absent from the resolved
debugRuntimeClasspath; with textRecognition and all 4 language
variants false, com.google.mlkit:text-recognition is fully absent.
Full yarn test suite passes (93/93).1 parent 050c005 commit 4504953
10 files changed
Lines changed: 49 additions & 12 deletions
File tree
- android
- src
- barcodeScanningDisabled/java/com/visioncameramlkit/infrastructure/mlkit/factories
- barcodeScanningEnabled/java/com/visioncameramlkit/infrastructure/mlkit
- adapters
- factories
- textRecognitionDisabled/java/com/visioncameramlkit/infrastructure/mlkit/factories
- textRecognitionEnabled/java/com/visioncameramlkit/infrastructure/mlkit
- adapters
- factories
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
170 | 172 | | |
171 | 173 | | |
172 | 174 | | |
| |||
188 | 190 | | |
189 | 191 | | |
190 | 192 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
195 | 200 | | |
196 | 201 | | |
197 | 202 | | |
198 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
199 | 206 | | |
200 | 207 | | |
201 | 208 | | |
| |||
250 | 257 | | |
251 | 258 | | |
252 | 259 | | |
253 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
254 | 263 | | |
255 | 264 | | |
256 | 265 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
0 commit comments