android 缩小_使用R8缩小您的Android应用

android 缩小

An Android app can serve many purposes for modern enterprises. You can come up with a customized Android app with desired features to streamline the business and connect with your customers with ease.

Android应用程序可以为现代企业提供多种用途。 您可以提出一个具有所需功能的自定义Android应用,以简化业务并轻松与客户联系。

However, you need to focus on the size of your Android app. It is the app’s size that decides whether people will install your app or not. Thanks to R8, you can optimize your app with a lighter APK.

但是,您需要关注Android应用程序的大小。 应用程序的大小决定了人们是否会安装您的应用程序。 多亏了R8,您可以使用更轻的APK优化应用。

Let’s discuss the role of R8 from the viewpoint of Android app development.

让我们从Android应用程序开发的角度讨论R8的作用。

在Android Studio中启用R8的配置 (Configuration to Enable R8 in Your Android Studio)

  1. Android Studio version 3.4.0 or higher.

    Android Studio版本3.4.0或更高版本。
  2. Android Gradle plugin version 3.4.0.

    Android Gradle插件版本3.4.0。

  3. minifyEnabled true (build.gradle project level for code optimization).

    minifyEnabled true ( build.gradle项目级别用于代码优化)。

  4. shrinkResources true (build.gradle project level for resource shrinking = optional).

    shrinkResources true (用于资源收缩的build.gradle项目级别=可选)。

When you use Android Studio 3.4 or the Android Gradle plugin 3.4.0 or higher, R8 is the default compiler.

当您使用Android Studio 3.4或Android Gradle插件3.4.0或更高版本时,R8是默认编译器。

You might be thinking what the implementation step to run or enable R8 in the project should be, please don’t think so much about it because there is no single line to do this as long as you’re currently working with the above-mentioned features.

您可能会想在项目中运行或启用R8的实现步骤应该是什么,请不要考虑太多,因为只要您当前正在使用上述功能,就没有一行可以执行此操作特征。

“Keep in mind, R8 is designed to work with your existing ProGuard rules, so you’ll likely not need to take any actions to benefit from R8" — Android Dev

“请记住,R8旨在与您现有的ProGuard规则配合使用,因此您可能无需采取任何措施即可从R8中受益” – Android Dev

Google is intensely focused on build performance, it shows how dedicated Google is to do it and how far they are willing to go to get it.

Google高度关注构建性能,它显示了Google致力于做到这一点以及他们愿意付出多大的努力。

You might be thinking about what the process is behind the scenes, right? So, let’s look at the process…

您可能正在考虑幕后流程是什么,对吗? 因此,让我们看一下流程...

生成APK时的构建过程 (Build Process While Generating APK)

  1. Desugar converts Java 8 features during the compilation time into something recognizable by Dalvik/ART compilers.

    Desugar在编译期间将Java 8功能转换为Dalvik / ART编译器可以识别的功能。

  2. ProGuard removes unused classes, fields, methods, and attributes.

    ProGuard删除未使用的类,字段,方法和属性。

  3. The DEX compiler converts Java bytecode to Android’s .dex file format.

    DEX编译器将Java字节码转换为Android的.dex文件格式。

Later, in 2017, Google introduced D8, an optional replacement of the DEX compiler.

后来在2017年,谷歌推出了D8,这是DEX编译器的可选替代品。

使用D8,构建过程如下所示 (With D8, the Build Process Looks Like This)

Image for post
Source 资源

ProGuard is the technique to shrink the code, which detects and removes the unused classes, fields, methods, packages, and attributes (including code libraries).

ProGuard是一种收缩代码的技术,它可以检测并删除未使用的类,字段,方法,程序包和属性(包括代码库)。

  1. Shrinking: It shrinks the code by detecting and removing unused code.

    收缩:它通过检测并删除未使用的代码来收缩代码。
  2. Optimizer: It analyzes and optimizes the byte code.

    优化器:它分析并优化字节码。
  3. Obfuscator: It renames the optimized classes, methods with short names. (Used to prevent reverse engineering).

    混淆器:它使用短名称重命名优化的类,方法。 (用于防止逆向工程)。

