APK slimming attributes-android:extractNativeLibs

First describe the conclusion:

android:extractNativeLibs = trueWhen gradle is packaged, it will be 对工程中的so库进行压缩finally generated apk包的体积会减小.
However, when the user installs the apk on the mobile phone, the system will decompress the compressed so library, which will cause the user 安装apk的时间变长.

Regarding the android:extractNativeLibsdefault value setting, if the developer has not made special configuration for android:extractNativeLibs:

  • minSdkVersion < 23 或 Android Gradle plugin < 3.6.0In case, when packing android:extractNativeLibs=true;
  • minSdkVersion >= 23 并且 Android Gradle plugin >= 3.6.0In case, when packing android:extractNativeLibs=false;

One, cause

Program zone https://www.yehe.org/

I accidentally found that using AndroidStudio to package the same Module as an aarAND apk, the disk space occupied by the two is huge:
打包为aar,占用磁盘空间4.4M;打包为apk,占用磁盘空间为11.7M;

Use AndroidStudio in apkanalyzer comparative analysis of the two libraries are packaged so Rwa File Sizebig gap, but both the Download Sizesize exactly the same:
打包为aar,so库Rwa File Size为3.4M;打包为apk,so库Rwa File Size为8.2M;
打包为aar,so库Download Size为3.3M;打包为apk,so库Download Size为3.3M;

The comparative analysis of the two packaging methods apkanalyzer is as follows:

Packaged as aar

Packaged as apk

Two kinds of packing ways Rwa File Sizewith Download Sizea big gap between that Raw File Sizeand Download Sizehow is it defined?

二、Raw File Size & Download Size

Official view_file_and_size_information: The description is as follows:

APK Analyzer shows raw file size and download file size values for each entity, as shown in figure 1. Raw File Size represents the unzipped size of the entity on disk while Download Size represents the estimated compressed size of the entity as it would be delivered by Google Play. The % of Total Download Size indicates the percentage of the APK's total download size the entity represents.

After translation:

APK Analyzer shows each entity Raw File Sizewith download file size:
Raw File Sizerepresentatives of the corresponding entity uncompressed sizes on disk;
Download Sizerepresentatives of the corresponding size of the entity in Google Play, estimated compression;
% of Total Download Sizerepresentatives of the corresponding module entity, share in the total size of the Download Size percentage.

View file and size information

Seeing this, doubt:

打包为aar时,AndroidStudio对Module中的so库进行了压缩;但打包为apk时,未对Module中的so库进行压缩

三、android:extractNativeLibs

Inquiring about relevant information, I found the article Android APK Raw File Size vs Download Size: The
article mentioned: When
packaging APK, 是否对so库进行压缩the control attribute is android:extractNativeLibs.

Android APK Raw File Size vs Download Size

AndroidManifest.xmlextractNativeLibsMode of use of properties:

<application
    android:extractNativeLibs="true">
</application>

3.1、android:extractNativeLibs = true

If android:extractNativeLibs = true, during apk packaging AndroidStudio会对Module中的so库进行压缩, the final apk size is smaller.

  • 好处是:When users download and upgrade in the application market, users have a stronger willingness to download and upgrade because they consume less traffic.
  • 缺点是:Because so is compressed and stored, when the user installs, the system will decompress the so and store a copy again. Therefore, the installation time will be longer, and the occupied user disk storage space will increase instead.

3.2、android:extractNativeLibs = false

If the android:extractNativeLibs = falseapk is packaged AndroidStudio不会对Module中的so库进行压缩, the final generated apk is larger in size.

  • 好处是:After the user installs, directly use /data/data/your.app.package/libthe so under the path, there is no additional so copy operation, relatively android:extractNativeLibs = truespeaking, save the user's disk storage space;

3.3. Conclusion

android:extractNativeLibs = true的设定还是利大于弊的。

设置为true可以工程中的so库进行压缩,最终减小生成的apk包大小。至于安装应用时,因so库解压缩而造成的安装时间增长,相对于带来的好处(提高应用市场用户的下载和升级意愿)而言,我认为是可接受的。

Four, android:extractNativeLibs default value

The official API of android:extractNativeLibs is described as follows:

android:extractNativeLibs official API description

You can learn from the official API description of android:extractNativeLibs:

  • The default value of android:extractNativeLibs in the source code is true;
  • Compiler Android Gradle plugin 3.6.0 or higher, the default value of android:extractNativeLibs is false;

但真的是这样吗?一起来探究一下。

4.1, extractNativeLibs default settings in the source code

Starting from Android 6.0 (API 23), PackageParser.javawhen reading android:extractNativeLibsattribute values in the Android frame source code , the default value is true;

Corresponding source code path:frameworks/base/core/java/android/content/pm/PackageParser.java

if (sa.getBoolean(
        com.android.internal.R.styleable.AndroidManifestApplication_extractNativeLibs,
        true)) {
    ai.flags |= ApplicationInfo.FLAG_EXTRACT_NATIVE_LIBS;
}

4.2, Android Gradle plugin 3.6.0 or higher

The compiler Android Gradle plugin 3.6.0 或更高版本will default to it when building the application extractNativeLibs 设置为 false.

By observing the AndroidManifest.xmlfiles generated after compilation , it is found that the default value of the gradle plugin is set to false, which is achieved when processing the AndroidManifest.xml file 在其中自动插入 android:extractNativeLibs=“false"来实现的.
Because android:extractNativeLibsthis property is in Android 6.0 (API 23) introduced, so if the project configuration minSdkVersion < 23, then, gradle plug-in is not automatically inserted android:extractNativeLibs=“false".

4.3. Conclusion

When the developer is packaging the apk, if there is no special configuration for android:extractNativeLibs:

  • If minSdkVersion < 23 或 Android Gradle plugin < 3.6.0, when packing android:extractNativeLibs=true;
  • If minSdkVersion >= 23 并且 Android Gradle plugin >= 3.6.0, when packing android:extractNativeLibs=false;

Five, reference

apkanalyzer:
https://developer.android.com/studio/command-line/apkanalyzer

view_file_and_size_information:
https://developer.android.com/studio/build/apk-analyzer.html#view_file_and_size_information

Android APK Raw File Size vs Download Size:
https://stackoverflow.com/questions/45024723/android-apk-raw-file-size-vs-download-size-how-to-shrink-the-raw-file-size

android:extractNativeLibs:
https://developer.android.com/guide/topics/manifest/application-element.html#extractNativeLibs

Setting android:extractNativeLibs to reduce app size:
https://stackoverflow.com/questions/42998083/setting-androidextractnativelibs-false-to-reduce-app-size

========== THE END ==========

Welcome to follow my official account

Guess you like

Origin blog.csdn.net/weixin_48967543/article/details/115269878