如何在 Android Studio 中使用 ProGuard?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20885725/
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-08-20 03:38:35  来源:igfitidea点击:

How to use the ProGuard in Android Studio?

androidandroid-studioproguard

提问by Felipe Porge Xavier

This is my first project in Android Studio, and the code of my apps are not obfuscated. Im using this configuration in build.gradle file: enter image description here

这是我在 Android Studio 中的第一个项目,我的应用程序代码没有被混淆。我在 build.gradle 文件中使用这个配置: 在此处输入图片说明

I'm using the Build > Generate Signed APK... with the Run Proguard checked. And, when I have tested using the Apk_OneClick.v4.2, my code is completly easy to read:

我正在使用 Build > Generate Signed APK... 并选中 Run Proguard。而且,当我使用 Apk_OneClick.v4.2 进行测试时,我的代码完全易于阅读:

enter image description here

在此处输入图片说明

Please, help-me. :(

请帮我。:(

采纳答案by Scott Barta

You're probably not actually signing the release build of the APK via the signing wizard. You can either build the release APK from the command line with the command:

您可能实际上并未通过签名向导对 APK 的发布版本进行签名。您可以使用以下命令从命令行构建发布 APK:

./gradlew assembleRelease

or you can choose the release variant from the Build Variantsview and build it from the GUI:

或者您可以从Build Variants视图中选择发布版本并从 GUI 构建它:

IDE main window showing Build Variants

显示构建变体的 IDE 主窗口

回答by pyus13

You can configure your build.gradle file for proguard implementation. It can be at module level or the project level.

您可以为 proguard 实现配置 build.gradle 文件。它可以在模块级别或项目级别。

 buildTypes {

    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

    }

}

The configuration shown is for debug level but you can write you own build flavors like shown below inside buildTypes:

显示的配置用于调试级别,但您可以在 buildTypes 中编写自己的构建风格,如下所示:

    myproductionbuild{
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }

Better to have your debug with minifyEnabled falseand productionbuild and other builds as minifyEnabled true.

最好将您的调试与minifyEnabled false生产构建和其他构建作为minifyEnabled true.

Copy your proguard-rules.txt file in the root of your module or project folder like

将您的 proguard-rules.txt 文件复制到您的模块或项目文件夹的根目录中,例如

$YOUR_PROJECT_DIR\YoutProject\yourmodule\proguard-rules.txt

$YOUR_PROJECT_DIR\YoutProject\yourmodule\proguard-rules.txt

You can change the name of your file as you want. After configuration use one of the three options available to generate your build as per the buildType

您可以根据需要更改文件的名称。配置后,使用三个可用选项之一根据 buildType 生成构建

  1. Go to gradle task in right panel and search for assembleRelease/assemble(#your_defined_buildtype)under module tasks

  2. Go to Build Variant in Left Panel and select the build from drop down

  3. Go to project root directory in File Explorer and open cmd/terminal and run

  1. 转到右侧面板中的 gradle 任务并assembleRelease/assemble(#your_defined_buildtype)在模块任务下搜索

  2. 转到左侧面板中的构建变体并从下拉列表中选择构建

  3. 在文件资源管理器中转到项目根目录并打开 cmd/terminal 并运行

Linux ./gradlew assembleRelease or assemble(#your_defined_buildtype)

Linux ./gradlew assembleRelease or assemble(#your_defined_buildtype)

Windows gradlew assembleRelease or assemble(#your_defined_buildtype)

视窗 gradlew assembleRelease or assemble(#your_defined_buildtype)

You can find apk in your module/build directory.

您可以在 module/build 目录中找到 apk。

More about the configuration and proguard files location is available at the link

有关配置和 proguard 文件位置的更多信息,请访问链接

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

回答by ViliusK

NB.: Now instead of

注意:现在而不是

runProguard false

you'll need to use

你需要使用

minifyEnabled false

回答by SK9

Try renaming your 'proguard-rules.txt' file to 'proguard-android.txt' and remove the reference to 'proguard-rules.txt' in your gradle file. The getDefaultProguardFile(...)call references a different default proguard file, one provided by Google and not that in your project. So remove this as well, so that here the gradle file reads:

尝试将您的“proguard-rules.txt”文件重命名为“proguard-android.txt”,并在您的 gradle 文件中删除对“proguard-rules.txt”的引用。该getDefaultProguardFile(...)调用引用了一个不同的默认 proguard 文件,该文件由 Google 提供,而不是您项目中的文件。所以也删除它,这样 gradle 文件就会显示:

buildTypes {
    release {
        runProguard true
        proguardFile 'proguard-android.txt'
    }
}

回答by mikeLundquist

The other answers here are great references on using proguard. However, I haven't seen an issue discussed that I ran into that was a mind bender. After you generate a signed release .apk, it's put in the /releasefolder in your app but my app had an apk that wasn't in the /releasefolder. Hence, I spent hours decompiling the wrong apk wondering why my proguard changes were having no affect. Hope this helps someone!

此处的其他答案是有关使用 proguard 的重要参考。但是,我还没有看到讨论过的问题是我遇到的令人费解的问题。生成签名版本 .apk 后,它会放在/release您的应用程序的文件夹中,但我的应用程序有一个不在该/release文件夹中的 apk 。因此,我花了几个小时反编译错误的 apk,想知道为什么我的 proguard 更改没有影响。希望这可以帮助某人!

回答by Bhavik Nathani

Here is Some of Most Common Proguard Rules that you need to add in proguard-rules.profile in Android Sutdio.

以下是一些最常见的 Proguard 规则,您需要在 Android Sutdio 的proguard-rules.pro文件中添加这些规则。

ButterKnife

牛油刀

 -keep class butterknife.** { *; }
 -dontwarn butterknife.internal.**
 -keep class **$$ViewBinder { *; }
 -keepclasseswithmembernames class * {
        @butterknife.* <fields>;
  }
 -keepclasseswithmembernames class * {
        @butterknife.* <methods>;
  }

Retrofit

改造

 -dontwarn retrofit.**
 -keep class retrofit.** { *; }
 -keepattributes Signature
 -keepattributes Exceptions

OkHttp3

OkHttp3

 -keepattributes Signature
 -keepattributes *Annotation*
 -keep class okhttp3.** { *; }
 -keep interface okhttp3.** { *; }
 -dontwarn okhttp3.** 
 -keep class sun.misc.Unsafe { *; }
 -dontwarn java.nio.file.*
 -dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement 

Gson

格森

 -keep class sun.misc.Unsafe { *; }
 -keep class com.google.gson.stream.** { *; }

Code obfuscation

代码混淆

-keepclassmembers class com.yourname.models** { <fields>; }