简体   繁体   English

如何为proguard编写规则?

[英]How to write rules for proguard?

I was need to make implementation of proguard in my project 我需要在我的项目中实施proguard

my default proguard settings were like this 我的默认proguard设置是这样的

android {
...///

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

....////
}

Then I made some little changes 然后我做了一些小改变

android {
...///

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

....////
}

There is my proguard-ruler.pro file https://ideone.com/ccPvPv 有我的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>;
#}

I tried a lot of different way to make a rules for libs, but doesn't work 我尝试了很多其他方法来为libs制定规则,但没有用

And there output that I get if I am trying make build apk in release mode. 如果尝试在发布模式下进行构建apk,则会得到输出。

https://ideone.com/Lg8tOT https://ideone.com/Lg8tOT

So as far as I understand I get this issue because I need to add rules for external libs. 据我了解,我遇到了这个问题,因为我需要为外部库添加规则。

I saw this resource 我看到了这个资源

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

but it doesn't help me... 但这对我没有帮助...

I am not a strong in proguard , so tell me please how I can add this rules? 我在proguard方面不强,所以请告诉我如何添加此规则?

If I understand correctly I need to write my custom rules, but how to do it? 如果我理解正确,则需要编写自定义规则,但是该怎么做呢?

Feel free to ask 随便问

Thanks in advance 提前致谢

EDIT 编辑

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

Now gradle looks like 现在gradle看起来像

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

My proguard-rules.pro 我的proguard-rules.pro

https://ideone.com/kxsCEh https://ideone.com/kxsCEh

My errors now 我现在的错误

https://ideone.com/JhCE3o https://ideone.com/JhCE3o

Why do you want to set rules for Proguard , is default configuration giving you any error ?. 为什么要为Proguard设置规则,默认配置会给您带来任何错误? Anyway, check this links for detail information about Proguard 无论如何,请查看此链接以获取有关Proguard详细信息

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

Default configuration means you just put lines that is, 默认配置意味着您只需放置以下行:

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

By this lines Gradle use default Proguard rules. 通过这一行,Gradle使用默认的Proguard规则。

Dexguard (commercial version of Proguard) Dexguard(Proguard的商业版本)

If want to make more secure your code you can use Dexguard . 如果要使代码更安全,可以使用Dexguard Dexguard is a commercial version of Proguard , because dexguard comes with string encryption feature that makes code more secure. DexguardProguard的商业版本,因为dexguard带有string encryption功能,可使代码更安全。

shrinkResources true will compress your images ( png , jpeg , etc) shrinkResources true将压缩您的图像( pngjpeg等)

minifyEnabled true will obfuscate you code. minifyEnabled true会混淆您的代码。

Those two rules will shrink you code and the apk will be more light. 这两个规则将使您的代码缩水,并且apk将会更轻。

Since minify is enabled, you need to put some rules, rules that you put in proguard-rules.pro in order to keep classes unobfuscated because some methods needs to be visible for use. 由于启用了minify ,因此需要放置一些规则,将这些规则放置在proguard-rules.pro中,以使类保持清晰,因为某些方法需要可见才能使用。

For example, you have a class used by Firebase, User . 例如,您有一个Firebase使用的类User Methods and fields from that class need to be visible in order to be functional. 该类的方法和字段必须可见才能起作用。 Firebase will convert response to that class and needs to be visible. Firebase会将响应转换为该类,并且需要可见。

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

Some rules are defined by libraries and you need to add them in order to build the project. 一些规则是由库定义的,您需要添加它们才能构建项目。

Warnings can be avoided with -dontwarn rule. 使用-dontwarn规则可以避免警告。

-dontwarn cryptix.**

I don't know why and how , but code below works for me perfectly. 我不知道为什么和如何,但是下面的代码对我来说很完美。

so my grade now looks like this 所以我的成绩现在看起来像这样

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'
    }
}

...///

}

and there is my proguard-rules.pro 还有我的proguard-rules.pro

-ignorewarnings

-keep class * {
    public private *;
}

also I checked result of obfuscation and it is really work. 我也检查了混淆的结果,这确实是可行的。

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

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