ProGuard Usage 翻译
Usage ?用法
To run ProGuard, just type:
運行ProGuard,只需輸入:
java -jar proguard.jar?options?...
You can find the ProGuard jar in the?lib?directory of the ProGuard distribution. Alternatively, the?bin?directory contains some short Linux and Windows scripts containing this command. Typically, you'll put most options in a configuration file (say,?myconfig.pro), and just call:
你可以在ProGuard lib目錄中找到 ProGuard jar。一些包含此命令的LInux和Windows 短腳本可在bin 目錄中找到供你選擇。典型的用法是,你將大多數的參數選項寫入一個配置文件中(如,myconfig.pro),然后運行:
java -jar proguard.jar @myconfig.pro
You can combine command line options and options from configuration files. For instance:
你可以將命令行選項和配置文件選項結合使用。舉個栗子:
java -jar proguard.jar @myconfig.pro -verbose
You can add comments in a configuration file, starting with a?#?character and continuing until the end of the line.
你可以在配置文件添加注釋,用法是以#開頭,接在#后面的單行都是注釋。
Extra whitespace between words and delimiters is ignored. File names with spaces or special characters should be quoted with single or double quotes.
文字和分隔符中間的空格是被忽略的。包含空格或者特殊字符的文件名需要用單引號或者雙引號引用。
Options can be grouped arbitrarily in arguments on the command line and in lines in configuration files. This means that you can quote arbitrary sections of command line options, to avoid shell expansion of special characters, for instance.
命令行選項可以在命令行和配置文件中任意組合。這就意味著你可以引用任意命令行選項,而避免設計特殊的shell特殊語句擴展。
The order of the options is generally irrelevant. For quick experiments, you can abbreviate them to their first unique characters.
選項的順序通常是無關緊要的。為了快速的執行,你可以縮寫他們,用他們的第一個特殊字符標示他們。
The sections below provide more details:
以下的單元組提供更多詳細信息:
- Input/Output Options?輸入流/輸出流選項
- Keep Options? ?keep選項
- Shrinking Options? ?縮減選項
- Optimization Options優化選項
- Obfuscation Options混淆選項
- Preverification Options預校驗選項
- General Options常規選項
- Class Pathsclass 路徑
- File Names文件名
- File Filters文件過濾器
- Filters過濾器
- Overview of?Keep?Optionskeep選項總覽
- Keep Option Modifierskeep選項修改器
- Class Specifications
Input/Output Options
@filename
Short for '-include?filename'. ? ?'-include?filename'的簡寫
-include?filename ?
Recursively reads configuration options from the given file?filename. ?
從給定的文件中遞歸的讀取配置選項。 (如:-include lib/annotations.pro)
-basedirectory?directoryname
Specifies the base directory for all subsequent relative file names in these configuration arguments or this configuration file.
指定配置參數中或配置文件中所有有關聯的文件的基礎目錄。
-injars?class_path
Specifies the input jars (or aars, wars, ears, zips, apks, or directories) of the application to be processed. The class files in these jars will be processed and written to the output jars. By default, any non-class files will be copied without changes. Please be aware of any temporary files (e.g. created by IDEs), especially if you are reading your input files straight from directories. The entries in the class path can be filtered, as explained in the?filters?section. For better readability, class path entries can be specified using multiple?-injars?options.
指定應用中需要proguard處理的jars(或 aars,wars,ears,zips,asks等文件或目錄)。這些jars中的class文件將被proguard處理并被寫入output jars中。默認情況下,任何非class文件將被原封不動的拷貝一份。請留意處理那些臨時的文件(如 IDEs工具創建的配置文件等),尤其是當你直接指定目錄的時候。這些class path入口可設置過濾器,過濾器內容可查看過濾器單元。為了便于閱讀,class path入口可以通過重復的使用-injars選項進行指定。
-outjars?class_path
Specifies the names of the output jars (or aars, wars, ears, zips, apks, or directories). The processed input of the preceding?-injars?options will be written to the named jars. This allows you to collect the contents of groups of input jars into corresponding groups of output jars. In addition, the output entries can be filtered, as explained in the?filters?section. Each processed class file or resource file is then written to the first output entry with a matching filter, within the group of output jars.
指定輸出的jars(或 aars,wars,ears,zips,asks等文件或目錄)的文件名稱。之前在-injars選項中指定的被處理后的內容將寫入outjars 指定的文件中。它允許你以組形式收集的jars內容按照組形式輸出為對應的outjars組里。此外,output通道可以設置過濾器,過濾器詳情查看過濾器單元。每一個處理的class文件或者resource文件會寫入在output jars組中與過濾器匹配的第一個output通道。
-injars base_in1.jar
-injars base_in2.jar
-injars base_in3.jar
-outjars base_out.jar
-injars? extra_in.jar
-outjars extra_out.jar
You must avoid letting the output files overwrite any input files. For better readability, class path entries can be specified using multiple?-outjars?options. Without any?-outjars?options, no jars will be written.
你需要避免讓輸出文件覆蓋了輸入文件。為了便于閱讀,class path出口可以通過重復的使用-outjars選項進行指定。如果沒有任何-outjars選項,則不會輸出任何文件。
-libraryjars?class_path
Specifies the library jars (or aars, wars, ears, zips, apks, or directories) of the application to be processed. The files in these jars will not be included in the output jars. The specified library jars should at least contain the class files that are?extended?by application class files. Library class files that are only?called?needn't be present, although their presence can improve the results of the optimization step. The entries in the class path can be filtered, as explained in the?filters?section. For better readability, class path entries can be specified using multiple?-libraryjars?options.
Please note that the boot path and the class path set for running ProGuard are not considered when looking for library classes. This means that you explicitly have to specify the run-time jar that your code will use. Although this may seem cumbersome, it allows you to process applications targeted at different run-time environments. For example, you can process?J2SE applications?as well as?JME midlets?or?Android apps, just by specifying the appropriate run-time jar.
指定應用中需要處理的library jars (or aars, wars, ears, zips, apks, or directories)。在library jars中的文件不會被寫入output jars中。指定的library jars應該至少包含應用class文件中需要繼承的class。
Library class 文件只需要被引用而不需要存在,雖然他們的存在可以提高優化的結果。class path的入口可以設置過濾器,詳見過濾器單元。為了便于閱讀,class path出口可以通過重復的使用-libraryjars選項進行指定。
請注意,proguard執行時的運行時jar包環境是未知的。這就意味著,你需要明確指定運行時jar包,你的代碼才能使用。雖然看起來比較繁瑣,但它能允許你處理針對不同的運行環境的應用。舉個栗子,你可以處理J2SE應用,JME應用和Android應用。只要指定預處理應用對應的運行時jar包就好了。
-skipnonpubliclibraryclasses
Specifies to skip non-public classes while reading library jars, to speed up processing and reduce memory usage of ProGuard. By default, ProGuard reads non-public and public library classes alike. However, non-public classes are often not relevant, if they don't affect the actual program code in the input jars. Ignoring them then speeds up ProGuard, without affecting the output. Unfortunately, some libraries, including recent JSE run-time libraries, contain non-public library classes that are extended by public library classes. You then can't use this option. ProGuard will print out warnings if it can't find classes due to this option being set.
指定在讀取library jars時跳過讀取那些non-public classes,以便于提高proguard的處理速度和減少內存消耗。默認情況下,proguard像public library classes一樣讀取 non-public classes。然而,如果他們不影響input jars里的現行代碼,non-public classes經常是無關緊要的。忽略他們,提高proguard速度,也不影響output的輸出。不幸的是,一些libraries,包括當前的 JSE run-time libraries,包含有被public library classes 繼承的non-public library classes。你就不能使用這個選項了。proguard將會打印因為這個選項的設置而引起的 it can’t find classes警告。
-dontskipnonpubliclibraryclasses
Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting.
不忽略non-public library classes.從4.5版本起,這個設置是默認的。
-dontskipnonpubliclibraryclassmembers
Specifies not to ignore package visible library class members (fields and methods). By default, ProGuard skips these class members while parsing library classes, as program classes will generally not refer to them. Sometimes however, program classes reside in the same packages as library classes, and they do refer to their package visible class members. In those cases, it can be useful to actually read the class members, in order to make sure the processed code remains consistent.
不忽略包級的library class里的成員(fields 和 methods)。默認的,當解析library classes時proguard會跳過這些類成員,因為代碼類通常不引用他們。然而有時候,代碼類與library classes處于用一個包名下,那就可以引用包級成員了。這樣,-dontskipnonpubliclibraryclassmembers 在確實讀取了這些包級成員時派上了用場,以確保處理后的代碼可用性。
-keepdirectories?[directory_filter]
Specifies the directories to be kept in the output jars (or aars, wars, ears, zips, apks, or directories). By default, directory entries are removed. This reduces the jar size, but it may break your program if the code tries to find them with constructs like "mypackage.MyClass.class.getResource("")". You'll then want to keep the directory corresponding to the package, "-keepdirectories mypackage". If the option is specified without a filter, all directories are kept. With a filter, only matching directories are kept. For instance, "-keepdirectories mydirectory" matches the specified directory, "-keepdirectories mydirectory/*" matches its immediate subdirectories, and "-keepdirectories mydirectory/**" matches all of its subdirectories.
指定目錄結構被保留在output (or aars, wars, ears, zips, apks, or directories)jars中。默認情況,目錄結構是被移除的。這樣可以減少jar的大小,但如果你使用類似于"mypackage.MyClass.class.getResource(“”)結構的代碼時,它將破壞你的程序。你若想保留對應的包名目錄結構,使用"-keepdirectories mypackage”。如果選項沒有設置過濾器,所有的目錄將被保留。使用過濾器,只用匹配的目錄將被保留。舉個栗子,"-keepdirectories mydirectory" 只匹配mydirectory目錄,"-keepdirectories mydirectory/*”匹配mydirectory的一層子目錄,"-keepdirectories mydirectory/**則匹配mydirectory下所有目錄。
-target?version
Specifies the version number to be set in the processed class files. The version number can be one of?1.0,?1.1,?1.2,?1.3,?1.4,?1.5?(or just?5),?1.6(or just?6),?1.7?(or just?7), or?1.8?(or just?8). By default, the version numbers of the class files are left unchanged. For example, you may want toupgrade class files to Java 6, by changing their version numbers and having them preverified. You probably shouldn't downgrade the version numbers of class files, since the code may contain constructs that are not supported in older versions.
指定處理后文件使用的java版本號。版本號形式可以是?1.0,?1.1,?1.2,?1.3,?1.4,?1.5?(or just?5),?1.6(or just?6),?1.7?(or just?7), or?1.8?(or just?8). 默認的,class files的版本號是不會變化的。如果你想升級class files 到java6,可以改變這個版本號,使之進行預驗證。你可能不能降低classes files的版本號,因為文件中可能會包含低版本不支持的結構。
-forceprocessing
Specifies to process the input, even if the output seems up to date. The up-to-dateness test is based on a comparison of the date stamps of the specified input, output, and configuration files or directories.
指定強行處理input數據,即使output的數據是最新的。驗證數據是否是最新的判斷是建立在指定的input,output,配置文件或目錄的日期戳的比較上。
Keep Options? Keep選項
-keep [,modifier,...] class_specification
Specifies classes and class members (fields and methods) to be preserved as entry points to your code. For example, in order to keep an application, you can specify the main class along with its main method. In order to process a library, you should specify all publicly accessible elements.
指定你代碼里的類和類成員(常變量和方法)保留原貌。舉個栗子,為了保護一個應用,你可以指定main class的main 方法被保留。而當處理一個library,你應該保留所有的公共可用的元素。
-keepclassmembers [,modifier,...] class_specification
Specifies class members to be preserved, if their classes are preserved as well. For example, you may want to keep all serialization fields and methods of classes that implement the Serializable interface.
指定類成員被保留,如果他們的類也被保護(類沒有被保護似乎也有效)。例如,你要保留所有實現Serializable接口的類的序列化成員。
-keepclassmembers class * implements java.io.Serializable {
? ? private 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();
}
-keepclasseswithmembers [,modifier,...] class_specification
Specifies classes and class members to be preserved, on the condition that all of the specified class members are present. For example, you may want to keep all applications that have a main method, without having to list them explicitly.
指定在所有指定的類成員都存在的情況下,保護類和類成員。例如你要保存所有有main方法的應用,不用一一明確的指定每一個。
-keepclasseswithmembers public class * {
? ? public static void main(java.lang.String[]);
}
-keepnames class_specification
Short for -keep,allowshrinking class_specification
Specifies classes and class members whose names are to be preserved, if they aren't removed in the shrinking phase. For example, you may want to keep all class names of classes that implement the Serializable interface, so that the processed code remains compatible with any originally serialized classes. Classes that aren't used at all can still be removed. Only applicable when obfuscating.
?-keep,allowshrinking class_specification 的縮寫
指定類和類成員名被保護,如果他們沒有被縮減解析被刪減掉。例如,你想保留所有實現了序列化接口的類名,以便處理后的代碼仍然可以兼容原來的序列化類。那些沒有使用的類依然會被刪除。此選項只有在運行了混淆選項的時候有效。
-keepclassmembernames class_specification
Short for -keepclassmembers,allowshrinking class_specification
Specifies class members whose names are to be preserved, if they aren't removed in the shrinking phase. For example, you may want to preserve the name of the synthetic class$ methods when processing a library compiled by JDK 1.2 or older, so obfuscators can detect it again when processing an application that uses the processed library (although ProGuard itself doesn't need this). Only applicable when obfuscating.
?-keepclassmembers,allowshrinking class_specification 的縮寫
指定類成員名被保護,如果他們沒有被縮減解析刪減掉。例如,你想保留用JDK1.2或者更老版本編譯的library項目里的內部類方法,當處理一個使用了這個處理的library的應用的時候混淆器能再次發現他。此選項只有在運行了混淆選項的時候有效。?
-keepclasseswithmembernames class_specification
Short for -keepclasseswithmembers,allowshrinking class_specification
Specifies classes and class members whose names are to be preserved, on the condition that all of the specified class members are present after the shrinking phase. For example, you may want to keep all native method names and the names of their classes, so that the processed code can still link with the native library code. Native methods that aren't used at all can still be removed. If a class file is used, but none of its native methods are, its name will still be obfuscated. Only applicable when obfuscating.
?-keepclasseswithmembers,allowshrinking class_specification 的縮寫
指定類和類成員被保護,前提是所有指定類成員在被縮減處理后依然存在。例如,你想保護所有的native方法名和他們的類名,使處理后的代碼仍然可以聯系到native library code.沒有被使用的native方法依然會被刪除。如果一個類有被使用,但它的native方法都沒有被使用,那么它的類名仍然會被混淆。此選項只有在運行了混淆選項的時候有效。?
-printseeds [filename]
Specifies to exhaustively list classes and class members matched by the various -keep options. The list is printed to the standard output or to the given file. The list can be useful to verify if the intended class members are really found, especially if you're using wildcards. For example, you may want to list all the applications or all the applets that you are keeping.
羅列出一個被-keep相關選項匹配的類和類成員的詳細列表。這個列表將被打印到output中或者指定的路徑中。這個列表能被用于驗證是否期望的類成員真的被保護到,尤其是當你使用通配符的時候。例如,你想列出所有應用中你想保護的。
Shrinking Options ? 縮減選項
-dontshrink
Specifies not to shrink the input class files. By default, shrinking is applied; all classes and class members are removed, except for the ones listed by the various -keep options, and the ones on which they depend, directly or indirectly. A shrinking step is also applied after each optimization step, since some optimizations may open the possibility to remove more classes and class members.
指定不縮減輸入的class文件。縮減選項是默認執行的。除了被keep保護的,以及相互間有直接或間接依賴關系的類和類成員,其他的類和類成員都會被刪除掉。縮減步驟會在每次優化后再執行一次,因為一些優化后可能存在符合刪除條件的類和類成員。
-printusage [filename]
Specifies to list dead code of the input class files. The list is printed to the standard output or to the given file. For example, you can list the unused code of an application. Only applicable when shrinking.
列出被縮減刪除的code。這個列表會被輸出到output路徑或者指定的路徑文件下。例如,你可以列出應用中沒有使用的code.只在縮減執行時才有效。
-whyareyoukeeping class_specification
Specifies to print details on why the given classes and class members are being kept in the shrinking step. This can be useful if you are wondering why some given element is present in the output. In general, there can be many different reasons. This option prints the shortest chain of methods to a specified seed or entry point, for each specified class and class member. In the current implementation, the shortest chain that is printed out may sometimes contain circular deductions -- these do not reflect the actual shrinking process. If the -verboseoption if specified, the traces include full field and method signatures. Only applicable when shrinking.
設置打印說明為什么要在縮減步驟中保留那些類和類成員。如果你想知道為什么一些元素在輸出結果中依然存在,這個選項很有用。通常情況下,可能會有很多不同的原因。這個選項為每個指定的類和類成員打印最短的方法引用鏈。
在當前的實現機制下,這個方法鏈有時候會包含有刪減的內容 - - 這些不會影響縮減的真實處理。如果指定了具體選項,蹤跡會包含指定的所有field和method簽名。只在縮減執行時才有效。
Optimization Options? 優化選項
-dontoptimize
Specifies not to optimize the input class files. By default, optimization is enabled; all methods are optimized at a bytecode level.
指定不優化輸入文件。默認情況下,優化是可用的。所有方法都會以字節碼等級進行優化。
-optimizations optimization_filter
Specifies the optimizations to be enabled and disabled, at a more fine-grained level. Only applicable when optimizing. This is an expert option.
以更細顆粒度的等級指定什么時候優化什么時候不優化。只有在優化中才有效。這是一個專業級的選項。
-optimizationpasses n
Specifies the number of optimization passes to be performed. By default, a single pass is performed. Multiple passes may result in further improvements. If no improvements are found after an optimization pass, the optimization is ended. Only applicable when optimizing.
指定執行優化的次數。默認只優化一次。多次優化會對結果進行深度提升。如果在一次優化后發現沒有可以提升的空間后,優化會停止。只有在優化的情況下此選項才有效。
-assumenosideeffects class_specification
Specifies methods that don't have any side effects (other than maybe returning a value). In the optimization step, ProGuard will then remove calls to such methods, if it can determine that the return values aren't used. ProGuard will analyze your program code to find such methods automatically. It will not analyze library code, for which this option can therefore be useful. For example, you could specify the method System.currentTimeMillis(), so that any idle calls to it will be removed. With some care, you can also use the option to remove logging code. Note that ProGuard applies the option to the entire hierarchy of the specified methods. Only applicable when optimizing. In general, making assumptions can be dangerous; you can easily break the processed code. Only use this option if you know what you're doing!
使方法失效(除非返回一個有被使用的值)。在優化中,如果確定這些方法的返回值沒有被使用,proguard將不去調用這些方法。proguard會自動分析你的代碼找到這樣的方法。在這個選項使用的時候,它不會分析library代碼,但這個選項的效果仍有用。
舉例說明,你可以指定System.currentTimeMillis(),不被調用。在某些場景中,你可以用這個選項去消除logging日志代碼。請注意,proguard允許此選項影響方法的整個層級。只在使用了優化選項時才有效。通常情況下,使用
assumptions是危險的,你會輕易的破壞代碼結構。請只有在你知道你在做什么的時候下使用。
-allowaccessmodification
Specifies that the access modifiers of classes and class members may be broadened during processing. This can improve the results of the optimization step. For instance, when inlining a public getter, it may be necessary to make the accessed field public too. Although Java's binary compatibility specifications formally do not require this (cfr. The Java Language Specification, Third Edition, Section 13.4.6), some virtual machines would have problems with the processed code otherwise. Only applicable when optimizing (and when obfuscating with the -repackageclasses option).
Counter-indication: you probably shouldn't use this option when processing code that is to be used as a library, since classes and class members that weren't designed to be public in the API may become public.
指定在proguard處理時擴展類和類成員的訪問權限。它能提升優化結果。例如,當內嵌一個public getter方法時,有必要讓相關的field也是public的。雖然Java的二進制兼容性規范沒有正式要求這么做,另外,有些虛擬機也可能在處理這樣優化過得代碼出現問題。只有在優化選項中(如果混淆了,也需要使用-repackageclasses選項)使用。
警告:你不應該在library項目代碼中使用此選項處理代碼,因為那些沒有被設計成public的類和類成員可能會變成public.
-mergeinterfacesaggressively
Specifies that interfaces may be merged, even if their implementing classes don't implement all interface methods. This can reduce the size of the output by reducing the total number of classes. Note that Java's binary compatibility specifications allow such constructs (cfr. The Java Language Specification, Third Edition, Section 13.5.3), even if they are not allowed in the Java language (cfr. The Java Language Specification, Third Edition, Section 8.1.4). Only applicable when optimizing.
Counter-indication: setting this option can reduce the performance of the processed code on some JVMs, since advanced just-in-time compilation tends to favor more interfaces with fewer implementing classes. Worse, some JVMs may not be able to handle the resulting code. Notably:
- Sun's JRE 1.3 may throw an InternalError when encountering more than 256 Miranda methods (interface methods without implementations) in a class.
合并所有interface接口,即使他們的實現類沒有實現所有的接口方法。它能減少output里類的數量。需要指出的是,Java的二進制兼容性規范是允許這樣的結構的,雖然java語言不允許這樣的結構。只有在優化選項使用時有效。
警告:使用了此選項后的code在一些jvm中執行表現會降低。因為高級的運行時編譯機制傾向于更多的接口,更少的實現類。更糟糕的時,一些jvms可能不能處理這種代碼。特別是:
Sun's JRE 1.3將拋出一個InternalError,當遇到一個類有超過256個 Miranda methods (沒有實現的接口方法)
Obfuscation Options
-dontobfuscate
Specifies not to obfuscate the input class files. By default, obfuscation is applied; classes and class members receive new short random names, except for the ones listed by the various -keep options. Internal attributes that are useful for debugging, such as source files names, variable names, and line numbers are removed.
指定不混淆輸入文件。默認混淆是使用的,類和類成員獲得一個新的短的隨機名稱,除了那些被-keep相關選項保護的。用于調試的內部屬性,如資源文件名,變量名,行號都被刪除。
-printmapping [filename]
Specifies to print the mapping from old names to new names for classes and class members that have been renamed. The mapping is printed to the standard output or to the given file. For example, it is required for subsequent incremental obfuscation, or if you ever want to make sense again of obfuscated stack traces. Only applicable when obfuscating.
打印類和類成員新舊名稱的對應表。對應表被打印到output路徑或者指定的路徑。例如,后續增量混淆需要它,或者你想查看混淆后的日志。只有在混淆執行時有效。
-applymapping filename
Specifies to reuse the given name mapping that was printed out in a previous obfuscation run of ProGuard. Classes and class members that are listed in the mapping file receive the names specified along with them. Classes and class members that are not mentioned receive new names. The mapping may refer to input classes as well as library classes. This option can be useful for incremental obfuscation, i.e. processing add-ons or small patches to an existing piece of code. If the structure of the code changes fundamentally, ProGuard may print out warnings that applying a mapping is causing conflicts. You may be able to reduce this risk by specifying the option -useuniqueclassmembernames in both obfuscation runs. Only a single mapping file is allowed. Only applicable when obfuscating.
復用在以前混淆打印出的對應表。對應表包含的類和類成員名稱不變。沒有包含的獲取新名稱。對應表涉及input和libaray的類。這個選項對增量混淆很有用。例如,增量更新和補丁更新。如果代碼的結構發生根本性的變化,proguard會發出使用對應表引起沖突的警告。你可一個通過在每次混淆時指定 -useuniqueclassmembernames 選項來降低這個風險。只能有一個對應表可以使用,只能在混淆時有效。
-obfuscationdictionary filename
Specifies a text file from which all valid words are used as obfuscated field and method names. By default, short names like 'a', 'b', etc. are used as obfuscated names. With an obfuscation dictionary, you can specify a list of reserved key words, or identifiers with foreign characters, for instance. White space, punctuation characters, duplicate words, and comments after a # sign are ignored. Note that an obfuscation dictionary hardly improves the obfuscation. Decent compilers can automatically replace them, and the effect can fairly simply be undone by obfuscating again with simpler names. The most useful application is specifying strings that are typically already present in class files (such as 'Code'), thus reducing the class file sizes just a little bit more. Only applicable when obfuscating.
指定一個文件設計有效的混淆名供類field和method使用。默認的的混淆名諸如a ,b 之類的。通過這個混淆字典,你可以指定保留關鍵字,或者外國標示符作為命名規則。空格,標點符號,重復的文字和#后面的注釋是被忽略的。請注意,混淆字典很難提升混淆效果。像樣點得編譯器可以自動替換他們,而其成效又可以通過用更簡單的命名方式替代。最有用的應用場景是指定存在于class的代表性的字符串(如‘Code’),因此混淆后的class文件大小會比默認混淆規則大一點。只在混淆時有效。
-classobfuscationdictionary filename
Specifies a text file from which all valid words are used as obfuscated class names. The obfuscation dictionary is similar to the one of the option -obfuscationdictionary. Only applicable when obfuscating.
指定混淆類名的命名規則。類似于-obfuscationdictionary.只在混淆時有效。
-packageobfuscationdictionary filename
Specifies a text file from which all valid words are used as obfuscated package names. The obfuscation dictionary is similar to the one of the option -obfuscationdictionary. Only applicable when obfuscating.
指定混淆包名的命名規則。類似于-obfuscationdictionary.只在混淆時有效。
-overloadaggressively
Specifies to apply aggressive overloading while obfuscating. Multiple fields and methods can then get the same names, as long as their arguments and return types are different, as required by Java bytecode (not just their arguments, as required by the Java language). This option can make the processed code even smaller (and less comprehensible). Only applicable when obfuscating.
Counter-indication: the resulting class files fall within the Java bytecode specification (cfr. The Java Virtual Machine Specification, Second Edition, first paragraphs of Section 4.5 and Section 4.6), even though this kind of overloading is not allowed in the Java language (cfr. The Java Language Specification, Third Edition, Section 8.3 and Section 8.4.5). Still, some tools have problems with it. Notably:
- Sun's JDK 1.2.2 javac compiler produces an exception when compiling with such a library (cfr. Bug #4216736). You probably shouldn't use this option for processing libraries.
- Sun's JRE 1.4 and later fail to serialize objects with overloaded primitive fields.
- Sun's JRE 1.5 pack200 tool reportedly has problems with overloaded class members.
- The class java.lang.reflect.Proxy can't handle overloaded methods.
- Google's Dalvik VM can't handle overloaded static fields.
指定混淆時采用野蠻式的重載。很多field和methods可以有相同的名稱,只要他們的入參和返回類型不同,只要符合java字節碼(不只是他們的參數,只要符合java語言要求)。此選項可以讓處理后的代碼更加的小(更加的難以理解)。
只在混淆時有效。
警告:即使這種重載不符合java語言,但它符合java字節碼規范。當然,一些工具處理它時還是會遇到問題。典型的如下:
Sun's JDK 1.2.2 javac 編譯器當編譯這樣一個library的時候會產生異常。你應該不能用這個選項去處理library。
Sun's JRE 1.4 及以后的版本序列化重載的原始fields時會失敗。
Sun's JRE 1.5 pack200 tool據說也會有重載的成員的相關問題。
java.lang.reflect.Proxy類不能處理重載的方法。
Google's Dalvik VM不能處理重載靜態fields.
-useuniqueclassmembernames
Specifies to assign the same obfuscated names to class members that have the same names, and different obfuscated names to class members that have different names (for each given class member signature). Without the option, more class members can be mapped to the same short names like 'a', 'b', etc. The option therefore increases the size of the resulting code slightly, but it ensures that the saved obfuscation name mapping can always be respected in subsequent incremental obfuscation steps.
For instance, consider two distinct interfaces containing methods with the same name and signature. Without this option, these methods may get different obfuscated names in a first obfuscation step. If a patch is then added containing a class that implements both interfaces, ProGuard will have to enforce the same method name for both methods in an incremental obfuscation step. The original obfuscated code is changed, in order to keep the resulting code consistent. With this option in the initial obfuscation step, such renaming will never be necessary.
This option is only applicable when obfuscating. In fact, if you are planning on performing incremental obfuscation, you probably want to avoid shrinking and optimization altogether, since these steps could remove or modify parts of your code that are essential for later additions.
指定有相同名稱的類成員使用相同的混淆名,不同名稱的類成員用不同的混淆名稱(針對每一個類成員泛型)。不用這個選項,更多的類成員會被混淆成類似于a,b這樣的名字。這個選項會稍微的增加結果的大小,但它能確保這個混淆后的名字能給以后的增量混淆帶來好處。
例如,有兩個不同的接口包含有同樣的方法名和泛型的方法。沒有這個選項的時候,這個方法會在第一次混淆的時候擁有不同的方法名。如果一個后續添加的補丁中包含一個實現了這兩個接口的類,proguard不得不在增量混淆中強制把這兩個方法名在改為一個同樣的名字。為了保持結果代碼的功能完整性,原來混淆的代碼需要做出改變。但在初始的混淆中有了這個選項,這種再改名的現象就不需要了。
這個選項只有在混淆時才有效。事實上,如果你計劃執行增量更新,你應當避免縮減和優化,因為這些步驟可能會刪除或者改變為以后增量代碼預留的代碼。
-dontusemixedcaseclassnames
Specifies not to generate mixed-case class names while obfuscating. By default, obfuscated class names can contain a mix of upper-case characters and lower-case characters. This creates perfectly acceptable and usable jars. Only if a jar is unpacked on a platform with a case-insensitive filing system (say, Windows), the unpacking tool may let similarly named class files overwrite each other. Code that self-destructs when it's unpacked! Developers who really want to unpack their jars on Windows can use this option to switch off this behavior. Obfuscated jars will become slightly larger as a result. Only applicable when obfuscating.
指定混淆時不生產混合方法名。默認的,混淆的方法名包含大小寫的混合形式。它生成出完美運行的jars。但當這個jar在不區分大小寫的系統(如windows)解壓的時候,解壓工具可能會覆蓋相似文件名的文件。這樣代碼在解壓的時候就損壞了。如果開發者想要在windows下解壓jars,他們就可以使用這個選項去關閉混合命名行為。混淆的代碼會稍微大一點。只有在混淆時有效。
-keeppackagenames [package_filter]
Specifies not to obfuscate the given package names. The optional filter is a comma-separated list of package names. Package names can contain ?, *, and ** wildcards, and they can be preceded by the ! negator. Only applicable when obfuscating.
不混淆指定規則的包名。過濾器選項是以逗號分隔額包名列表。包名可包含?, *, 和 **通配符,也可以在前面包含!作為非的含義。只有在混淆時有效。
-flattenpackagehierarchy [package_name]
Specifies to repackage all packages that are renamed, by moving them into the single given parent package. Without argument or with an empty string (''), the packages are moved into the root package. This option is one example of further obfuscating package names. It can make the processed code smaller and less comprehensible. Only applicable when obfuscating.
將所有重命名的包名重新包裹,把他們都移動到一個單獨的父包目錄下。如果不寫入參[package_name]或者寫為空字符串’’,所有包則都會被移動到根包下。這個選項使用的一個場景是深度混淆包名。它能讓處理后的代碼更小,更難讀懂。只有在混淆時才有效。
-repackageclasses [package_name]
Specifies to repackage all class files that are renamed, by moving them into the single given package. Without argument or with an empty string (''), the package is removed completely. This option overrides the -flattenpackagehierarchy option. It is another example of furtherobfuscating package names. It can make the processed code even smaller and less comprehensible. Its deprecated name is -defaultpackage. Only applicable when obfuscating.
Counter-indication: classes that look for resource files in their package directories will no longer work properly if they are moved elsewhere. When in doubt, just leave the packaging untouched by not using this option.
將所有混淆的類文件重新打包到一個給定的單獨的包下。如果不寫入參[package_name]或者寫為空字符串’’,則將包路徑完全去除。它能讓處理后的代碼更小,更難讀懂。它原來的名字是-defaultpackage.只在混淆時有效。
警告:那些需要在他們包路徑中尋找資源文件的移至別處的類將不能工作。當你不確定時,還是不要使用這個選項好了。
-keepattributes [attribute_filter]
Specifies any optional attributes to be preserved. The attributes can be specified with one or more -keepattributes directives. The optional filter is a comma-separated list of attribute names that Java virtual machines and ProGuard support. Attribute names can contain ?, *, and** wildcards, and they can be preceded by the ! negator. For example, you should at least keep the Exceptions, InnerClasses, and Signatureattributes when processing a library. You should also keep the SourceFile and LineNumberTable attributes for producing useful obfuscated stack traces. Finally, you may want to keep annotations if your code depends on them. Only applicable when obfuscating.
保留指定的屬性。屬性可以用多個-keepattributes 來指定。添加項可以是java虛擬機和proguard支持的以逗號分隔的屬性名稱列表。屬性名稱可包含
??, *, 和**這樣的通配符,也可以在前面使用 非符號!例如,當你處理一個library項目的時候,你至少要保留Exceptions, InnerClasses, 和 Signature屬性(異常,內部類,泛型)。你也應當保留SourceFile和 LineNumberTable屬性(源文件名,行號)以生成有用的混淆日志。最后,你可能想保留annotations (注解),如果你的代碼依賴于它。只有在混淆時有效。
-keepparameternames
Specifies to keep the parameter names and types of methods that are kept. This option actually keeps trimmed versions of the debugging attributes LocalVariableTable and LocalVariableTypeTable. It can be useful when processing a library. Some IDEs can use the information to assist developers who use the library, for example with tool tips or autocompletion. Only applicable when obfuscating.
保留方法的參數和類型。這個選項實際上是debugging屬性LocalVariableTable 和 LocalVariableTypeTable的平衡使用。當處理library時,它很有用。一些IDEs能使用這些信息幫助那些使用了這個library的開發者們,例如提示或者自動補全工具。只有在混淆時有效。
-renamesourcefileattribute [string]
Specifies a constant string to be put in the SourceFile attributes (and SourceDir attributes) of the class files. Note that the attribute has to be present to start with, so it also has to be preserved explicitly using the -keepattributes directive. For example, you may want to have your processed libraries and applications produce useful obfuscated stack traces. Only applicable when obfuscating.
指定一個字符串常量替代SourceFile的內容(也可以是SourceDir)。請注意,指定的屬性得使被-keepattributes 指定的。
你想讓處理的library或者應用生成有用可讀性高的混淆日志時,可以使用此配置項。只在混淆時有效。
-adaptclassstrings [class_filter]
Specifies that string constants that correspond to class names should be obfuscated as well. Without a filter, all string constants that correspond to class names are adapted. With a filter, only string constants in classes that match the filter are adapted. For example, if your code contains a large number of hard-coded strings that refer to classes, and you prefer not to keep their names, you may want to use this option. Primarily applicable when obfuscating, although corresponding classes are automatically kept in the shrinking step too.
指定 混淆那些與類名對應的字符串常量。如果沒有設置過濾器,所有與類名對應的字符串常量都要混淆。如果有過濾器,只有匹配過濾器的常量會混淆。例如,假設你得代碼中包含有大量的對應類的硬編碼,你又不想保留他們的名字,你可以使用這個選項。需要提醒的是,混淆時優先考慮可用性,盡管有關聯的類在縮減階段也會被保護。
-adaptresourcefilenames [file_filter]
Specifies the resource files to be renamed, based on the obfuscated names of the corresponding class files (if any). Without a filter, all resource files that correspond to class files are renamed. With a filter, only matching files are renamed. For example, see processing resource files. Only applicable when obfuscating.
基于相關聯的類文件(或者其他什么)的混淆名,混淆資源文件名。如果沒有設置過濾器,所有資源文件名都要混淆。如果有過濾器,只有匹配過濾器的會混淆。可以參考processing resource files處理資源文件的例子。只有在混淆時才有效
-adaptresourcefilecontents [file_filter]
Specifies the resource files whose contents are to be updated. Any class names mentioned in the resource files are renamed, based on the obfuscated names of the corresponding classes (if any). Without a filter, the contents of all resource files updated. With a filter, only matching files are updated. The resource files are parsed and written using the platform's default character set. You can change this default character set by setting the environment variable LANG or the Java system property file.encoding. For an example, see processing resource files. Only applicable when obfuscating.
Caveat: You probably only want to apply this option to text files, since parsing and adapting binary files as text files can cause unexpected problems. Therefore, make sure that you specify a sufficiently narrow filter.
指定資源文件里的內容進行混淆。基于相關聯的類文件(或者其他什么)的混淆名,任何與資源文件內容有關的類里的名稱都會被重命名。沒有過濾器,所有的資源文件都會被混淆。有了過濾器,只有匹配的才會處理。資源文件內容會按照平臺的默認編碼格式進行書寫。你可以通過設置環境變量LANG或者java系統配置文件里的 file.encoding屬性進行自定義。例子參考 processing resource files.只有混淆時才有效。
警告:你應當只讓此選項適用于text文件,因為接卸和改編二進制文件為text文件會引起意想不到的問題。因此,確保你指定一個充分窄范圍的過濾器。
Preverification Options? 預校驗選項
-dontpreverify
Specifies not to preverify the processed class files. By default, class files are preverified if they are targeted at Java Micro Edition or at Java 6 or higher. For Java Micro Edition, preverification is required, so you will need to run an external preverifier on the processed code if you specify this option. For Java 6, preverification is optional, but as of Java 7, it is required. Only when eventually targeting Android, it is not necessary, so you can then switch it off to reduce the processing time a bit.
不校驗處理后的類文件。默認的,類文件是會被校驗的,只要他們的版本是Java Micro Edition或java6以上版本。Java Micro Edition版本的代碼是必須要校驗的,所以如果你使用了這個選項,你也要運行一個額外的校驗過程。java6校驗是可選項,但是java7是必選項。只有在android下運行的代碼,是不需要校驗的,所有你可以關閉這個校驗過程來減少一點點處理的時間。
-microedition
Specifies that the processed class files are targeted at Java Micro Edition. The preverifier will then add the appropriate StackMap attributes, which are different from the default StackMapTable attributes for Java Standard Edition. For example, you will need this option if you areprocessing midlets.
指定處理的類文件以Java Micro Edition運行。校驗器會添加適當的StackMap屬性,因為和Java Standard Edition默認的StackMapTable不一樣。
當你處理midlets項目的時候,你需要用到這個選項。
General Options 常規選項
-verbose
Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
輸出處理期間更多的信息。如果處理中因為一個異常中斷了,這個選項就會輸出詳細的日志,而不僅僅是異常信息。
-dontnote [class_filter]
Specifies not to print notes about potential mistakes or omissions in the configuration, such as typos in class names or missing options that might be useful. The optional filter is a regular expression; ProGuard doesn't print notes about classes with matching names.
不打印潛在的配置文件的錯誤或者遺漏,如類名的錯別字,或者有用的選項。可選的過濾器是規則表達式;proguard不會打印符合過濾器的相關類信息。
-dontwarn [class_filter]
Specifies not to warn about unresolved references and other important problems at all. The optional filter is a regular expression; ProGuard doesn't print warnings about classes with matching names. Ignoring warnings can be dangerous. For instance, if the unresolved classes or class members are indeed required for processing, the processed code will not function properly. Only use this option if you know what you're doing!
不提示無法判斷引用和其他重要的錯誤問題。可選的過濾器是規則表達式;proguard不會打印符合過濾器的相關類信息。忽略警示是危險的。
例如,如果無法判斷的類和類成員確實是運行時需要的內容,則處理后的代碼可能會無效。只有在你知道你做什么的時候使用。
-ignorewarnings
Specifies to print any warnings about unresolved references and other important problems, but to continue processing in any case. Ignoring warnings can be dangerous. For instance, if the unresolved classes or class members are indeed required for processing, the processed code will not function properly. Only use this option if you know what you're doing!
提示任何無法判斷引用和其他重要的錯誤問題,但卻繼續執行處理流程而不中斷。忽略警示是危險的。例如,如果無法判斷的類和類成員確實是運行時需要的內容,則處理后的代碼可能會無效。只有在你知道你做什么的時候使用。
-printconfiguration [filename]
Specifies to write out the entire configuration that has been parsed, with included files and replaced variables. The structure is printed to the standard output or to the given file. This can sometimes be useful for debugging configurations, or for converting XML configurations into a more readable format.
打印出build解析后執行的完整的proguard配置信息,包括包含的文件和替換的變量。這個結構會被寫入output中或者指定的文件。在debugging配置或者轉換XML為更易讀的結構是會有用。
-dump [filename]
Specifies to write out the internal structure of the class files, after any processing. The structure is printed to the standard output or to the given file. For example, you may want to write out the contents of a given jar file, without processing it at all.
輸出intput的原來包含的類的內部結構。這個結構會被寫入output中或者指定的文件。
例如,你想輸出給定的一個jar文件結構,在沒有被處理的時候。 write out the contents of a given jar file
Class Paths
ProGuard accepts a generalization of class paths to specify input files and output files. A class path consists of entries, separated by the traditional path separator (e.g. ':' on Unix, or ';' on Windows platforms). The order of the entries determines their priorities, in case of duplicates.
Each input entry can be:
- A class file or resource file,
- An apk file, containing any of the above,
- A jar file, containing any of the above,
- An aar file, containing any of the above,
- A war file, containing any of the above,
- An ear file, containing any of the above,
- A zip file, containing any of the above,
- A directory (structure), containing any of the above.
Proguard接受廣泛化的類路徑來指定輸入和輸出文件。類路徑可由多個條目組成,以傳統的分隔符分隔(例如,Unix上用:? Windows上用;)。有多數的情況下,詞條的順序決定了他們的優先級。
路徑文件可以是:
類文件或者資源文件,
apk文件,包含以上
jar文件,包含以上
aar文件,包含以上
war文件,包含以上
ear文件,包含以上
zip文件,包含以上
目錄結構,包含以上
The paths of directly specified class files and resource files is ignored, so class files should generally be part of a jar file, an aar file, a war file, an ear file, a zip file, or a directory. In addition, the paths of class files should not have any additional directory prefixes inside the archives or directories.
直接在路徑中指定的類文件和資源文件是被忽略處理的,所以類文件應當是jar,aar,war,ear,zip或者目錄里的一部分。另外,類文件的路徑應該沒有任何附加的檔案或目錄前綴(不明白- -|)。
Each output entry can be:
- An apk file, in which all class files and resource files will be collected.
- A jar file, in which any and all of the above will be collected,
- An aar file, in which any and all of the above will be collected,
- A war file, in which any and all of the above will be collected,
- An ear file, in which any and all of the above will be collected,
- A zip file, in which any and all of the above will be collected,
- A directory, in which any and all of the above will be collected.
output的對象可以是:
apk文件,包含類文件和資源文件。
jar文件,包含以上。
aar文件,包含以上。
war文件,包含以上。
ear文件,包含以上。
zip文件,包含以上。
目錄,包含以上。
When writing output entries, ProGuard will generally package the results in a sensible way, reconstructing the input entries as much as required. Writing everything to an output directory is the most straightforward option: the output directory will contain a complete reconstruction of the input entries. The packaging can be almost arbitrarily complex though: you could process an entire application, packaged in a zip file along with its documentation, writing it out as a zip file again. The Examples section shows a few ways to restructure output archives.
當輸出output的時候,proguard通常會以簡單明了的方式重新打包結果,盡量按照input的時候重新打包。把所有東西輸出到一個目錄是最直接的方法:output目錄將包含一個完成的input重構結構。打包可以隨意的復雜化:你可以處理一個完整的應用,打包成zip和文檔放在一起,然后再一起打包成zip輸出。
例子單元有重構output檔案的多個方法。restructure output archives
-injars base_in1.jar
-injars base_in2.jar
-injars base_in3.jar
-outjars base_out.jar
-injars? extra_in.jar
-outjars extra_out.jar
Files and directories can be specified as discussed in the section on file names below.
文件和目錄可以按照下面文件名單元規則進行指定。
In addition, ProGuard provides the possibility to filter the class path entries and their contents, based on their full relative file names. Each class path entry can be followed by up to 7 types of file filters between parentheses, separated by semi-colons:
- A filter for all aar names that are encountered,
- A filter for all apk names that are encountered,
- A filter for all zip names that are encountered,
- A filter for all ear names that are encountered,
- A filter for all war names that are encountered,
- A filter for all jar names that are encountered,
- A filter for all class file names and resource file names that are encountered.
此外,proguard提供基于全路徑的內容過濾。路徑可以包含以下7種類型的文件設置過濾選項:
aar,apk,zip,ear,war,jar,類名和資源名
If fewer than 7 filters are specified, they are assumed to be the latter filters. Any empty filters are ignored. More formally, a filtered class path entry looks like this:
如果少于7中類型被設定,那就假定只針對于后面的過濾類型。空的過濾器是被忽略的。更正式點,一個路徑過濾器應該如下這樣。
classpathentry([[[[[[aarfilter;]apkfilter;]zipfilter;]earfilter;]warfilter;]jarfilter;]filefilter)
Square brackets "[]" mean that their contents are optional.
[]的區域標示里面的內容是可選項
For example, "rt.jar(java/**.class,javax/**.class)" matches all class files in the java and javax directories inside the rt jar.
例如,"rt.jar(java/**.class,javax/**.class)" 表示匹配rtjar中在java和javax目錄下的類文件。
For example, "input.jar(!**.gif,images/**)" matches all files in the images directory inside the input jar, except gif files.
"input.jar(!**.gif,images/**)”表示匹配images目錄下的文件,不包括gif文件。
The different filters are applied to all corresponding file types, irrespective of their nesting levels in the input; they are orthogonal.
不同的過濾器可以對應所有的文件類型,與input的嵌套等級無關(即不管input是否包含過濾類型);他們是可相互交叉的。
For example, "input.war(lib/**.jar,support/**.jar;**.class,**.gif)" only considers jar files in the lib and support directories in the input war, not any other jar files. It then matches all class files and gif files that are encountered.
?"input.war(lib/**.jar,support/**.jar;**.class,**.gif)表示,只考慮lib,support文件夾下的jar。結果是,這個過濾器會會匹配遇到的所有類文件和gif。
The filters allow for an almost infinite number of packaging and repackaging possibilities. The Examples section provides a few more examples forfiltering input and output.
過濾器允許包含無限數量的包層級和重新打包的層級。示例單元提供了一些input和output的例子。
File Names
ProGuard accepts absolute paths and relative paths for the various file names and directory names. A relative path is interpreted as follows:
- relative to the base directory, if set, or otherwise
- relative to the configuration file in which it is specified, if any, or otherwise
- relative to the working directory.
proguard允許文件名和目錄名采用絕對路徑和相對路徑。相對路徑會按如下方式進行解讀:
與基本目錄相關聯,如果符合則確定,否則繼續尋找
與指定的配置文件關聯,如果符合則確定,否則繼續尋找
與工作目錄關聯
The names can contain Java system properties (or Ant properties, when using Ant), delimited by angular brackets, '<' and '>'. The properties are automatically replaced by their corresponding values.
路徑名可以包含java環境變量(或者ANT變量),以<>表示。環境變量會自動轉換為相應的值。
For example, <java.home>/lib/rt.jar is automatically expanded to something like /usr/local/java/jdk/jre/lib/rt.jar. Similarly, <user.home> is expanded to the user's home directory, and <user.dir> is expanded to the current working directory.
例如,<java.home>/lib/rt.jar 會自動擴展為類似/usr/local/java/jdk/jre/lib/rt.jar. 同樣的,<user.home>擴展為用戶home目錄。 <user.dir> 擴展為當前工作目錄。
Names with special characters like spaces and parentheses must be quoted with single or double quotes. Each file name in a list of names has to be quoted individually. Note that the quotes themselves may need to be escaped when used on the command line, to avoid them being gobbled by the shell.
名稱中包含有類似空格,括號的特殊字符得用單引號或者雙引號表示。名稱列表的每一個名稱都得用引號區分出來。請注意,引號本身應當避免在命令行中使用,以免引號被shell吞噬。
For example, on the command line, you could use an option like '-injars "my program.jar":"/your directory/your program.jar"'.
例如,你可以在命令行上這樣使用一個選項’-injars "my program.jar":"/your directory/your program.jar"'.
File Filters
Like general filters, a file filter is a comma-separated list of file names that can contain wildcards. Only files with matching file names are read (in the case of input jars), or written (in the case of output jars). The following wildcards are supported:
| ? | matches any single character in a file name. |
| * | matches any part of a filename not containing the directory separator. |
| ** | matches any part of a filename, possibly containing any number of directory separators.
|
與常用的過濾器一樣,文件過濾器也是包含通配符的以逗號分隔的文件列表。只有匹配的文件才會被讀取(與input jar有關),寫入(與output有關)。一些是支持的通配符:
?表示匹配任何單字符
*表示匹配文件名的任何部分,不包含目錄
**匹配文件名的任何部分,即使名字中間包含任何多的目錄
For example, "java/**.class,javax/**.class" matches all class files in the java and javax.
例如,"java/**.class,javax/**.class" 包含java,javax文件夾及子文件夾下的所有類文件。
Furthermore, a file name can be preceded by an exclamation mark '!' to exclude the file name from further attempts to match with subsequentfile names.
此外,文件名前可以使用!來排除后面的文件。
For example, "!**.gif,images/**" matches all files in the images directory, except gif files.
例如,"!**.gif,images/**" 匹配所有image目錄下的文件,除了gif文件
The Examples section provides a few more examples for filtering input and output.
?filtering input and output? 單元中有更多例子。
Filters
ProGuard offers options with filters for many different aspects of the configuration: names of files, directories, classes, packages, attributes, optimizations, etc.
A filter is a list of comma-separated names that can contain wildcards. Only names that match an item on the list pass the filter. The supported wildcards depend on the type of names for which the filter is being used, but the following wildcards are typical:
| ? | matches any single character in a name. |
| * | matches any part of a name not containing the package separator or directory separator. |
| ** | matches any part of a name, possibly containing any number of package separators or directory separators. |
Proguard為配置文件提供多方位的過濾器:文件名,目錄,類,包,屬性,優化等等。
過濾器是包含通配符的以逗號分隔的名稱列表。只有匹配的名字才能通過過濾。支持的通配符依據過濾器使用的名稱類型,但如下的通配符是通用的:
?匹配任意單個字符
*匹配包含在包和目錄分隔符之間的任何部分
**匹配包含任何包,目錄的任何內容
For example, "foo,*bar" matches the name foo and all names ending with bar.
例如,"foo,*bar”匹配叫foo的和以bar結尾的名字
Furthermore, a name can be preceded by a negating exclamation mark '!' to exclude the name from further attempts to match with subsequentnames. So, if a name matches an item in the filter, it is accepted or rejected right away, depending on whether the item has a negator. If the name doesn't match the item, it is tested against the next item, and so on. It if doesn't match any items, it is accepted or rejected, depending on the whether the last item has a negator or not.
此外,文件名前可以使用!來排除后面的文件。因此一個名字匹配后是接受還是拒絕,基于它是否有否決器。如果一個名字不匹配一個規則,它會嘗試下一個,然后一直下去。如果它不匹配任何一個,它接受與否決定與最后是否有否決器。
For example, "!foobar,*bar" matches all names ending with bar, except foobar.
例如, "!foobar,*bar" 匹配所有以bar結尾的名字,但是不包含foobar.
Overview of Keep Options?
The various -keep options for shrinking and obfuscation may seem a bit confusing at first, but there's actually a pattern behind them. The following table summarizes how they are related:
| Keep | From being removed or renamed | From being renamed |
| Classes and class members | -keep | -keepnames |
| Class members only | -keepclassmembers | -keepclassmembernames |
| Classes and class members, if class members present | -keepclasseswithmembers | -keepclasseswithmembernames |
為縮減和混淆提供的-keep相關選項乍一看有些混亂,但他們背后是有模式的。下圖就展示了他們的關系。
Each of these -keep options is of course followed by a specification of the classes and class members (fields and methods) to which it should be applied.
每一個-keep選項都可以跟隨一個類和類成員的限定規則。
If you're not sure which option you need, you should probably simply use -keep. It will make sure the specified classes and class members are not removed in the shrinking step, and not renamed in the obfuscation step.
- If you specify a class, without class members, ProGuard only preserves the class and its parameterless constructor as entry points. It may still remove, optimize, or obfuscate its other class members.
- If you specify a method, ProGuard only preserves the method as an entry point. Its code may still be optimized and adapted.
如果你不確定使用哪個,那就直接使用-keep選項。它將保證不會在縮減中刪除你指定的類和類成員,也不會改變他們的名字。
如果你只指定了類,沒有指定類成員。proguard只會保留類名和默認的無參構造器。它其他的類成員仍然會經歷刪除,優化,混淆的過程。
如果你只指定方法名,proguard只會保護方法名,它里面的代碼仍然會優化和改編。
Class Specifications
A class specification is a template of classes and class members (fields and methods). It is used in the various -keep options and in the -assumenosideeffects option. The corresponding option is only applied to classes and class members that match the template.
The template was designed to look very Java-like, with some extensions for wildcards. To get a feel for the syntax, you should probably look at theexamples, but this is an attempt at a complete formal definition:
類規則是類和類成員限定的模板。它被運用于-keep和-assumenosideeffects屬性中。相應的選項只會影響匹配模板的類和類成員。
模板被設計的近似于java的樣子,外加一些擴展的通配符。為了對這個語法有個初步的認知,你應該去看看那些例子。下面是完整正式的模板定義:
[@annotationtype] [[!]public|final|abstract|@ ...] [!]interface|class|enum classname
? ? [extends|implements [@annotationtype] classname]
[{
? ? [@annotationtype] [[!]public|private|protected|static|volatile|transient ...] <fields> |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (fieldtype fieldname);
? ? [@annotationtype] [[!]public|private|protected|static|synchronized|native|abstract|strictfp ...] <methods> |
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <init>(argumenttype,...) |
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? classname(argumenttype,...) |
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (returntype methodname(argumenttype,...));
? ? [@annotationtype] [[!]public|private|protected|static ... ] *;
? ? ...
}]
Square brackets "[]" mean that their contents are optional. Ellipsis dots "..." mean that any number of the preceding items may be specified. A vertical bar "|" delimits two alternatives. Non-bold parentheses "()" just group parts of the specification that belong together. The indentation tries to clarify the intended meaning, but white-space is irrelevant in actual configuration files.
[]里的內容標示為可選項。…意味著任何數量的內容可能會被在前面指定。|代表可選擇。()則表示里面的內容是一個整體。縮進是為了便于你的觀察,但空格本身是沒有作用的。
- The class keyword refers to any interface or class. The interface keyword restricts matches to interface classes. The enum keyword restricts matches to enumeration classes. Preceding the interface or enum keywords by a ! restricts matches to classes that are not interfaces or enumerations, respectively.
class關鍵字可影響interface和class。interface只約束interface類。enum關鍵字則約束枚舉類。在interface和enum前加!則約束不是接口和枚舉的類。
- Every classname must be fully qualified, e.g. java.lang.String. Inner classes are separated by a dollar sign "$", e.g. java.lang.Thread$State. Class names may be specified as regular expressions containing the following wildcards:
類名需要使用全路徑表示。例如,java.lang.String 內部類以$分隔,例如java.lang.Thread$State.類名匹配可以使用如下通配符:
- ?
- matches any single character in a class name, but not the package separator. For example, "mypackage.Test?" matches "mypackage.Test1" and "mypackage.Test2", but not "mypackage.Test12".
- *
- matches any part of a class name not containing the package separator. For example, "mypackage.*Test*" matches "mypackage.Test" and "mypackage.YourTestApplication", but not "mypackage.mysubpackage.MyTest". Or, more generally, "mypackage.*" matches all classes in "mypackage", but not in its subpackages.
- **
- matches any part of a class name, possibly containing any number of package separators. For example, "**.Test" matches all Testclasses in all packages except the root package. Or, "mypackage.**" matches all classes in "mypackage" and in its subpackages.
?匹配類名中單個任意字符,但不包含包分隔符。例如, "mypackage.Test?" 匹配 "mypackage.Test1" 和 "mypackage.Test2", 但不匹配 "mypackage.Test12".
*匹配類名中任意部分,不包含包分隔符。例如, "mypackage.*Test*" 匹配 "mypackage.Test" 和 "mypackage.YourTestApplication", 但不匹配”mypackage.mysubpackage.MyTest". 更常見的, "mypackage.*" 匹配在 "mypackage", 包下所有類,但不包含子包。
**匹配類名中任意部分,包含包分隔符。"**.Test" 匹配所有 除了根目錄下的所有目錄中包含的Test類 . "mypackage.**" 匹配”mypackage" 包及其子包種的所有類。
For additional flexibility, class names can actually be comma-separated lists of class names, with optional ! negators, just like file name filters.This notation doesn't look very Java-like, so it should be used with moderation.
為了添加方便,類名列表可以以逗號分開,也可以添加!否決器,就如同文件名過濾器一樣。這個規則與java不是非常相似,所以請適度使用。
For convenience and for backward compatibility, the class name * refers to any class, irrespective of its package.
為了便捷和向后兼容,*可以表示任何類名,與它的包名無關。
The extends and implements specifications are typically used to restrict classes with wildcards. They are currently equivalent, specifying that only classes extending or implementing the given class qualify. Note that the given class itself is not included in this set. If required, it should be specified in a separate option.
extends 和implements關鍵字常與通配符關聯使用。他們作用相等,指定只有那些extending或者implementing給定類的類才能通過。請注意,給定的類(被繼承或實現的類)是不會包含在這個限定規則里的。若想把給定的類也指定,你得單獨寫個規則限定他。
The @ specifications can be used to restrict classes and class members to the ones that are annotated with the specified annotation types. An annotationtype is specified just like a classname.
@標示符可以用于限定那些使用指定注解的類和類成員。注解的指定和類名一樣(即需要全路徑)
Fields and methods are specified much like in Java, except that method argument lists don't contain argument names (just like in other tools like javadoc and javap). The specifications can also contain the following catch-all wildcards:
field和method的設定類似于java.除了那些沒有入參名的方法(類似javadoc的注釋)。規范可以包含下面的通配符使用:
| <init> | matches any ? ? constructor. |
| <fields> | matches any field. |
| <methods> | matches any method. |
| * | matches any field or method. |
<init> 匹配所有構造器
<fields>匹配所有常變量
<methods>匹配所有方法
*匹配所有fields和方法
Note that the above wildcards don't have return types. Only the <init> wildcard has an argument list.
Fields and methods may also be specified using regular expressions. Names can contain the following wildcards:
請注意,以上的通配符不需要設置返回類型的。只有<init>通配符可設置入參。
field和menthods也可以用正規的的表達式表達。以下是可用的通配符:
| ? | matches any single character in a method name. |
| * | matches any part of a method name. |
?匹配方法名單個任意字符
*匹配方法名任意部分
Types in descriptors can contain the following wildcards:
表示類型的可以是以下的通配符:
| % | matches any primitive type ("boolean", "int", etc, but not "void"). |
| ? | matches any single character in a class name. |
| * | matches any part of a class name not containing the package separator. |
| ** | matches any part of a class name, possibly containing any number of package separators. |
| *** | matches any type (primitive or non-primitive, array or non-array). |
| … | matches any number of arguments of any type.
|
%匹配任何基本類型(如boolean,int等,但不能匹配void)
?匹配類名任何單個字符
*匹配不包含包分隔符的類名的任何部分
**匹配包含包分隔符的類名的任何部分
***匹配任何類型(基本的非基本的,數組的非數組的)
…匹配任意多任意類型的入參
Note that the ?, *, and ** wildcards will never match primitive types. Furthermore, only the *** wildcards will match array types of any dimension. For example, "** get*()" matches "java.lang.Object getObject()", but not "float getFloat()", nor "java.lang.Object[] getObjects()".
請注意,?,*,**不能匹配基本類型。除此之外,只有***能匹配任何規模的數組類型。
例如,** get*()" 匹配 "java.lang.Object getObject()", 但不匹配 "float getFloat()", 也不匹配 "java.lang.Object[] getObjects()".
Constructors can also be specified using their short class names (without package) or using their full class names. As in the Java language, the constructor specification has an argument list, but no return type.
The class access modifiers and class member access modifiers are typically used to restrict wildcarded classes and class members. They specify that the corresponding access flags have to be set for the member to match. A preceding ! specifies that the corresponding access flag should be unset.Combining multiple flags is allowed (e.g. public static). It means that both access flags have to be set (e.g. public and static), except when they are conflicting, in which case at least one of them has to be set (e.g. at least public or protected).ProGuard supports the additional modifiers synthetic, bridge, and varargs, which may be set by compilers.
構造器也可以通過設置短類名(不包含包名)或全類名指定。在java中,構造器有入參列表,但沒有返回類型。
類和類成員的作用于也常用于約束通配符限定的類和類成員。通過他們限定匹配的成員。!否決符是不能被設置在作用域標識前的。混合使用作用域標識是可以的(例如 public static).這就意味著,兩種作用域標示符都可以設置,除非他們之間有沖突,也就是說沖突的作用域標示符只能設置一個(例如,設置public或者protected).proguard也支持額外的可以在編譯器上設置的標識如synthetic, bridge, 和 varargs。
總結
以上是生活随笔為你收集整理的ProGuard Usage 翻译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51单片机实验软件proteus出现错误
- 下一篇: 浅谈幼儿园计算机论文,浅谈信息技术与幼儿