After the unnecessary code is removed, next comes another key part of ProGuard: Obfuscate. Renaming is a more complex operation: change one name, and you have to find every place in the whole project that references it and change it too.
To simplify the analysis, the following only covers renaming classes. After simplifying, it’s again divided into two parts: creating the new name and replacing the old name, explained below in the order they run.
Creating the new name
Following the earlier analysis, again we go straight to Obfuscator’s execute():
// In Obfuscator |
ProGuard lets you additionally specify the words used to replace names in the Obfuscate stage. To simplify the analysis, we assume none are set here, so classNameFactory and packageNameFactory at the beginning are both null.
Next, classesAccept is called:
// In ClassPool |
As usual, the ProgramClasses are taken out one by one here, and accept() is called with the newly created ClassObfuscator passed in.
Below we keep using the example from the previous chapters:
com/example/classname |
So the current ProgramClass represents com/example/classname. Let’s go straight to visitProgramClass():
// In ClassObfuscator |
On the first visit, newClassName is null. First look at internalPackagePrefix(). This method is simple: it takes the input string up to the last /. With the example, we get:
com/example/ |
Then it’s passed into newPackagePrefix():
// In ClassObfuscator |
If the passed-in package doesn’t have a new name yet, newPackagePrefix is called recursively.
With the example, what’s passed into the next recursion is com/, so packagePrefix is com/, and newSuperPackagePrefix becomes the empty string "". The empty string goes into the next recursion, but because the value it maps to in packagePrefixMap is also the empty string, "" is returned directly.
Back in the com/ recursion, "" is passed into generateUniquePackagePrefix():
// In ClassObfuscator |
Here a SimpleNameFactory is created and passed, together with "", into generateUniquePackagePrefix():
// In ClassObfuscator |
This method is simple. First, SimpleNameFactory’s nextName() is used to get a new name. We won’t go into its content here; in short, it’s a combination of English letters, just like the results we usually get from ProGuard.
Assume we get a; then the newPackagePrefix returned at the end is "" plus a plus /. After newPackagePrefix is returned, it’s put into packagePrefixMap together with com/, giving the first name mapping:
com/ -> a/ |
The com/example part follows the same pattern, giving the following mapping:
com/example/ -> a/b/ |
Back in visitProgramClass, newPackagePrefix gets a/b/. newClassName is generated by generateUniqueClassName():
// In ClassObfuscator |
Here SimpleNameFactory is again used to get a new name; assume it’s c:
// In ClassObfuscator |
This combines the package obtained earlier with the new class name, so the name of the example class becomes:
a/b/c |
Back in ClassObfuscator, after getting the new name, it’s set through setNewClassName():
// In ClassObfuscator |
So at this point the ProgramClass holds the new name. But you may have noticed that this setting doesn’t actually change the name of the class. If getName() is called:
// In ClassConstant |
Clearly the way the name is read has nothing to do with how it was just set; instead it’s related to u2nameIndex. This variable is related to the Constant Pool in Java’s class file structure, which we won’t go into here. For details, see the official document.
Replacing the old name
Next, let’s see where the new name is actually set from visitorInfo into u2nameIndex.
Back in execute(), reading further down, we see the following snippet:
// In Obfuscator |
From experience, we can go straight to ClassRenamer:
// In ClassRenamer |
thisClassConstantAccept() goes straight to visitClassConstant():
// In ClassRenamer |
Here ClassObfuscator’s static method is used:
// In ClassObfuscator |
There’s no special logic; it just calls getVisitorInfo(). From the earlier analysis, we can be sure that a/b/c is obtained and returned here.
Back in ClassRenamer, ConstantPoolEditor is then used to call addUtf8Constant() with the new class name passed in, and the result is stored into u2nameIndex.
This connects with the earlier analysis: the next time getName() is called, it returns a/b/c instead of com/example/classname.
What’s more
After reading the above, you might wonder: if the class name has been replaced, how are the methods that reference this class adjusted?
Continuing down from execute(), we reach:
// In Obfuscator |
Let’s go straight to ClassReferenceFixer’s visitProgramClass():
// In ClassReferenceFixer |
methodsAccept() goes straight to visitProgramMethod():
// In ClassReferenceFixer |
Here the descriptor is taken out to be modified. A descriptor is how Java represents the types of a method’s parameters and return value; we won’t go into detail here, see the official document for details. You can understand it as: just by changing the descriptor, the classes referenced by the method can be replaced with the new names.
Below, assume we have a method that takes com.example.classname as input and has no return value. The descriptor of this method is:
(Lcom/example/classname;)V |
So ClassReferenceFixer’s visitProgramMethod first gets the old descriptor, and passes it into newDescriptor() together with referencedClasses, which is the ProgramClass representing com/example/classname:
// In ClassReferenceFixer |
The passed-in descriptor is first wrapped into a DescriptorClassEnumeration, whose content we won’t go into here. In short, the first nextFluff() gets (L, so the current value of newDescriptorBuffer is (L.
Then, through a loop, nextClassName() and nextFluff() are called in order, with the following results respectively:
className = com/example/classname |
Then it goes to newClassName():
private static String newClassName(String className, Clazz referencedClass) { |
Here the getName() mentioned earlier is used. From the earlier analysis, the value obtained here is the new name a/b/c. After it’s returned and appended to newDescriptorBuffer, we get the following result:
(La/b/c;)V |
Back in ClassReferenceFixer, the new descriptor is assigned to u2descriptorIndex. Looking at where it’s used confirms that it’s the field used when getting the descriptor:
// In ProgramMethod |
That’s the basic process from creating the new name to replacing it.
Conclusion
With this, the introduction to ProGuard comes to an end. Although the analysis only focused on classes for the sake of smooth reading, I believe you’ve gained a certain understanding of ProGuard’s implementation along the way. If you’re interested in reading the other parts, it should be much smoother than at the beginning.