Java 如何在 Android Studio 中启用 ProGuard 混淆?

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

How to Enabling ProGuard obfuscation in Android Studio?

javaandroidandroid-studio

提问by visual process

I have to protect my app by enabling Proguard obfuscation in Android Studio. I have searched for the process of how to apply it but i did not get any clear solution. When i try it, i always get an error. So can anyone tell me the clear steps to apply it in my app?

我必须通过在 Android Studio 中启用 Proguard 混淆来保护我的应用程序。我已经搜索了如何应用它的过程,但我没有得到任何明确的解决方案。当我尝试时,我总是得到一个错误。那么谁能告诉我在我的应用程序中应用它的明确步骤吗?

I am doing this by the following steps:

我正在通过以下步骤执行此操作:

  1. In Android Studio, open up an Android project.

  2. Change to Project View.

  3. Change the following line:

    minifyEnable falseto minifyEnable true

  4. Set ProGuard Rules(optional)

    4.1 In Project View, select the proguard-rules.pro file.

    4.2 Add in the following lines to tell ProGuard not to obfuscate certain classes.

    -keepclassmembers class com.dom925.xxxx 
    {
      public *
    }
    
  1. 在 Android Studio 中,打开一个 Android 项目。

  2. 更改为项目视图。

  3. 更改以下行:

    minifyEnable falseminifyEnable true

  4. 设置 ProGuard 规则(可选)

    4.1 在项目视图中,选择 proguard-rules.pro 文件。

    4.2 添加以下几行以告诉 ProGuard 不要混淆某些类。

    -keepclassmembers class com.dom925.xxxx 
    {
      public *
    }
    

Error that I am getting by following the steps are

我按照步骤得到的错误是

Error:Execution failed for task ':app:packageRelease'. Unable to compute hash of D:\Android\Pojectname\app\build\intermediates\classes-proguard\release\classes.jar

错误:任务 ':app:packageRelease' 的执行失败。无法计算 D:\Android\Pojectname\app\build\intermediates\classes-proguard\release\classes.jar 的哈希值

采纳答案by Harshad

I figured out the problem:

我解决了这个问题:

Open up the proguard-rules.pro for your project and add this to the bottom:

打开项目的 proguard-rules.pro 并将其添加到底部:

-dontwarn java.nio.file.Files
-dontwarn java.nio.file.Path
-dontwarn java.nio.file.OpenOption
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Basically how I solved it was this I tried to run my app in 'release' mode and got a bunch of errors similar to this guy here: https://github.com/square/okio/issues/144

基本上我是如何解决这个问题的,我试图在“发布”模式下运行我的应用程序,但遇到了一堆类似于这里的人的错误:https: //github.com/square/okio/issues/144

I pretty much followed what he said and fixed it.

我几乎按照他说的去做并修复了它。

Hope this can help others with generating their APK's!

希望这可以帮助其他人生成他们的 APK!

visit more detail here :

在此处访问更多详细信息:

Error:Execution failed for task ':app:packageRelease'. > Unable to compute hash of /../AndroidStudioProjects/../classes.jar

错误:任务 ':app:packageRelease' 的执行失败。> 无法计算 /../AndroidStudioProjects/../classes.jar 的哈希值

回答by Maheshwar Ligade

To enable ProGuard in Android Studio.

在 Android Studio 中启用 ProGuard。

Below is the sample how to enable default ProGuard in Android Studio.

下面是如何在 Android Studio 中启用默认 ProGuard 的示例。

  1. Go to the build.gradle file of app
  2. enable the minifyEnabled true
  3. enable shrinkResources trueto reduce the APK size
  4. proguardFiles getDefaultProguardFile('proguard-android.txt')to enable the default one. If you want to use your own proguard file then use the below rules.

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    
        debug {
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    
  1. 进入app的build.gradle文件
  2. 启用minifyEnabled true
  3. 启用 shrinkResources true以减少APK 大小
  4. proguardFiles getDefaultProguardFile('proguard-android.txt')启用默认的。如果您想使用自己的 proguard 文件,请使用以下规则。

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

The link with ProGuard settings for Android and other settings are available in these links:

这些链接中提供了包含适用于 Android 的 ProGuard 设置和其他设置的链接:

For more detail go through this link

有关更多详细信息,请访问此链接

回答by appapurapu

if you are building the android project with Hyman, then it will automatically do the shrinking, obfuscation, repackaging and multidex. Just add below in :

如果您使用 Hyman 构建 android 项目,那么它会自动进行收缩、混淆、重新打包和 multidex。只需在下面添加:

defaultConfig {
       HymanOptions {
            enabled true
        }        
    }

and in build types, mention the project proguardfile :

在构建类型中,提及项目proguard文件:

buildTypes {
        release {
            // Hyman build environment does not require minifyEnabled or shrinkResources.
            // Conceptually, the Hyman compiler consolidates the functionality of javac, ProGuard, and dex in a single conversion step
            //minifyEnabled = true      
            //shrinkResources true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
       }

       debug {
...................
        }
    } 

To disable the ProGuard obfuscation, it is required to add below line in your proguard-project.txt file

要禁用 ProGuard 混淆,需要在 proguard-project.txt 文件中添加以下行

####No obfuscation
-dontobfuscate