如何为proguard编写规则?

问题描述:

我需要做执行的ProGuard的在我的项目如何为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>; 
#} 

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

并且有输出,我得到如果我正在尝试make build 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

我不知道为什么和如何,但下面的代码完全适合我。

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

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 *; 
} 

还我检查混淆的结果,它是真正的工作。

你为什么要设置规则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' 

通过此行摇篮使用默认Proguard的规则。

Dexguard(商业版Proguard的的)

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

+0

你是什么意思默认配置? –

+0

进行了更改,并将它们添加到问题中,请看一下 –

+0

首先,您为什么要使用proguard来删除膨胀代码或其他任何东西? –

shrinkResources true将压缩你的图片(pngjpeg等)

minifyEnabled true会混淆你的代码。

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

由于minify已启用,因此需要在proguard-rules.pro中放置一些规则,规则以保持类未混淆,因为某些方法需要可见才能使用。

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

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

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

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

-dontwarn cryptix.** 
+0

所以只是为了我的理解。看看我有这个警告'警告:cryptix.util.test.TestGUI:无法找到引用的类java.awt.TextComponent',如果我正确使用了你,我需要做下一步'#-keep class cryptix.util.test .TestGUI。** {*;}'?是? –

+0

这是一个警告。应该用'-twtwarn'来禁用。我将编辑我的帖子。 –