An overview of the

I usually in project development, initially APK package may not be too big, but with the change of time, the iteration of the project, slowly returned to find out the APK package is more and more big, the bigger package each may desire of users to download is smaller, so the usual project iteration, the APK thin body work to also want to continue, This article will document several ways to slim down APK

Preliminary study on composition of APK

We generate a package and drag the package into Android Studio to see the package composition

The composition of APK is shown in the figure above. Now let’s see what each part is

  • Lib mainly houses the SO library for each CPU architecture
  • Classes. dex is a bytecode file generated by compilation of Java files
  • Res stores resource files (images, layouts, etc.)
  • Meta-inf Specifies the related signature information
  • Androidmanifest.xml configuration file
  • Resources. arsc is a mapping table of resource files. The resource ID and path information of specific resources are stored in it

We have a preliminary understanding of the composition of APK, and then we can optimize the weight loss of APK

Optimization scheme

1. Confuse the code with minifyEnabled

android {
buildTypes {
    release {
        minifyEnabled true
    }
}
}
Copy the code

Write obfuscation rules in ProGuard-rules.pro

2. Remove unwanted resources with shrinkResources

android {
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
    }
}
}
Copy the code

ShrinkResources and minifyEnabled must be enabled at the same time

Special note: it is important to note that useless resources or images are not removed, but a placeholder of the same name is used. Let’s use images as examples

(figure 2)

As shown in the above two pictures, best.png image is not used, its size is 3.3K, which belongs to useless resource. After opening shrinkResources, WE find that Best.png is indeed packed into APK, but the size has changed to 67B, and the specific content is a placeholder for the small black block

3. Delete unused resource files from the project

We through the Android studio provide lint tool to screen not to use a resource file, and then to use resource files deleted through Analyze – Run Inspection By Name… Option, and then enter unused Resources to find unused resource files

Useless resource files are found and can be deleted as shown above

4. Remove unused code from the project

We use the Android Studio provided lint tool to check for unused code, and then delete unnecessary code By using the analysis-run Inspection By Name… Option, and then type unused declaration to find useless code

Useless code can be found and removed as shown above

Special note: Methods or classes referenced by reflection are not recognized and will be checked out, so be careful when deleting them

5. Compress the image

Images can be compressed in several ways

  • Converting some large images to JPG format will greatly reduce the size of the image
  • Images are compressed through the Tinypng website
  • Use vector diagrams
  • To convert the image To webP format (right-click image -Conver To webP To convert the image) note in particular: webP format requires the minimum API 18 after converting the image will be much smaller, as shown below)

6. Use Shape instead of image

Many click effects or background images may use images. Shape can be replaced to effectively reduce the size of APK

7. Use resConfigs to configure the language

Android applications themselves support internationalization, but many domestic projects are used in China, so there is no need for internationalization. In this case, the relevant String can be subtracted to make it contain only a specific language (such as Chinese).

android { defaultConfig { ... ResConfigs "zh-rcn"}}Copy the code

As shown in the figure above, with the resConfigs configuration, the only language packaged into APK is Chinese

8. Obfuscate resources with AndResGuard

The AndResGuard tool of Zhang Shaowen, the great god of wechat, was used to confuse the resource files of the project, which increased the difficulty of anti-cracking while reducing the size of APK

The specific source code and usage method is as follows [github.com/shwenzhang/…]

9. So library operation

When we introduced third-party SDKS, many SDKS had corresponding SO libraries, and the so libraries given were divided into many architectures (Armeabi, Armeabi-V7A, x86, etc.).

Android currently supports seven different CPU architectures: ARMv5, ARMv7 (since 2010), x86 (since 2011), MIPS (since 2012), ARMv8, MIPS64 and X86_64 (since 2014)

All architecture devices x86, x8664, Armeabi-V7A, arm64-V8A support armeabi architecture. So files, so we can choose to use Armeabi-v7A or Armeabi-V7A support according to our business needs

For example, wechat, QQ, netease Cloud Music and many other big apps only retain Armeabi-V7A

android {
defaultConfig {
    ndk {
        abiFilters 'armeabi','armeabi-v7a'
    }
}
}
Copy the code

After optimization, there are only ‘armeabi’ and ‘Armeabi-v7a’ in the apK lib package

conclusion

Project slimming is a long time work, and only after understanding the composition of APK can you better slim APK, through the above steps you will have a different APK change