Android Package Size Optimization Suggestions - Resources

These are some suggestions for reducing the size of the package given by the official Android documentation. I happened to be reading this content recently, so I will record and share it. This article is mainly for Android resource (Resource) related optimization suggestions.

1 Remove useless resources

Use the lint static code analysis tool to find unused resresources.

Note assertthat libthe unused resources under and cannot be detected, and the assert/resource reference uses the reflection method. In addition to detection tools, Gradle also supports automatic removal of useless resources when packaging and compiling. Use shrinkResources:

android {
    
    
    // Other settings
    buildTypes {
    
    
        release {
    
    
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Here minifyEnabledneeds to be opened together, that is, code compression. When compiling in this way, R8the useless code will be removed first, and then the gradle plugin will be used to remove useless resources. Detailed usage reference: Shrink, obfuscate, and optimize your app

In addition, you can also use resConfigconfiguration to flavorpackage specified resources for specified . For example, the images in the outbound package and the first-party package have different resources and are configured in different folders. When packaging, you can only package the resources in the specified folder, which can also reduce the volume to a certain extent.

2 Reduce the size of dependent libraries

When developing, when selecting a third-party tool library, you need to pay attention to the size of the library. For example, the selection of Android image loading libraries Glide , Picasso , and Fresco will depend on the size of the library as one of the selection criteria.

In addition, if you only use one of the (small) functions of a certain tool library, can you consider downloading it and modifying it yourself, and properly remove unnecessary function codes, thereby reducing the size of the dependent library.

3 Native animated image support

Use ImageDecoderthe native API to support the image file format of GIF with animation and WebP, so as to delete the third-party library and reduce the package size.

Requires Android 11 or above to support

4 Support specific resolutions

ldpiStarting from Android 4.4, , mdpi, tvdpi, hdpi, xhdpi, xxhdpiand xxxhdpiso many resolutions (screen density, density) are supported , but it is not necessary to create corresponding image resources for each resolution.

Maybe only 0.1% of your users need to ldpi...

At this time, in fact, we only need a folder with one resolution, for example drawable-nodpi/, for others, the system will automatically adapt (zoom in and zoom out)

At least one resolution is officially recommendedxxhdpi

5 Use drawable or code to render pictures

Some monochrome background images do not need static image files, and Drawableobjects (in xml) can be drawn at runtime, thereby saving the size of static image files.

Correspondingly, use code to generate some simple renderings (render from code), such as setting the page background to a solid color.

Compared with using image files, the package size can be reduced to a certain extent.

6 Multiplexing resources

In some cases, the two pictures may be the same except for the angle or color. At this time, we do not recommend using two pictures. You can use code to color or rotate them.

For the pure coloring (changing the color) of the picture, the system greater than Android 5.0 (API 21) can use android:tintand tintMode, and the version less than Android 5.0 can use ColorFilterto complete.

To change the rotation angle of the picture, such as changing the up arrow to the down arrow, use the following:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_thumb_up"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="180" />

7 Compress image resource files

The official built-in tool aapt , when compiling, will automatically perform lossless compression (lossless compression) on the pictures in res/drawable/(other directories are invalid, for example ), if not needed, you can manually close it:asset/

buildTypes.all {
    
     isCrunchPngs = false }

It should be noted that isCrunchPngs is turned on by default in release (this will increase the compilation time), and it is turned off by default in debug.

Use third-party tools to compress images in different formats:

8 Using the WebP file format

Android 3.2 (API 13) began to support the WebP image format.

Compared with JPEG and PNG, WebP has better compression efficiency (that is, the compressed volume is larger and the loss is smaller). You can directly convert BMP, JPG, PNG or static GIF images to WebP format in Android Studio.

JPEG is lossy compression and PNG is lossless data compression, which is mainly reflected in the change of transparency. (lossless data compression, a raster-graphics file format)

9 Using vector graphics

Vector graphics are resolution-independent pictures, and a 100-byte file can describe a high-definition picture that fills the entire screen.

Vector graphics are represented by in the code VectorDrawable.

But there are certain performance issues. Vector graphics are essentially a series of xml data. The Android system calls cpu\gpu to draw a picture according to the "description" of vector graphics: for example, the upper part of the x coordinate is filled with red, the lower part of the y coordinate is filled with green, and where and where is filled with gray …

It is suitable for small-area icon (icon) drawing, not suitable for large-area image drawing, and has a certain performance consumption.

example:

<!-- res/drawable/battery_charging.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="24dp"
    android:width="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">

   <group
         android:name="rotationGroup"
         android:pivotX="10.0"
         android:pivotY="10.0"
         android:rotation="15.0" >

      <path
        android:name="vect"
        android:fillColor="#FF000000"
        android:pathData="M15.67,4H14V2h-4v2H8.33C7.6,4 7,4.6 7,5.33V9h4.93L13,7v2h4V5.33C17,4.6 16.4,4 15.67,4z"
        android:fillAlpha=".3"/>

      <path
        android:name="draw"
        android:fillColor="#FF000000"
        android:pathData="M13,12.5h2L11,20v-5.5H9L11.93,9H7v11.67C7,21.4 7.6,22 8.33,22h7.33c0.74,0 1.34,-0.6 1.34,-1.33V9h-4v3.5z"/>
   </group>
</vector>

There are also animation effects that can also be achieved using vector graphics (compared to using frame-by-frame animation, it is necessary to put a large number of single-frame pictures to combine an animation, which greatly increases the size of the apk package!)

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, but you must be able to select models, expand, and improve programming thinking. In addition, a good career plan is also very important, and the habit of learning is very important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master the various knowledge points of Android development.
img
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.

Welcome everyone to support with one click and three links. If you need the information in the article, you can directly scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓

PS: There is also a ChatGPT robot in the group, which can answer your work or technical questions
picture

Guess you like

Origin blog.csdn.net/YoungOne2333/article/details/131987363