One, foreword

This is the sixth article in the Android SDK development art exploration series. It briefly introduces the SDK development about the SDK package volume optimization knowledge. It includes the composition analysis of APK and AAR, the concept of SDK package size and integration increment, and the ideas of resource compression and optimization in SDK development. Package volume control is a very complicated system engineering, there are many schemes can be practiced, the key is the input-output ratio. This article does not attempt to list all of the optimizations in the web article, but rather shows simple and feasible configurations to compress and optimize the SDK. I hope this is a thinking inspiration, awakening a heart of continuous optimization, as well as the characteristics of compression and optimization practices in SDK development. Understand SDK components and explore SDK development best practices in package volume compression and control integration increment.

Series of articles:

Android SDK Development Art Exploration (1) Introduction and Design

(2) Exception or ErrorCode

Android SDK development art exploration (iii) initialization

Android SDK development art exploration (4) personalized configuration

Android SDK development art exploration (5) security and verification

Android SDK development art exploration (6) compression and optimization

Android SDK development art exploration (vii) Dependency principles and packaging methods

2. Composition analysis of APK and AAR

2.1 APK file composition

An APK file consists of a Zip file that contains all the files that make up an application. Includes Java class files, resource files, and files containing compiled resources.

APK contains the following directories:

  • META-INF/: containsCERT.SFCERT.RSASignature file, as wellMANIFEST.MFManifest files.
  • assets/: Contains application resources. The application can be usedAssetManagerObject retrieves these resources.
  • res/: contains not compiled toresources.arscResources in.
  • lib/: contains compiled code specific to the processor’s software layer. This directory contains subdirectories for each platform type, such asarmeabi,armeabi-v7a,arm64-v8a,x86,x86_64mips.

APK also contains the following files. Of these files, only androidmanifest.xml is required.

  • resources.arsc: contains compiled resources. This file containsres/values/XML content in all configurations of the folder. The packaging tool extracts this XML content, compiles it to binary form, and compresses the content. This content includes language strings and styles, and is not directly contained inresources.arscThe path to the contents of a file, such as layout files and images.
  • classes.dex: contains classes compiled in a DEX file format that the Dalvik/ART virtual machine understands.
  • AndroidManifest.xml: contains the core Android manifest file. This file lists the application name, version, access rights, and referenced library files. This file uses Android’s binary XML format.

2.2 Composition of AAR file

AAR file has a.aar file extension and is itself a zip file that must contain the following files/directories:

  • /AndroidManifest.xml
  • /classes.jar
  • /res/
  • /R.txt

In addition, an AAR file may contain one or more of the following optional files/directories:

  • /assets/
  • /libs/name.jar
  • /jni/abi_name/name.soAbi_name is one of the ABI supported by Android.
  • /proguard.txt
  • /lint.jar
  • /api.jar
  • /public.txt(The official document defines it as mandatory, but the actual measurement can not include it)

SDK package size and integration increment

First, let’s distinguish between these two concepts:

**SDK package size: ** refers to the size and volume of SDK packages such as AAR files;

**SDK integration increment: ** refers to the increment after APK integrates with AAR, that is, the APK volume difference before and after integration.

Here it is clear that the APK volume after integrating an AAR **< the APK volume before integrating an AAR +** THE AAR volume. For example, if an AAR is 30M long, the increment of APP integration with that AAR is generally not more than 30M. The specific increment depends on the actual resources or dependency allocation of the main project. Alternatively, some AArs support only one ABI, changing the configuration to fit the integration, and ultimately reducing the size of the entire APK.

Therefore, to popularize this concept, it is useful to note the SDK package size and estimated integration increments in the SDK documentation or product notes. For APK size-sensitive users, take some precautions.

Iv. Ideas of resource compression and optimization in SDK development

As mentioned in the foreword, this article is not intended to list all the compression optimization solutions mentioned in the web article, but rather focuses on the simple, feasible and effective optimization ideas in SDK development scenarios, as well as the potential pitfalls of configuring for SDK.

4.1. General compilation and configuration

The general approach to resource optimization and compression is to enable compression, obfuscation, and optimization. ShrinkResources cannot be turned on in the Library module, unlike the App module. Shrinker Cannot be Used for Libraries.

android {
        buildTypes {
            // Note that the following configuration takes effect only for the release version of the project. Note that the version difference may be caused by configuration difference during debugging
            release {
                // Enable code compression, obfuscation, and optimization.
                minifyEnabled true
                // Enable resource reduction. Note that this option is not enabled in the Library module.
                shrinkResources true
                // The ProGuard rule file is loaded by default. You can add custom files here, or configure excluded rules in proGuard-rules.pro
                proguardFiles getDefaultProguardFile(
                        'proguard-android-optimize.txt'),
                        'proguard-rules.pro'}}... }Copy the code

4.2 resource compression and native class library configuration

4.2.1 Resource file compression

Resource files refer to multimedia resources such as images, audio and video, which can be compressed and optimized to a certain extent. And, if your project hasn’t been optimized like this, a simple fix will do the trick.

For image, audio, video compression, replacement there are a lot of mature solutions on the network, I won’t mention them here.

4.2.2. Simplify the native class library

Configure reserved ABI directories under jniLibs as required, for example, armeabi-v7a and arm64-V8a

defaultConfig {
        ...
        ndk {
            abiFilters 'armeabi-v7a'.'arm64-v8a' // Specify the supported ABIs}... }Copy the code

In SDK development and integration, this configuration is important if your code includes the SO library. Since different components may have different jniLibs directories, if left unchecked, the App will crash by packing incomplete directories.

Here’s an example:

Only in the original App project support armeabi – v7a (due to delete the directory of clean enough, gradle configuration is fine), then if new integrates a contains armeabi – v7a arm64 – v8a two ABI AAR, but not limited to support in the configuration of gradle ABIs, Then there will be two SO files in the typed APK, but only the SO files in the Armeabi-V7A folder are complete. When the APK is loaded by an ARM64-v8A-enabled phone, it will retrieve SO in the arm64-V8A directory by default. The App crashes because it cannot find the SO module associated with the original App.

Five, the conclusion

This paper briefly introduces the knowledge of SDK file composition, volume control and optimization, as well as the compression optimization configuration that should be paid attention to during SDK development and integration, hoping to arouse developers’ perceptual cognition and optimization awareness of SDK package volume and integration increment, and provide some ideas for exploring how to develop an excellent SDK. Project optimization comes from external requirements, but also from the developer’s own awareness and awareness.

Finally, if this document is helpful or inspiring to your development, like/follow/share three lines is the best incentive for the author to continue to create, thanks for your support!

Refer to the article

  • Create an Android library
  • Reduce, obfuscate, and optimize your application

Copyright Notice:

This article first appeared in my column AndDev Android development has been authorized to hongyang public account exclusive release.