简体   繁体   中英

Changing minSdkVersion from 16 to 26 increased release APK size from 17 to 39 MB

I have a relatively small and simple application that has always generated a ~17 MB release APK file. Some time ago I noticed that the APK size increased to a whopping (for this app) 39 MB. I tracked down the change that caused it and it turns out that the same codebase, where the only change is the minSdkVersion from 16 to 26 without any other changes results in the APK increase.

Strange thing is, when I unpack the APKs, the unpacked directories take up ~40 MB on disk. There are some changes, but all of them are in very small files, like some layouts are missing in the 26 version, negligible. The biggest part of the 40 MB is the lib folder which contains *.so libraries, which amounts to 37 MB, but they are identical in both APK versions. (The application is a Flutter application build with flutter build apk --release .)

I don't actually want nor need the minSdkVersion to be 26 and will revert this change, but I am very curious about:

  1. What changed along the way in Android to cause such a dramatic size increase between these two minSdkVersion s?
  2. How can the APKs, when unpacked, be of pretty much the same size? Seems like maybe the compression when build for minSdkVersion 16 was much better?
  3. If the unpacked directories are of pretty much the same size, does the size increase actually matter for the end users? Will they need to download 17 vs 37 MB? Will the app build for minSdkVersion 26 take up more space on their devices?

This is working as intended as per the docs on android:extractNativeLibs :

Whether or not the package installer extracts native libraries from the APK to the filesystem. If set to "false" , then your native libraries must be page aligned and stored uncompressed in the APK. Although your APK might be larger, your application should load faster because the libraries are directly loaded from the APK at runtime. On the other hand, if set to "true" , native libraries in the APK can be compressed. During installation, the installer decompresses the libraries, and the linker loads the decompressed libraries at runtime; in this case, the APK would be smaller, but installation time might be slightly longer.

The default value is "true" if extractNativeLibs is not configured in AndroidManifest.xml . However, when building your app using Android Gradle plugin 3.6.0 or higher, this property is reset to "false" if it is NOT configured in AndroidManifest.xml and minSdkVersion >= 23 .

So you're gaining runtime performance by not extracting native libraries, making for a better user experience. Since the system does not need to decompress the .so file, you're actually saving space on the user's device as well.

As explained in the Reduce your app size :

When building the release version of your app, package uncompressed .so files in the APK by setting android:extractNativeLibs="false" in the <application> element of your app's manifest. Disabling this flag prevents PackageManager from copying .so files from the APK to the filesystem during installation and has the added benefit of making updates of your app smaller.

So while the APK size is, at first, larger, subsequent updates are actually significantly smaller. This is because the Google Play Store automatically only downloads the difference between APKs when doing an app upgrade - by storing the .so file uncompressed, this diff is significantly smaller as additional compression would normally completely change the .so file, rather than only change the parts that actually changed.

After upgrading compile and target sdk version, apk size got increased because native JNI libs are uncompressed by default in latest versions .So, Android official documentation recommends to add it in app level build.gradle

 android {
    packagingOptions {
        jniLibs {
            useLegacyPackaging true // Enabling flag to compress JNI Libs to reduce APK size Ref: https://developer.android.com/studio/releases/gradle-plugin#compress-native-libs-dsl
        }
    }
}

After adding above code snippet, apk size got reduced as earlier versions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM