简体   繁体   English

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

[英]How to enable ProGuard obfuscation in Android Studio?

I have to protect my app by enabling Proguard obfuscation in Android Studio.我必须通过在 Android Studio 中启用 Proguard 混淆来保护我的应用程序。 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?那么谁能告诉我在我的应用程序中应用它的明确步骤吗?

I am doing this by the following steps:我正在通过以下步骤执行此操作:

  1. In Android Studio, open up an Android project.在 Android Studio 中,打开一个 Android 项目。

  2. Change to Project View.更改为项目视图。

  3. Change the following line:更改以下行:

    minifyEnable false to minifyEnable true minifyEnable falseminifyEnable true

  4. Set ProGuard Rules(optional)设置 ProGuard 规则(可选)

    4.1 In Project View, select the proguard-rules.pro file. 4.1 在项目视图中,选择 proguard-rules.pro 文件。

    4.2 Add in the following lines to tell ProGuard not to obfuscate certain classes. 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'.错误:任务 ':app:packageRelease' 的执行失败。 Unable to compute hash of D:\\Android\\Pojectname\\app\\build\\intermediates\\classes-proguard\\release\\classes.jar无法计算 D:\\Android\\Pojectname\\app\\build\\intermediates\\classes-proguard\\release\\classes.jar 的哈希值

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进入app的build.gradle文件
  2. enable the minifyEnabled true启用minifyEnabled true
  3. enable shrinkResources true to reduce the APK size启用shrinkResources true以减少APK 大小
  4. proguardFiles getDefaultProguardFile('proguard-android.txt') to enable the default one. proguardFiles getDefaultProguardFile('proguard-android.txt')启用默认的。 If you want to use your own proguard file then use the below rules.如果您想使用自己的 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有关更多详细信息,请访问此链接

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'. 错误:任务 ':app:packageRelease' 的执行失败。 > Unable to compute hash of /../AndroidStudioProjects/../classes.jar > 无法计算 /../AndroidStudioProjects/../classes.jar 的哈希值

if you are building the android project with jack, then it will automatically do the shrinking, obfuscation, repackaging and multidex.如果您使用 jack 构建 android 项目,那么它会自动进行收缩、混淆、重新打包和 multidex。 Just add below in :只需在下面添加:

defaultConfig {
       jackOptions {
            enabled true
        }        
    }

and in build types, mention the project proguard file :在构建类型中,提及项目proguard文件:

buildTypes {
        release {
            // Jack build environment does not require minifyEnabled or shrinkResources.
            // Conceptually, the jack 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

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

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