简体   繁体   中英

how to reduce android app size that is using pdf files in assets folder

I am making a book app with pdf files in the assets folder. All files are 10-20mb after compressing. How can I keep the size of the app small? It's 500+mb currently.

Android studio provides a handful tool: APK Analyser APK Analyser will tear down your application and let you know which component in your .apk file is taking the larger amount of space? Let's have a look in Anti-Theft screen lock apk file without any optimization applied.

APK 分析器示例 1

From the apk analyser output, you can see that the application raw size is about 3.1MB. After applying play store compressions, the application size is roughly 2.5 MB. As you can see from the screenshot, there are main 3 folders that are consuming most of the application size.

  • classes.dex — This is the dex file which contains all the byte code files of your java code that will run on your DVM or ART.
  • res — This folder includes all the files under your res folder. Most of the time this will contain all the images, icons and raw files, menu files, and layouts.
  • resources.arsc — This file holds all the value resources. This file contains all the data you have under your different value folders. This resource contains strings, dimensions, styles, integers, ids etc.

APK 分析器示例 2

So, now you know what an APK is made of. Let's see, how can we decrease the application size by optimising one by one component.

Reduce classes.dex:

As we discussed, this contains all the java code. While you build your application, gradle will combine all of your .class files from all the modules and convert those .class files to .dex files and combine them in single classes.dex file. Single classes.dex file can accommodate, approximately 64K methods. If you exceed this limit, you have to enable multidexing in your project. Which will create another classes1.dex file to include all remaining methods. Thus the number of classes.dex file depends on the number of methods you have.

APK 分析器示例 3

As you can see currently “Anti-Theft Screen Lock” contains 4392 classes and 29897 methods. This result is without applying proguard. You have two default proguard file you can apply.

- proguard-android-optimize.txt

- proguard-android.txt

As the name suggests “proguard-android-optimize.txt” is the more aggressive progurard configuration. We used this as the default proguard configuration. You can add you custom proguard configurations in proguard-rules.pro file in your /app directory .

Compression Gist 1

By setting minifyEnabled to true (In the Compression Gist 1), you are telling proguard to remove all the unused methods, instructions and slim down the classes.dex file.

Here is the result from minify enabled APK,

APK 分析器示例 4

As you can see by enabling the proguard in every module of our project we can we are able to reduce the classes.dex file size almost by 50%. Also, you can see method count decreased from 29897 to 15168 (almost 50%).

Size decreased from 3.1 MB to 1.98MB. (~50% decrease)

Reduce res:

The second largest component in your apk size is your res folder, which contains all the images, raw files, and XML. You cannot add/remove/modify your XML, as they are containing your layouts. But we can decrease the image files.

  • “shrinkResources” attribute will remove all the resources, those are not used anywhere in the project. Enable this in your build.gradle file by adding below line:

Compression Gist 2

  • “resConfigs” attribute will remove all the other localized resources while building the application. In our case, “Anti-Theft Screen Lock” only supports the English language. While all the support libraries may have localized folders for other languages. Which we don't need. So, add following line to allow only English resources to be added in APK file.

Compression Gist 3

  • Android Studio 2.3, and you application minimum version is 18 or above, you should use webp instead of png. webp images are having less size than png files and also it retains the original image quality. More to that webp images are natively supported in Android. So you can load webp images in ImageView same as other raster images. So, you don't have to change your layouts.

You can select drawable and mipmap folders from you project, right click and select convert to webp . This will open below configuration dialog.

转换为 webp Press ok and it will convert all the png images to webp format one-by-one. If the webp image is having the larger size than png, Android Studio will automatically skip that file.

Let's see the final result:

APK 分析器示例 5

  • The res folder size decrease from 710KB to 597KB.

By applying the above simple tricks the application size decreases from 3.19 MB to 1.89 MB.

Happy Coding 😊

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM