In Part3 we covered how the ProGuard configuration is read. Now let’s go back to the entry point of ProGuardTask:
// In ProGuardTask |
After the Configuration is read, execute() is called:
// In ProGuard |
Let’s go straight to readInput():
// In ProGuard |
From the content, we can confirm this is what we mentioned in Part1: after reading the ProGuard configuration, the code to be processed is read next, and divided into the Program class pool and the Library class pool.
Next, look at execute():
// In InputReader |
First, the library is read through readInput(). For a detailed introduction of this method, see the Appendix. In short, this method is responsible for:
- Unzipping and reading the Class files inside the archive and putting them into the class pool.
In this example, it reads the Class files from the Jar file paths listed in configuration.libraryJars and adds them to libraryClassPool.
// Read the program class files. |
The previous part reads the library; next comes reading the program. What’s different from before is the last argument used, ClassPresenceFilter.
Before going deeper, let’s first break this ClassPresenceFilter down. Its structure is as follows:
ClassPresenceFilter { |
Following the introduction in the Appendix, this ClassPresenceFilter executes the following steps:
- Check whether the class that was read is already in the first one,
programClassPool. If it is,duplicateClassPrinteris used directly to output a note; if not, it goes into the next ClassPresenceFilter. - The second ClassPresenceFilter checks whether the class is already in
libraryClassPool. If it is, a note is output as well; if not, it’s added toprogramClassPoolthrough ClassPoolFiller.
In short, if it’s in neither programClassPool nor libraryClassPool, it’s put into programClassPool.
... |
At this point, the Class files inside the Jar files specified through -injars and -libraryjars have all been read and sorted into the Library class pool and the Program class pool.
What’s more
From the introduction above, you can see that the timing of reading the library may change depending on FAVOR_LIBRARY_CLASSES:
// In InputReader |
Through a specific system property, favor.library.classes, you can decide whether to process the library before the program.
The processing order then becomes one of the following two:
- With
favor.library.classes: library first, then program. - Without
favor.library.classes: program first, then library.
This change was mainly proposed by a Google engineer who works on AOSP. For details, see the discussion thread.
Appendix
readInput()
The following uses a snippet in InputReader.execute() as the example:
// In InputReader |
Get file path
Here configuration.libraryJars is used. It’s a ClassPath, which Configuration uses to record the file paths obtained from the configuration files:
// In Configuration |
Paths set through -injars and -outjars are put in programJars; -libraryjars goes into libraryJars.
The readInput() above goes into the following readInput():
// In InputReader |
Here the file paths are taken out of the ClassPath one by one and passed into another readInput():
// In InputReader |
Constructor DataEntryReader
Here DataEntryReaderFactory.createDataEntryReader() is used to wrap the passed-in dataEntryReader once more:
public static DataEntryReader createDataEntryReader(String messagePrefix, |
Only the more commonly used parts are kept here. For an Android project, classPathEntry should be the path of a Jar file, and reader is the ClassFilter mentioned in the example, which wraps the ClassReader; for simplicity it’s referred to as the ClassReader below.
First, look at the first call to wrapInJarReader():
// In DataEntryReaderFactory |
Again the unused parts are omitted, so the passed-in reader, which is the ClassReader, is first wrapped into a JarReader.
isJar is isApk, so it’s false and goes into the else branch: the JarReader, together with the passed-in ClassReader and a DataEntryNameFilter, is wrapped into a FilteredDataEntryReader and returned.
Let’s first mention DataEntryNameFilter here:
// In DataEntryNameFilter |
The passed-in stringMatcher is an ExtensionMatcher:
// In ExtensionMatcher |
From the content above, extension is the .apk passed in when wrapInJarReader was called.
Back in createDataEntryReader(), isApk is false, so wrapInJarReader() is called again. The FilteredDataEntryReader we just got is wrapped into another JarReader, but this time isJar is true, so the current JarReader is returned directly.
So far, the reader obtained through DataEntryReaderFactory.createDataEntryReader() has the following structure:
JarReader { |
Unzip file
After getting the needed DataEntryReader, a DirectoryPump is created, and it starts reading the file through pumpDataEntries().
// In DirectoryPump |
The first thing used here is JarReader’s read():
// in JarReader |
We know a Jar file is in the Zip archive format, so it makes sense to use ZipInputStream to get the content of the Jar file. Through getNextEntry(), the Class files can be taken out one by one and passed to dataEntryReader.read():
// In FilteredDataEntryReader |
Through dataEntryFilter, which is the DataEntryNameFilter, accepts is called to make the decision:
// In DataEntryNameFilter |
When this method is called, it uses stringMatcher, which is the ExtensionMatcher, to call matches and passes in the file path. From the process just described, we know the path passed in here ends with .class:
// In ExtensionMatcher |
What it does is simple: check whether the end of the file path is the same as extension. The value of extension is the .apk passed in at the first call to wrapInJarReader, so it returns false.
Back in FilteredDataEntryReader.read(), getting false means rejectedDataEntryReader, which is the ClassReader, is used to continue.
Read class
// In ClassReader |
The ClassReader here is the one passed in when InputReader.readInput() was called at the beginning of the example, so isLibrary is true, and a LibraryClassReader is passed in through LibraryClass.accept():
// In LibraryClass |
This is a Visitor Pattern, so we need to go back and look at LibraryClassReader’s visitLibraryClass():
// In LibraryClassReader |
LibraryClassReader gets the content of the Class file through dataInputStream and parses it. The process of reading the file is no longer related to this post, so we won’t go deeper. In short, the parsed result is stored back into the LibraryClass, and the same goes for ProgramClass.
Here, based on how accept() is used, we can point out a concept first:
- Executing
clazz.accept(visitor)can be read directly asvisitor.visitXXXClass(). Whetherclazzis a ProgramClass or a LibraryClass decides whatXXXis.
Put into pool
Back in ClassReader.read(), if the LibraryClassReader part completed successfully, getName() won’t be null, so accept is called once more with classVisitor, which is the ClassPresenceFilter mentioned in the example above:
// In ClassPresenceFilter |
From the content above, we know it goes to visitLibraryClass:
// In ClassPresenceFilter |
First, classFileVisitor is used to find the suitable ClassVisitor to call visitLibraryClass:
// In ClassPresenceFilter |
This checks whether the current class already exists in classPool, which is the ClassPool passed in by ProGuard when InputReader.execute() was called.
If it does, presentClassVisitor is called, which is the DuplicateClassPrinter, and is only used to output a note:
// In DuplicateClassPrinter |
If it’s missingClassVisitor, it’s a ClassPoolFiller. This class only implements visitAnyClass, but its parent class SimplifiedVisitor has:
// In SimplifiedVisitor |
This way, the class that was read is added to the Library class pool or the Program class pool.
So, readInput() is implemented in the following steps:
- Take out the file paths specified in the ProGuard configuration files one by one.
- Create the corresponding DataEntryReader based on the file type.
- Get the paths of the class files inside the file one by one through the DataEntryReader.
- Read the class files through LibraryClassReader or ProgramClassReader and turn them into clazz objects.
- Sort the clazz into the Library class pool or the Program class pool.
Its main purpose is:
- Unzipping and reading the Class files inside the archive and putting them into the class pool.