【问题标题】:How to write rules for proguard?如何为proguard编写规则?
【发布时间】:2017-08-31 07:59:17
【问题描述】:

我需要在我的项目中实现proguard

我的默认 proguard 设置是这样的

android {
...///

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

....////
}

然后我做了一些小改动

android {
...///

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

....////
}

有我的proguard-ruler.pro文件https://ideone.com/ccPvPv

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/Shahar/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# 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 *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#If you not sure about proguard process so uncomment next line
#-dontobfuscate

# view res/layout/design_layout_snackbar_include.xml #generated:18
#-keep class android.support.design.internal.SnackbarContentLayout { <init>
(...); }

# view res/layout/activity_main.xml #generated:11
#-keep class android.support.design.widget.AppBarLayout { <init>(...); }

# view AndroidManifest.xml #generated:19
#-keep class ru.jollydroid.athdemo.MainActivity { <init>(...); }

#

# Proguard configuration for Jackson 2.x (fasterxml package instead of 
codehaus  package)
#-keep class com.fasterxml.jackson.databind.ObjectMapper {
#    public <methods>;
#    protected <methods>;
#}
#-keep class com.fasterxml.jackson.databind.ObjectWriter {
#    public ** writeValueAsString(**);
#}
#-keepnames class com.fasterxml.jackson.** { *; }


#-----------------
#-keepnames com.fasterxml.jackson.databind.** { *; }
#
#-keepnames com.squareup.okhttp.** { *; }
#
#-keepnames cryptix.util.test.** { *; }
#
#-keepnames jp.wasabeef.recyclerview.animators.** { *; }
#
#-keepnames cryptix.util.gui.** { *; } 
#
#-keepnames ui.activities.** { *; }
#
#-keepnames ui.adapters.** { *; } 
#
#-keepnames ui.fragments.** { *; }
#
#-keepnames webServices.controllers.** { *; }
#-------------------------

#Was worked
#-----------------
#-dontwarn com.fasterxml.jackson.databind.**
#
#-dontwarn com.squareup.okhttp.**
#
#-dontwarn cryptix.util.test.**
#
#-dontwarn jp.wasabeef.recyclerview.animators.**
#
#-dontwarn cryptix.util.gui.**
#
# -dontwarn ui.activities.**
#
#-dontwarn ui.adapters.**
#
#-dontwarn ui.fragments.**
#
#-dontwarn webServices.controllers.**
#-------------------------

#-dontwarn java.awt.**
#-dontwarn java.beans.Beans
#-dontwarn javax.security.**
#-keep class javamail.** {*;}
#-keep class javax.mail.** {*;}
#-keep class javax.activation.** {*;}
#-keep class com.sun.mail.dsn.** {*;}
#-keep class com.sun.mail.handlers.** {*;}
#-keep class com.sun.mail.smtp.** {*;}
#-keep class com.sun.mail.util.** {*;}
#-keep class mailcap.** {*;}
#-keep class mimetypes.** {*;}
#-keep class myjava.awt.datatransfer.** {*;}
#-keep class org.apache.harmony.awt.** {*;}
#-keep class org.apache.harmony.misc.** {*;}

# Proguard configuration for Jackson 2.x (fasterxml package instead of 
codehaus 
 package)


#-keep class com.fasterxml.jackson.annotation.** { *; }
#
#-dontwarn com.fasterxml.jackson.databind.**
#
#-keepclassmembers class com.myapp.models.** { *; }
#
#-keepattributes SourceFile,LineNumberTable
#-keep class com.parse.*{ *; }
#-keep class android.content.res.Xm.ResourceParser.** { *; }
#-keep class com.googlecode.** { *; }
#-dontwarn com.parse.**
#-dontwarn com.squareup.picasso.**
#-keepclasseswithmembernames class * {
#    native <methods>;
#}

我尝试了很多不同的方法来为库制定规则,但都不起作用

如果我尝试在发布模式下构建 apk,我会得到输出。

https://ideone.com/Lg8tOT

据我了解,我遇到了这个问题,因为我需要为外部库添加规则。

我看到了这个资源

https://github.com/krschultz/android-proguard-snippets

但这对我没有帮助......

我不是很擅长proguard,所以请告诉我如何添加这个规则?

如果我理解正确,我需要编写自定义规则,但该怎么做?

欢迎提问

提前致谢

编辑

进行更改https://gist.github.com/albinmathew/c4436f8371c9c41461ab

现在 gradle 看起来像

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

我的proguard-rules.pro

https://ideone.com/kxsCEh

我现在的错误

https://ideone.com/JhCE3o

【问题讨论】:

    标签: java android


    【解决方案1】:

    shrinkResources true 将压缩您的图像(pngjpeg 等)

    minifyEnabled true 会混淆你的代码。

    这两条规则会缩小你的代码,apk 会更轻。

    由于启用了minify,您需要放置一些规则,您放入proguard-rules.pro 的规则以保持类不被混淆,因为某些方法需要可见才能使用。

    例如,您有一个 Firebase 使用的类,User。该类的方法和字段需要可见才能正常工作。 Firebase 会将响应转换为该类并且需要可见。

    #-keep class .User.** {*;}
    

    一些规则是由库定义的,您需要添加它们才能构建项目。

    使用-dontwarn 规则可以避免警告。

    -dontwarn cryptix.**
    

    【讨论】:

    • 所以只是为了我的理解。看我有这个警告Warning:cryptix.util.test.TestGUI: can't find referenced class java.awt.TextComponent,如果我理解正确,我需要做下一个'#-keep class cryptix.util.test.TestGUI.** {*;}`?是吗?
    • 这是一个警告。应使用-dontwarn 禁用。我将编辑我的帖子。
    【解决方案2】:

    您为什么要为Proguard 设置规则,默认配置是否会给您带来任何错误?无论如何,请查看此链接以获取有关 Proguard 的详细信息

    https://gist.github.com/albinmathew/c4436f8371c9c41461ab https://gist.github.com/Jackgris/c4a71328b1ae346cba04

    默认配置意味着你只放行,即,

    minifyEnabled true
    useProguard true
    shrinkResources true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    

    通过这一行,Gradle 使用默认的 Proguard 规则。

    Dexguard(Proguard 的商业版)

    如果想让您的代码更安全,您可以使用DexguardDexguardProguard 的商业版本,因为dexguard 带有string encryption 功能,使代码更安全。

    【讨论】:

    • 默认配置是什么意思?
    • 进行了修改并添加到问题中,请看一下
    • 首先,为什么要使用 proguard 来删除膨胀代码或其他任何东西?
    • 我需要混淆我的代码...让逆向工程变得更加困难
    • 好的,那么您只需在您的 gradle 中添加 minifyEnabled true。但是如果你想让你的代码更安全,你可以使用DexguardDexguardProguard的商业版
    【解决方案3】:

    我不知道为什么以及如何,但是下面的代码非常适合我。

    所以我的成绩现在看起来像这样

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

    还有我的proguard-rules.pro

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

    我还检查了混淆的结果,它确实有效。

    【讨论】:

      猜你喜欢
      • 2020-01-06
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多