Your Guide to ProGuard: Optimize, Shrink, and Obfuscate Java Code Efficiently

Jun 1, 2024 | Programming

ProGuard is a powerful tool that aids developers in optimizing Java bytecode, ensuring that applications are not only faster but also smaller in size. This article is designed to walk you through the functionalities of ProGuard, how to get started with it, and some troubleshooting tips to keep your development smooth.

What is ProGuard?

ProGuard is a free shrinker, optimizer, obfuscator, and preverifier for Java bytecode. Its abilities include:

  • Detecting and removing unused classes, fields, methods, and attributes.
  • Optimizing bytecode and removing unused instructions.
  • Renaming remaining classes, fields, and methods to shorter, meaningless names.

The outcome? Applications and libraries that are considerably smaller and faster.

Getting Started with ProGuard

Quick Start

Using the Command Line

To kick things off, download the latest release from GitHub releases.

Run ProGuard as follows:

  • On Linux/Mac OS: bash bin/proguard.sh options...
  • On Windows: bin/proguard.bat options...

It’s most common to put options into a configuration file (e.g., myconfig.pro) and run it like this:

  • Linux/Mac OS: bash bin/proguard.sh @myconfig.pro
  • Windows: bin/proguard.bat @myconfig.pro

For a complete listing of all options, refer to the configuration section of the manual.

Integrating with Gradle

To run ProGuard as a Gradle task, ensure that it is available in the class path by adding the following to your build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.guardsquare:proguard-gradle:7.6.0'
    }
}

Define a task with the configuration resembling this:

tasks.register('proguard', ProGuardTask) {
    configuration file('proguard.pro')
    injars tasks.named('jar', Jar).flatMap { it.archiveFile }

    // Automatic handling of Java version
    if (System.getProperty('java.version').startsWith('1.')) {
        libraryjars "$System.getProperty('java.home')/librt.jar"
    } else {
        libraryjars "$System.getProperty('java.home')/jmods/java.base.jmod"
    }
    verbose
    outjars layout.buildDirectory.file('libs/$baseCoordinates-minified.jar')
}

For more details on Gradle setup, consult the Gradle setup page.

ProGuard Features

ProGuard operates like an advanced optimizing compiler, boasting a variety of features:

  • Removes unused classes, fields, methods, and attributes.
  • Shortens identifiers and merges classes.
  • Inlines methods and propagates constants.
  • May reduce application size by 20% to 90%, depending on external libraries.
  • Improvements in application performance by up to 20%.
  • Removes logging code without requiring source code access.

Building ProGuard

Building ProGuard is straightforward; you will need a Java 8 JDK installed. Clone the ProGuard repository and run:

bash ./gradlew assemble

The generated artifacts will be in the lib directory, and you can execute ProGuard using:

bash bin/proguard.sh

To publish artifacts to your local Maven repository, use:

bash ./gradlew publishToMavenLocal

Troubleshooting Ideas

If you encounter any issues while using ProGuard, consider the following:

  • Make sure that your configuration file is correctly set up and paths are accurate.
  • Check whether you’re using the correct Java version supported by your ProGuard version.
  • For usage questions, the Guardsquare Community is there to help.
  • To report bugs or crashes, use the issue tracker.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox