開源混淆工具ProGuard配置詳解及配置實例(轉)

ProGuard的作用:
 
1.創建緊湊的代碼文檔是爲了更快的網絡傳輸,快速裝載和更小的內存佔用.
2.創建的程序和程序庫很難使用反向工程.
3.所以它能刪除來自源文件中的沒有調用的代碼
4.充分利用java6的快速加載的優點來提前檢測和返回java6中存在的類文件.
 
參數:
 
-include {filename}    從給定的文件中讀取配置參數
-basedirectory {directoryname}    指定基礎目錄爲以後相對的檔案名稱
-injars {class_path}    指定要處理的應用程序jar,war,ear和目錄
-outjars {class_path}    指定處理完後要輸出的jar,war,ear和目錄的名稱
-libraryjars {classpath}    指定要處理的應用程序jar,war,ear和目錄所需要的程序庫文件
-dontskipnonpubliclibraryclasses    指定不去忽略非公共的庫類。
-dontskipnonpubliclibraryclassmembers    指定不去忽略包可見的庫類的成員。

保留選項

-keep {Modifier} {class_specification}    保護指定的類文件和類的成員
-keepclassmembers {modifier} {class_specification}    保護指定類的成員,如果此類受到保護他們會保護的更好
-keepclasseswithmembers {class_specification}    保護指定的類和類的成員,但條件是所有指定的類和類成員是要存在。
-keepnames {class_specification}    保護指定的類和類的成員的名稱(如果他們不會壓縮步驟中刪除)
-keepclassmembernames {class_specification}    保護指定的類的成員的名稱(如果他們不會壓縮步驟中刪除)
-keepclasseswithmembernames {class_specification}    保護指定的類和類的成員的名稱,如果所有指定的類成員出席(在壓縮步驟之後)
-printseeds {filename}    列出類和類的成員-keep選項的清單,標準輸出到給定的文件
 
壓縮

-dontshrink    不壓縮輸入的類文件
-printusage {filename}
-whyareyoukeeping {class_specification}    
 
優化

-dontoptimize    不優化輸入的類文件
-assumenosideeffects {class_specification}    優化時假設指定的方法,沒有任何副作用
-allowaccessmodification    優化時允許訪問並修改有修飾符的類和類的成員
 
混淆

-dontobfuscate    不混淆輸入的類文件
-printmapping {filename}
-applymapping {filename}    重用映射增加混淆
-obfuscationdictionary {filename}    使用給定文件中的關鍵字作爲要混淆方法的名稱
-overloadaggressively    混淆時應用侵入式重載
-useuniqueclassmembernames    確定統一的混淆類的成員名稱來增加混淆
-flattenpackagehierarchy {package_name}    重新包裝所有重命名的包並放在給定的單一包中
-repackageclass {package_name}    重新包裝所有重命名的類文件中放在給定的單一包中
-dontusemixedcaseclassnames    混淆時不會產生形形色色的類名
-keepattributes {attribute_name,...}    保護給定的可選屬性,例如LineNumberTable, LocalVariableTable, SourceFile, Deprecated, Synthetic, Signature, and InnerClasses.
-renamesourcefileattribute {string}    設置源文件中給定的字符串常量


Ant Example:

<!-- This Ant build file illustrates how to process applications,
     by including ProGuard-style configuration options.
     Usage: ant -f applications2.xml -->

<project name="Applications" default="obfuscate" basedir="../..">

<target name="obfuscate">
  <taskdef resource="proguard/ant/task.properties"
           classpath="lib/proguard.jar" />

  <proguard>

    <!-- Specify the input jars, output jars, and library jars. -->

    -injars  in.jar
    -outjars out.jar

    -libraryjars ${java.home}/lib/rt.jar
    <!-- -libraryjars junit.jar    -->
    <!-- -libraryjars servlet.jar  -->
    <!-- -libraryjars jai_core.jar -->
    <!-- ...                       -->

    <!-- Save the obfuscation mapping to a file, and preserve line numbers. -->

    -printmapping out.map
    -renamesourcefileattribute SourceFile
    -keepattributes SourceFile,LineNumberTable

    <!-- Preserve all annotations. -->

    -keepattributes *Annotation*

    <!-- Preserve all public applications. -->

    -keepclasseswithmembers public class * {
        public static void main(java.lang.String[]);
    }

    <!-- Preserve all native method names and the names of their classes. -->

    -keepclasseswithmembernames class * {
        native &lt;methods&gt;;
    }

    <!-- Preserve the methods that are required in all enumeration classes. -->

    -keepclassmembers class * extends java.lang.Enum {
        public static **[] values();
        public static ** valueOf(java.lang.String);
    }

    <!-- Explicitly preserve all serialization members. The Serializable
         interface is only a marker interface, so it wouldn't save them.
         You can comment this out if your library doesn't use serialization.
         If your code contains serializable classes that have to be backward
         compatible, please refer to the manual. -->

    -keepclassmembers class * implements java.io.Serializable {
        static final long serialVersionUID;
        static final java.io.ObjectStreamField[] serialPersistentFields;
        private void writeObject(java.io.ObjectOutputStream);
        private void readObject(java.io.ObjectInputStream);
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    }

    <!-- Your application may contain more items that need to be preserved;
         typically classes that are dynamically created using Class.forName -->

  </proguard>
</target>

</project>

發佈了106 篇原創文章 · 獲贊 2 · 訪問量 3667
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章