Understanding Android APK size
The Mapbox Maps SDK for Android is a complex and powerful framework, but it only contributes 3–4 megabytes to the ultimate size of your application. This guide suggests a few strategies for reducing the final Android app APK size.
Shrink code and resources
Forgotten code, menus, images, strings, and more can pile up, but your final APK might not use them all.
ProGuard and DexGuard, even without obfuscation, will remove unused Java code from your code and its dependencies. Don't forget to check for and set up necessary ProGuard rules if you're using other third-party libraries!
Adding shrinkResources
to your app-level Gradle file will remove unused resource files that live in the /res
folder. Adding minifyEnabled
runs ProGuard to shrink code by deleting unused code and other references. Set both to true
. Both are quick wins in making sure that your compiled project only includes what it truly needs.
android {
...
buildTypes {
release {
minifyEnabled true
shrinkResources true
...
}
}
}
Drop unnecessary architectures
The Mapbox Maps SDK ships with three architectures:
./arm64-v8a/libmapbox-maps.so
./x86/libmapbox-maps.so
./x86_64/libmapbox-maps.so
Each of these files add up to the resulting APK. If your app doesn't need x86 support, for example, you could drop x86
and x86_64
to save some space. See Leverage ABI splits below for details.
Exclude unnecessary modules
The Maps SDK is a multi-module Gradle project with Android Libraries and Android applications. Each of these modules add up to the resulting APK.
If a module is prefixed with the name plugin
it indicates it's an optional module that will be loaded automatically at runtime if it's part of the compilation classpath. Plugins can be excluded with a Gradle exclusion to disable a plugin fully and to save binary size. By default the SDK includes the majority of plugins found in mapbox-maps-android
.
For example, if you do not plan to use animations in your application, you could remove the animation plugin:
implementation ('com.mapbox.maps:android:11.7.1'){
exclude group: 'com.mapbox.plugin', module: 'maps-animation'
}
Leverage ABI splits
This is a feature that lets you build an APK file for each CPU, only containing the relevant native libraries. This process is described in the Android Studio Project Site.
If you distribute your app via Google Play, you can use this approach through the Multiple APK Support distribution feature.
Leveraging APK splits is one of the tips we give users when it comes to shrinking their APK size. Splitting results in building different APK for the different supported ABIs in an application. Google Play is optimized to only download the APK for the ABI of the device that is installing the app.
Find more information about multiple APKs in the Android Studio documentation.