How one dependency made my Android app 15 MB, and the change that brought it to 5.8 MB
I ported a business app from iPhone to Android in Kotlin and Compose. The code is small: screens, a few network calls, a map and a barcode scanner. The first signed app bundle was 15.4 MB. By the evening it was 5.8 MB with the same features. Almost all of the difference was one dependency line.
Find out what is inside first
An app bundle is a zip file. Before changing anything, list it by size:
unzip -l app-release.aab | sort -n -r | head -20
The top of my list was not my code and not my images. It was the barcode model and a native library, present four times: once for each processor type the bundle supports.
ML Kit comes in two forms
Google's ML Kit libraries for barcodes and text exist in two variants, and the names are easy to miss:
- Bundled: com.google.mlkit:barcode-scanning. The model and the native code are inside your app. It works from the first second, on any device.
- Unbundled: com.google.android.gms:play-services-mlkit-barcode-scanning. Your app holds only a thin client. Google Play services downloads the model once and shares it between all apps on the phone.
Most tutorials use the first one. For the unbundled variant you change the dependency and add one line to the manifest, so the model is fetched when the app is installed:
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode" />
I also ask for the module when the scanner screen opens, through the ModuleInstall API of Play services, in case the download at install time has not happened yet.
The numbers
For the business app:
- Bundled barcode scanning, four processor types: 15.4 MB
- Unbundled: 5.79 MB, of which about 3.2 MB is the R8 mapping file that Play keeps for crash reports
- The release APK a phone actually installs: 2.7 MB
A second app of mine reads book pages with text recognition and counts exercise repetitions with pose detection. There the steps were larger:
- Everything bundled, four processor types: 50.8 MB
- Text recognition unbundled, two processor types (arm64 and 32-bit ARM): 20.5 MiB
- arm64 only: 15.9 MiB
- arm64 only, with the mapping file moved out of the bundle: 12.6 MiB
Where it stops
Pose detection has no unbundled variant. Its models are 4.8 MiB and its arm64 native library is 5.5 MiB, so that feature alone is 10.3 MiB. Without it the same app would be about 2.5 MiB. If you need a bundle under 10 MB and a camera feature of that kind, the arithmetic says no, and it is better to know that on the first day.
What you give up
The unbundled variant is not free:
- The first scan may have to wait for the download. Show a clear "getting ready" state; do not show an error.
- Phones without Google Play services never get the model. Decide whether those devices matter for your app.
- An emulator image without the Play Store may never download the module. My emulator showed "waiting for the module" for good, while the bundled build had scanned on it without trouble. Test the unbundled build on a real phone.
- Play services can send diagnostic data for ML Kit, so read what your Data safety form says after the switch.
The order I would do it in
- List the bundle by size.
- Remove processor types you do not need. x86 is for emulators and a few old tablets.
- Switch every ML Kit feature that has an unbundled variant.
- Turn on R8 and resource shrinking for the release build, if they are off.
- Only then look at images and fonts.