D8 runs faster and produces smaller .dex files with equivalent or better runtime performance when compared to the historic compiler, DX.

与历史悠久的编译器DX相比,D8运行更快,并且生成的.dex文件更小,具有同等或更好的运行时性能。

Google made D8 the default DEX compiler in 2018. Later, in 2019, Google introduced R8, which is a replacement for ProGuard and has all the features of D8.

谷歌在2018年使D8成为默认的DEX编译器。后来,在2019年,谷歌推出了R8,它是ProGuard的替代品,具有D8的所有功能。

使用R8,构建过程如下所示 (With R8, the Build Process Looks Like This)

Image for post
Source 资源

As a developer, you all know that we can believe only in the practical result. So, that’s why Android officials have uploaded the below results to better understand how R8 works faster and better than ProGuard.

作为开发人员,你们都知道我们只能相信实际结果。 因此,这就是Android官员上传以下结果以更好地了解R8如何比ProGuard更快和更好地工作的原因。

Here are the results of the Santa Tracker app.

这是圣诞老人追踪器应用程序的结果

Image for post
Source 资源
Image for post
Source 资源
Image for post
Source 资源

R8配置文件 (R8 Configuration Files)

  1. proguard-rules.pro (Android Studio creates this file in the root directory of that module, you can add your custom ProGuard to keep rules according to your requirements.)

    proguard-rules.pro (Android Studio在该模块的根目录中创建此文件,您可以添加自定义ProGuard来根据需要保留规则。)

  2. proguard-android-optimize.txt (Generated by the Android Gradle plugin at runtime.)

    proguard-android-optimize.txt (由Android Gradle插件在运行时生成。)

If you want to see the reports of the R8 compiler, add the below lines to the proguard-rules.pro file

如果要查看R8编译器的报告,请将以下行添加到proguard-rules.pro文件中

  1. -printconfiguration ~/tmp/full-r8-config.txt (Report of R8 applies when building your projects.)

    -printconfiguration ~/tmp/full-r8-config.txt (R8的报告在构建项目时适用。)

  2. -printusage <output-dir>/usage.txt (Report of removed code by R8.)

    -printusage <output-dir>/usage.txt (R8删除的代码的报告。)

  3. -printseeds <output-dir>/seeds.txt (Report of entry points that R8 determines from your project’s keep rules.)

    -printseeds <output-dir>/seeds.txt (R8从项目的保留规则确定的入口点报告。)

R8和ProGuard之间的区别 (Difference Between R8 and ProGuard)

R8 (R8)

R8 combines desugaring, shrinking, obfuscating, optimizing, and dexing all in one step — resulting in noticeable build performance improvements.

R8一步将除糖,收缩,模糊,优化和脱色结合在一起,从而显着提高了构建性能。

ProgGuard (ProgGuard)

ProGuard integrates only shrinking, obfuscating, and optimizing.

ProGuard仅集成了缩小,模糊和优化。

According to the above statements, ProGuard only works to reduce APK size. So, other processes like desugaring and dexing are included additionally in the build process so they take more time to build.

根据以上声明,ProGuard仅可减小APK大小。 因此,在构建过程中还包括其他过程,例如脱糖和脱胶,因此它们将花费更多的时间来构建。

R8 works for three different things in one step, so it finishes the whole build process in one step to reduce the overall time of compilation.

R8一步就可以处理三种不同的事物,因此它一步就完成了整个构建过程,从而减少了编译的总时间。

I definitely believe that you will want to enable R8 in your project to experience a reduction in build process time and reduced size APK.

我绝对相信,您将希望在项目中启用R8,以减少构建过程时间并减小APK大小。

So, that’s it for R8.

因此, R8就是这样。

Happy coding, I hope you have enjoyed reading my blog.

祝您编程愉快,希望您喜欢阅读我的博客。

翻译自: https://medium.com/better-programming/shrink-your-android-app-with-r8-afe17c4d393

android 缩小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值