Want to Optimize Your Java Code? Shrink the Bytecode, Page 2
Code Shrinking with ProGuardWhen working with various configuration parameters is a hassle, ProGuard provides a simple GUI to add JAR files and generate the output files. You could use the GUI to generate the configuration file from all the selected options.
In the example code for this article, the original size of the input JAR file is 2KB, with one unused class and several lines of dead code intentionally left in the source Java file. After processing through ProGuard, the size of the JAR file decreased to 1KB (a 50% reduction in size).
Figure 1 shows the output of ProGuard displaying snippets of the dead code. In Test.java, ProGuard correctly identified the two variables and the two methods that are part of the unused code.
Figure 1. Snapshot of ProGuard Output Showing Dead Code: ProGuard correctly identified the variables and methods that are part of the unused code.
Figure 2 shows the configuration file used for this example.
Figure 2. Configuration Parameters Used in this Example: The ProGuard shrinking does not impact the JAR files' functionality.
The ProGuard shrinking does not impact the JAR files' functionality. If you are not comfortable with the shrunken JAR files, use ProGuard to identify the dead code and perform a manual code cleanup. Then you can proceed with the regular build process.
ProGuard AnnotationsProGuard provides features to define your own set of annotations and incorporate them into your code. This feature eliminates the need to keep the list of files and methods that you want ProGuard to keep from shrinking. The annotations provided in the ProGuard examples section should suffice for most Java projects though.
Code Cleanup Made EasyWhen programmers are under constant deadline pressure, something has to giveespecially those things that do not provide immediate value, such as dead code cleanup. Even though programmers have the intention to come back and clean up the code, they seldom do. With ProGuard on hand, that does not have to be the case anymore.
For Further Reading
About the AuthorRaghu Donepudi is an independent contractor. He works as a technical manager for a U.S. government agency. He holds a master's degree in computer science from Lamar University in Texas. He is a Sun-certified Java developer who has authored a number of innovative software design techniques. Reach him at email@example.com.
Page 2 of 2