There are several rules which control the naming of classes, methods, and fields:
Rule | Description |
---|---|
-keeppackagenames [<filter>] |
Don’t rename packages which match the filter. (ProGuard docs) |
-flattenpackagehierarchy [<name>] |
When renaming a class, move the package containing the class to a common base package with the specified name, or to the default package if no name is specified. Using -allowaccessmodification increases the number of classes which can be moved to a new package. (ProGuard docs) (See note) |
-repackageclasses [<name>] |
When renaming a class, move it to the named package, or to the default package if no package is named. (Overrides -flattenpackagehierarchy ) Using -allowaccessmodification increases the number of classes which can be moved to a new package. (ProGuard docs) (See note) |
-overloadaggressively |
Use the same name as much as possible, even if it may not be allowed by the source language. (ProGuard docs) |
-adaptclassstrings [<filter>] |
Update strings containing class names to use the new names. This can be filtered to only look for strings in certain classes. (ProGuard docs) |
-adaptresourcefilenames [<filter>] |
Rename Java resource files to match renamed classes. This can be filtered to look at particular files. (ProGuard docs) |
-adaptresourcefilecontents [<filter>] |
Update Java resource file contents to match renamed classes. This can be filtered to look at particular files. (ProGuard docs) |
Flatten vs. Repackage
There is a subtle difference between -flattenpackagehierarchy
and -repackageclasses
. -repackageclasses
moves the classes into a single package. -flattenpackagehierarchy
renames the packages to be based on the name, keeping classes in their own package.
Given three classes:
com.example.packageOne.ClassOne
com.example.packageOne.subPackageOne.ClassTwo
com.example.packageTwo.ClassThree
-repackageclasses "go.here"
will result in:
com.example.packageOne.ClassOne -> go.here.a:
com.example.packageOne.subPackageOne.ClassTwo -> go.here.b:
com.example.packageTwo.ClassThree -> go.here.c:
-flattenpackagehierarchy "go.here"
will result in:
com.example.packageOne.ClassOne -> go.here.a.a:
com.example.packageOne.subPackageOne.ClassTwo -> go.here.b.a:
com.example.packageTwo.ClassThree -> go.here.c.a:
Dictionaries
R8 will provide new names by cycling through the English alphabet. By using dictionaries, it is possible to control, to a degree, how R8 will determine the new names for classes, methods, and fields.
Rule | Description |
---|---|
-classobfuscationdictionary <filename> |
Use the specified file to find new names for classes. (ProGuard docs) |
-obfuscationdictionary <filename> |
Use the specified file to find new names for methods and fields. (ProGuard docs) |
-packageobfuscationdictionary <filename> |
Use the specified file to find new names for packages. (ProGuard docs) |
Dictionary Files
The dictionary files contain lists of unique names separated by whitespace or punctuation. A #
can be used to specify a comment. The filename specified should be relative to the directory containing the rules file. The names must consist of characters allowed for Java identifiers.
a1, a2, a3 #A few identifiers
class package for while do if else switch goto this null #Reserved word identifiers
#Identifiers on their own lines
q
w
e
r
t
y
Mapping Files
Map files contain direct links between the original and new names of classes, methods, and fields.
Rule | Description |
---|---|
-applymapping <filename> |
Use the specified map for renaming. (ProGuard docs) |
-printmapping [<filename>] |
Print a mapping from the original to the new names to the specified file, or to stdout if there is no file specified. (ProGuard docs) (See note) |
-printmapping
Regardless of the -printmapping
rule, maps will always be output to a variant specific file (e.g.build/outputs/mapping[/r8][/{flavorName}]/{buildType}/mapping.txt
). If -printmapping
is configured to print to a file in a configuration that is used by more than one variant, the configured file will be overwritten to reflect whichever variant built last.