2017-08-31 88 views
0

我需要做執行的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

回答

0

我不知道爲什麼和如何,但下面的代碼完全適合我。

所以我的成績現在看起來是這樣

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

還我檢查混淆的結果,它是真正的工作。

0

你爲什麼要設置規則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來刪除膨脹代碼或其他任何東西? –

0

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'來禁用。我將編輯我的帖子。 –