简体   繁体   English

如何减少在assets文件夹中使用pdf文件的android应用程序大小

[英]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 文件。

APK 分析器示例 1

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 个主要文件夹占用了大部分应用程序大小。

  • 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. classes.dex — 这是 dex 文件,其中包含将在 DVM 或 ART 上运行的 Java 代码的所有字节码文件。
  • res — This folder includes all the files under your res folder. res — 此文件夹包含 res 文件夹下的所有文件。 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. resources.arsc — 该文件包含所有价值资源。 This file contains all the data you have under your different value folders.此文件包含您在不同值文件夹下的所有数据。 This resource contains strings, dimensions, styles, integers, ids etc.此资源包含字符串、维度、样式、整数、ID 等。

APK 分析器示例 2

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.让我们看看,我们如何通过一个一个的组件优化来减小应用程序的大小。

Reduce classes.dex:减少classes.dex:

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 文件的数量取决于您拥有的方法数量。

APK 分析器示例 3

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 配置。

Compression Gist 1 压缩要点 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.通过将minifyEnabled设置为 true(在压缩要点 1 中),您告诉 proguard 删除所有未使用的方法、指令并缩小classes.dex文件。

Here is the result from minify enabled APK,这是启用了缩小功能的 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%.正如您所看到的,通过在我们项目的每个模块中启用 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%)

Reduce res:降低资源:

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.但是我们可以减少图像文件。

  • “shrinkResources” attribute will remove all the resources, those are not used anywhere in the project. “shrinkResources”属性将删除所有资源,这些资源在项目中的任何地方都没有使用。 Enable this in your build.gradle file by adding below line:通过添加以下行在您的 build.gradle 文件中启用此功能:

Compression Gist 2 压缩要点 2

  • “resConfigs” attribute will remove all the other localized resources while building the application. “resConfigs”属性将在构建应用程序时删除所有其他本地化资源。 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.因此,添加以下行以仅允许在 APK 文件中添加英文资源。

Compression Gist 3 压缩要点 3

  • Android Studio 2.3, and you application minimum version is 18 or above, you should use webp instead of png. Android Studio 2.3,且您的应用程序最低版本为 18 或更高版本,您应该使用webp而不是 png。 webp images are having less size than png files and also it retains the original image quality. webp 图像的大小小于 png 文件,并且它保留了原始图像质量。 More to that webp images are natively supported in Android.更重要的是,Android 原生支持 webp 图像。 So you can load webp images in ImageView same as other raster images.所以你可以像其他光栅图像一样在 ImageView 中加载 webp 图像。 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 .您可以从项目中选择 drawable 和 mipmap 文件夹,右键单击并选择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.按确定,它会将所有 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:让我们看看最终的结果:

APK 分析器示例 5

  • The res folder size decrease from 710KB to 597KB. res 文件夹大小从 710KB 减少到 597KB。

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.

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