【问题标题】:How to prepare a proguard file and whats included in it?如何准备 proguard 文件以及其中包含的内容?
【发布时间】:2016-11-07 21:49:50
【问题描述】:

我正在 Android Studio 中开发一个应用程序,我想将 proguard 添加到我的应用程序中。但我不知道该怎么办?我也想了解它的背景。谁能给我看点东西?谢谢。

【问题讨论】:

    标签: android gradle functional-programming android-gradle-plugin android-proguard


    【解决方案1】:

    在你的 gradle 文件中设置 trueminifyEnabled

    您可以定义是否在调试、发布或两者中启用 proguard

    buildTypes {
            release {
                minifyEnabled true
                proguardFiles 'proguard-rules.pro'
            }
            debug {
                minifyEnabled false
                proguardFiles 'proguard-rules.pro'
            }
        }
    

    你也可以设置 proguardFiles 来配置他,查看site 以查看有关它的文档,看看这个例子:

    # Add project specific ProGuard rules here.
    # By default, the flags in this file are appended to flags specified
    # in /Users/balysv/Documents/Android/sdk/tools/proguard/proguard-android.txt
    # You can edit the include path and order by changing the ProGuard
    # include property in project.properties.
    #
    # For more details, see
    #   http://developer.android.com/guide/developing/tools/proguard.html
    
    # Add any project specific keep options here:
    
    # If your project uses WebView with JS, uncomment the following
    # and specify the fully qualified class name to the JavaScript interface
    # class:
    #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
    #   public *;
    #}
    
    -optimizationpasses 5
    -dontskipnonpubliclibraryclasses
    -dontskipnonpubliclibraryclassmembers
    -dontpreverify
    -verbose
    

    如果您想使用自定义字典进行代码混淆,请使用您的字典文件设置此配置:

    -obfuscationdictionary proguard-dic.txt
    -classobfuscationdictionary proguard-dic.txt
    -packageobfuscationdictionary proguard-dic.txt
    

    字典文件是一个简单的文本文件,其中包含您要用来混淆代码的标签,每行 1 个标签。

    【讨论】:

    • 谢谢,它的内容如何?
    • 如何为我的依赖项应用此配置?
    • 你的依赖也会被混淆,你可以在本站上传你的apk,看看反编译的结果。 javadecompilers.com/apk
    • 你太棒了!谢谢。我会马上试试这个。好的编码:)
    【解决方案2】:

    为了使您的 APK 文件尽可能小,您应该启用压缩功能以删除发布版本中未使用的代码和资源。

    ProGuard 提供代码收缩功能,它可以检测并删除打包应用程序中未使用的类、字段、方法和属性,包括包含的代码库中的那些(使其成为解决 64k 引用限制的有用工具) .

    ProGuard 还优化字节码,删除未使用的代码指令,并用短名称混淆剩余的类、字段和方法。混淆代码使您的 APK 难以进行逆向工程,这在您的应用使用安全敏感功能(例如许可验证)时尤其有用。

    例如,build.gradle 文件中的以下 sn-p 为发布构建启用代码收缩:

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

    Example of use from Proguard, from Android Studio

    【讨论】:

    • 谢谢老哥,我去看看。好的,我如何为我的依赖项使用 proguard?
    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多