java Android 源代码如何缩小和混淆?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13669592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:33:15  来源:igfitidea点击:

how Android source code minify and obfuscate?

javaandroidminify

提问by crossRT

I had done my apps, any idea to minify the Android and Java code? can it save the apk file size and improve performance? compare to unminify source code.

我已经完成了我的应用程序,有什么想法可以缩小 Android 和 Java 代码吗?它可以节省apk文件大小并提高性能吗?与 unminify 源代码相比。

回答by Rickard

Enable shrinkResources in your build type. Note that it requires minify to be enabled. In your Gradle file:

在您的构建类型中启用收缩资源。请注意,它需要启用 minify。在您的 Gradle 文件中:

android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
        }
    }
}

The accepted answer is now outdated, so I added this answer if someone else stumbles upon this question.

已接受的答案现已过时,因此如果其他人偶然发现此问题,我会添加此答案。

回答by Nicklas Gnejs Eriksson

You can use ProGuard for this. Note however that your code will be obfuscated when it comes to crash reports etc so you need to keep obfuscation maps to be able to retrieve the stack traces between versions etc. It does however protect you a lot better from reverse engineering your .APK.

您可以为此使用 ProGuard。但是请注意,当涉及崩溃报告等时,您的代码将被混淆,因此您需要保留混淆映射,以便能够检索版本之间的堆栈跟踪等。但是,它确实可以更好地保护您免受 .APK 的逆向工程。

More info@Android Developers: http://developer.android.com/tools/help/proguard.html

更多信息@Android 开发者:http: //developer.android.com/tools/help/proguard.html

回答by Jave

Just enable ProGuard, it shrinks and obfuscates your application. It is included with the Android developer tools.

只需启用ProGuard,它就会缩小和混淆您的应用程序。它包含在 Android 开发人员工具中。

回答by Ajay Mistry

There are three part

共有三部分

  1. minifyEnabled
  2. useProguard
  3. shrinkResources
  1. 启用缩小
  2. 使用Proguard
  3. 收缩资源

if you want to remove unused part of coding then you can use shrinkResources true

如果您想删除未使用的编码部分,则可以使用 shrinkResources true

if you don't want to use default files like dmp.txt,mapping.txt,seeds.txt,usage.txtetc then you should use useProguard false

如果你不希望使用默认的文件,如dmp.txtmapping.txtseeds.txtusage.txt等那么你应该使用useProguard false

if you want to shrink your code and resource then you have to use minifyEnabled true

如果你想缩小你的代码和资源,那么你必须使用 minifyEnabled true

for more details refer this link

有关更多详细信息,请参阅此链接

回答by Marcin Orlowski

Minificationmakes little sense in non scripting languages. What you can do is to use ProGuard to i.e. remove unused portions of the code, but as the major bloater in Android application are usually resources (drawables), ensure you do not have i.e. too many of them, or too big in size.

缩小在非脚本语言中毫无意义。您可以做的是使用 ProGuard 来删除代码中未使用的部分,但由于 Android 应用程序中的主要膨胀通常是资源(可绘制),请确保您没有太多或太大的资源。

回答by Samuel Moshie

This worked for me and i disable instant run

这对我有用,我禁用了即时运行

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

        }

in proguard-rules.pro

在 proguard-rules.pro 中

-ignorewarnings
-keep class * {
    public private *;
}