[英]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.我正在使用资产文件夹中的 pdf 文件制作书籍应用程序。 All files are 10-20mb after compressing.压缩后所有文件都是10-20mb。 How can I keep the size of the app small?我怎样才能保持应用程序的大小很小? It's 500+mb currently.目前是 500+mb。
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? Android Studio 提供了一些工具: APK Analyzer APK Analyzer 将拆除您的应用程序并让您知道 .apk 文件中的哪个组件占用了更大的空间? Let's have a look in Anti-Theft screen lock apk file without any optimization applied.让我们看看没有应用任何优化的防盗锁屏 apk 文件。
From the apk analyser output, you can see that the application raw size is about 3.1MB.从 apk 分析器的输出中,您可以看到应用程序的原始大小约为 3.1MB。 After applying play store compressions, the application size is roughly 2.5 MB.应用 Play 商店压缩后,应用程序大小约为 2.5 MB。 As you can see from the screenshot, there are main 3 folders that are consuming most of the application size.从屏幕截图中可以看出,有 3 个主要文件夹占用了大部分应用程序大小。
So, now you know what an APK is made of.所以,现在您知道 APK 是由什么组成的。 Let's see, how can we decrease the application size by optimising one by one component.让我们看看,我们如何通过一个一个的组件优化来减小应用程序的大小。
As we discussed, this contains all the java code.正如我们所讨论的,这包含所有的 java 代码。 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.在您构建应用程序时,gradle 会将您所有模块中的所有 .class 文件合并,并将这些 .class 文件转换为 .dex 文件,并将它们合并到单个 classes.dex 文件中。 Single classes.dex file can accommodate, approximately 64K methods.单个classes.dex文件可以容纳大约 64K 个方法。 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.这将创建另一个 classes1.dex 文件以包含所有剩余的方法。 Thus the number of classes.dex file depends on the number of methods you have.因此 classes.dex 文件的数量取决于您拥有的方法数量。
As you can see currently “Anti-Theft Screen Lock” contains 4392 classes and 29897 methods.如您所见,目前“防盗锁屏”包含 4392 个类和 29897 个方法。 This result is without applying proguard.这个结果没有应用 proguard。 You have two default proguard file you can apply.您有两个可以应用的默认 proguard 文件。
- proguard-android-optimize.txt - proguard-android-optimize.txt
- proguard-android.txt - proguard-android.txt
As the name suggests “proguard-android-optimize.txt” is the more aggressive progurard configuration.顾名思义, “proguard-android-optimize.txt”是更具侵略性的 progurard 配置。 We used this as the default proguard configuration.我们使用它作为默认的 proguard 配置。 You can add you custom proguard configurations in proguard-rules.pro file in your /app directory .您可以在/app 目录中的proguard-rules.pro文件中添加自定义 proguard 配置。
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.通过将minifyEnabled设置为 true(在压缩要点 1 中),您告诉 proguard 删除所有未使用的方法、指令并缩小classes.dex文件。
Here is the result from minify enabled APK,这是启用了缩小功能的 APK 的结果,
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%.正如您所看到的,通过在我们项目的每个模块中启用 proguard,我们可以将 classes.dex 文件大小减少近 50%。 Also, you can see method count decreased from 29897 to 15168 (almost 50%).此外,您可以看到方法计数从 29897 减少到 15168(几乎 50%)。
Size decreased from 3.1 MB to 1.98MB.大小从 3.1 MB 减少到 1.98 MB。 (~50% decrease) (减少约 50%)
The second largest component in your apk size is your res folder, which contains all the images, raw files, and XML. apk 大小中的第二大组件是 res 文件夹,其中包含所有图像、原始文件和 XML。 You cannot add/remove/modify your XML, as they are containing your layouts.您不能添加/删除/修改您的 XML,因为它们包含您的布局。 But we can decrease the image files.但是我们可以减少图像文件。
You can select drawable and mipmap folders from you project, right click and select convert to webp .您可以从项目中选择 drawable 和 mipmap 文件夹,右键单击并选择convert to webp 。 This will open below configuration dialog.这将在配置对话框下方打开。
Press ok and it will convert all the png images to webp format one-by-one.按确定,它会将所有 png 图像一一转换为 webp 格式。 If the webp image is having the larger size than png, Android Studio will automatically skip that file.如果 webp 图像的尺寸大于 png,Android Studio 将自动跳过该文件。
Let's see the final result:让我们看看最终的结果:
By applying the above simple tricks the application size decreases from 3.19 MB to 1.89 MB.通过应用上述简单技巧,应用程序大小从 3.19 MB 减少到 1.89 MB。
Happy Coding 😊快乐编码😊
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.