March 5, 2021
Hot Topics:

Want to Optimize Your Java Code? Shrink the Bytecode

  • By Raghu Donepudi
  • Send Email »
  • More Articles »

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.

Figure 2 shows the configuration file used for this example.

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

  • JavaCodeShrinking.zip
  • For Further Reading

  • "ProGuard Introduction" (from proguard.sourceforge.net)

    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.

    Page 2 of 2

    This article was originally published on December 2, 2009

    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

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