【51CTO.com Quick Translation】I wonder if you have noticed that the ever-increasing business needs of users have prompted developers to continuously integrate more content into the same application. As a result, this has directly led to the increasing size of applications. Some large applications will even become "big users" of smartphone storage resources, which will cause slow operation and seriously affect the work efficiency of users. According to Google's survey statistics: if the size of an application exceeds 150 MB, its chance of being installed will decrease by 30%. Moreover, for every 6 MB increase in size, the conversion rate of the application being installed may decrease by 1%. Currently, many technical experts are working on creating small applications and tools. Android App Bundle is one of these publishing tools. Through it, users do not need to re-download the content that is already in the native application from the app store. The platform claims to be able to reduce the size of the application by 35%. Next, let's explore what methods can be used to reduce the size of the Android application itself during the application development life cycle. 1. Image Optimization We can easily reduce the size of an image without reducing the resolution. For example, we can convert .jpg and .png images to .webp (Web Image Format). This format has both lossy compression for .jpg and transparency for .png, so many tools support this type of conversion. For example, tools such as guetzli and packjpg are best suited for compressing .jpg files, while tools such as pngcrush and zopflipng are suitable for converting .png files. Based on this, designers can use vector graphics to create simple images that save space and do not lose resolution. They can be used as VectorDrawable objects in Android, and can enable files to generate clear images that fit the screen size. The corresponding Java code is as follows:
As we all know, some images are not related to static resources, and the program framework can dynamically draw such images at runtime. For example, when developing small applications, Vector Drawables objects can only occupy the minimum amount of storage space in the APK file and create images that meet the design specifications. Of course, complex objects may occupy a certain share of CPU and RAM, thereby reducing the overall running speed. In addition, as a WYSIWYG image editor tool, Draw 9-patch can also save space by reducing the size of the image. In actual use, you can use this tool to create bitmaps that can fit various mobile devices and different screen sizes. Moreover, the selected part of the image can also be scaled vertically or horizontally according to the gesture indicators you draw in the image. Furthermore, we can also use the aapt tool to process .png images, which are available in the device directory res/drawable/, to achieve lossless compression. For example: we can convert true color .png images (no more than 256 colors) into a palette with 8-bit images enabled. Obviously, the latter greatly reduces the size of the image while retaining the display effect. However, it is worth noting that the aapt tool does not compress .png files in the asset/folder. Moreover, it cannot optimize images that use more than 256 colors. In addition, its disadvantage is that it may cause the compressed .png files to rebound. To do this, we need to avoid this shortcoming by using the cruncherEnabled flag in Gradle (Translator's Note: An open source tool for project automation builds based on the concepts of Apache Ant and Apache Maven) (see the following code):
2. Eliminate Redundant Code In short, redundant code is unnecessary code that can be obtained by recalculation or does not contribute to the application's functionality during execution. The methods or classes corresponding to these codes are not only useless to the application, but may also cause program bloat and waste system resources to generate unnecessary loading and execution. 3. Eliminate Dead Code The size of the APK file is directly proportional to the loading speed of the application, the memory used, and the power consumption. It can be seen that any temporarily useless code retained only for the purpose of possible future use will only take up more space and make the application consume more device power. Therefore, please do not hesitate to find them directly and delete them decisively. This move is not only to improve the quality of the source code, but also to reduce the amount of code maintenance, so that the application can avoid errors or attacks. 4. Limit the use of library resources Usually, when developing Android applications, we use various external libraries, and we often need to maximize the availability and versatility of these libraries. Among them, the most commonly used are Google Play Services (which can be used to restore the automatic translation of application texts) and Android Support Library (which can be used to improve the user experience on old devices). Of course, these libraries are also designed for servers or desktops. They often come with various methods and objects that cannot provide services for mobile applications. For this, you need to keep only the parts required by the application when writing the program. To do this, you not only need to modify different call permissions, but also use only those libraries suitable for mobile devices to implement certain specific functions. 5. Only supports specific screen densities Unlike the iPhone, which has a basically fixed screen size, the Android platform devices produced by different manufacturers around the world are very diverse. They have products with different screen sizes and resolutions. In addition, in terms of Android screen density, there are also types such as ldpi, tvdpi, mdpi, hdpi, xhdpi, xxhdpi and xxxhdpi. Therefore, if you want your application to be small and beautiful, then there is no need to support all screen types. You only need to ensure that your interface can be displayed clearly and smoothly on basic devices. Of course, in practice, you can also investigate and find out the market share of devices with a specific screen density. If the share is really low, there is really no need to adapt or even bundle your app with each density. The following command can be used to automatically expand the Android system to adapt to screens of different densities.
6. Avoid Enumerations Typically, enumeration operations will add 1.0 to 1.4 KB of space to an application's classes.dex file. Moreover, if there are too many enumerations in the program, especially when there are various shared libraries or complex systems, they will slow down the program at run time and take up more space. We can use ProGuard and the @IntDef annotation function to convert the enumeration type to an integer to reduce the size of the application while retaining all the advantages of enumeration. 7. Use APK Size Analyzer APK Analyzer can provide you with various data about the size of your APK. At the top of its interface, you can choose to view the original file size, that is, the amount of disk space the APK occupies. Its "Download Size" indicates how much data traffic a user will consume to download the target application, taking into account the compression of the Play Store account. APK Size Analyzer can be downloaded efficiently through the plugin marketplace of Android Studio. In its analysis report, it will display the size of folders and files in descending order. With this feature, you can easily find the "big occupants" of space. At the same time, you can also use the "Compare" function to view the size difference between the new and old versions of an app. 8. Downloadable fonts Since most apps in the Play Store use the same fonts, most fonts are included in App Bundles. When a user runs different apps on a device that use the same fonts, they need to be able to reuse each other locally. Fortunately, Google has considered this situation and provides downloadable fonts in its support library. Therefore, when you develop an app, you no longer need to use the API to bundle font files, you only need to request the corresponding fonts. 9. Provide multiple APKs Sometimes, users may only want to use a part of an app's features. As a whole, an app often cannot be split from an existing APK. To this end, you can upload your app on Google Play through Android App Bundles so that Google can enable APK optimization based on the device's configuration. In actual development, you can also divide one APK into multiple ones so that users can choose the most practical APK based on their own judgment and the actual device configuration. summary In a highly competitive market, in addition to providing high-quality functional services, the download size of our application and the space it occupies on the user's device are also one of the key factors that determine its success or failure. By reducing the size of the application, we will be able to continuously improve the user experience. How To Reduce Android App Size During Development? [Translated by 51CTO. Please indicate the original translator and source as 51CTO.com when reprinting on partner sites] |
<<: What is it like to update to iOS 14?
>>: How much of your storage capacity has WeChat and Alipay taken up? Exaggerated
As the weather gets colder, down jackets have bee...
Do you remember? In the 2021 Chinese Poetry Compe...
Why are 5,300 Airbus employees facing unpaid leav...
He Zong The news that Baidu was going to make dri...
This article takes product status, user growth an...
SystemServer SystemServer is an important process...
The past few years have been exciting (or scary, ...
Expert of this article: Li Zongou, Master of Opht...
Lin Dan cheated on his wife, and many people are ...
[[143398]] MoKee OpenSource 51.1 2015-08-01 Offic...
Editor's note: This article is written by sen...
Today, Qingguajun will share with you the analysi...
Produced by: Science Popularization China Author:...
Of course, we can definitely say that this is an ...
Tuchong Creative First, let's look at the def...