简体   繁体   English

为什么 release .apk 比 th debug .apk 小

[英]Why is the release .apk smaller than th debug .apk

In my app's example, the debug apk is 20Mbs (from 13Mbs, after upgrading the Gradle version and Gradle Plugin version), and the release apk is just 5Mb.在我的应用程序示例中,debug apk 为 20Mbs(从 13Mbs 开始,升级 Gradle 版本和 Gradle Plugin 版本后),而发布 apk 仅为 5Mb。 Why is that?这是为什么?

In the build.gradle file, the buildTypes part has remained unchanged, so there hasn't been any optimizations / shrinking from the R8 compiler or from ProGuard.在 build.gradle 文件中, buildTypes部分保持不变,因此 R8 编译器或 ProGuard 没有任何优化/收缩。

android {
    ...
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

RELEASE:释放: 发布 classes.dex --> 2.6Mb lib ---> 1.7Mb

DEBUG:调试: 调试 classes.dex --> 3.4Mb 库 ---> 32.2Mb

You can see the big difference in size in the lib folder, among others.您可以在lib文件夹等中看到大小的巨大差异。

There are more optimizations happen during the release builds besides ProGuard and R8.除了 ProGuard 和 R8 之外,在发布版本期间还有更多优化。 According to the documentation https://developer.android.com/studio/build/shrink-code.html根据文档https://developer.android.com/studio/build/shrink-code.html

When you build you project using Android Gradle plugin 3.4.0 or higher , the plugin no longer uses ProGuard to perform compile-time code optimization.当您使用Android Gradle 插件 3.4.0 或更高版本构建项目时,该插件不再使用 ProGuard 来执行编译时代码优化。 Instead, the plugin works with the R8 compiler to handle the following compile-time tasks:相反,该插件与 R8 编译器一起处理以下编译时任务:

Code shrinking (or tree-shaking): detects and safely removes unused classes, fields, methods, and attributes from your app and its library dependencies (making it a valuable tool for working around the 64k reference limit).代码收缩(或摇树):检测并安全地从您的应用程序及其库依赖项中删除未使用的类、字段、方法和属性(使其成为解决 64k 引用限制的宝贵工具)。 For example, if you use only a few APIs of a library dependency, shrinking can identify library code that your app is not using and remove only that code from your app.例如,如果您只使用库依赖项的几个 API,收缩可以识别您的应用程序未使用的库代码,并仅从您的应用程序中删除该代码。 To learn more, go to the section about how to shrink your code.要了解更多信息,请转到有关如何缩减代码的部分。

Resource shrinking: removes unused resources from your packaged app, including unused resources in your app's library dependencies.资源收缩:从打包的应用程序中删除未使用的资源,包括应用程序库依赖项中未使用的资源。 It works in conjunction with code shrinking such that once unused code has been removed, any resources no longer referenced can be safely removed as well.它与代码收缩一起工作,这样一旦删除了未使用的代码,也可以安全地删除不再引用的任何资源。 To learn more, go to the section about how to shrink your resources.要了解更多信息,请转到有关如何缩减资源的部分。

Obfuscation: shortens the name of classes and members, which results in reduced DEX file sizes.混淆:缩短类和成员的名称,从而减少 DEX 文件大小。 To learn more, go to the section about how to obfuscate your code.要了解更多信息,请转到有关如何混淆代码的部分。

Optimization: inspects and rewrites your code to further reduce the size of your app's DEX files.优化:检查并重写您的代码以进一步减小应用 DEX 文件的大小。 For example, if R8 detects that the else {} branch for a given if/else statement is never taken, R8 removes the code for the else {} branch.例如,如果 R8 检测到给定 if/else 语句的 else {} 分支从未被采用,R8 将删除 else {} 分支的代码。 To learn more, go to the section about code optimization.要了解更多信息,请转到有关代码优化的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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