Want to Optimize Your Java Code? Shrink the Bytecode

Code Shrinking with ProGuard

When 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.

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 Annotations

ProGuard 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 Easy

When programmers are under constant deadline pressure, something has to giveespecially 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.

Code Download

For Further Reading

About the Author

Raghu 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 dsraghu@hotmail.com.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date