Request Pricing
Login
Request Pricing

Login

    ProGuard manual

    Setup - Android Gradle

    This page will guide you through to the basic steps of processing your Android application or library with ProGuard.

    Java / Kotlin desktop or server projects

    If you have a Java / Kotlin desktop or server project, you can find instructions here.

    ProGuard Gradle Plugin (AGP version 4+)

    You can add the ProGuard plugin to your project by including the following in your root level build.gradle(.kts) file:

    buildscript {
        repositories {
            google()       // For the Android Gradle plugin.
            mavenCentral() // For the ProGuard Gradle Plugin and anything else.
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:x.y.z'    // The Android Gradle plugin.
            classpath 'com.guardsquare:proguard-gradle:7.1.0'  // The ProGuard Gradle plugin.
        }
    }
    buildscript {
        repositories {
            google()       // For the Android Gradle plugin.
            mavenCentral() // For the ProGuard Gradle Plugin and anything else.
        }
        dependencies {
            classpath("com.android.tools.build:gradle:x.y.z")  // The Android Gradle plugin.
            classpath("com.guardsquare:proguard-gradle:7.1.0") // The ProGuard Gradle plugin.
        }
    }

    To actually apply the plugin to your project, just add the line to your module level build.gradle(.kts) file after applying the Android Gradle plugin as shown below.

     apply plugin: 'com.android.application'
     apply plugin: 'com.guardsquare.proguard'
    plugins {
        id("com.android.application")
        id("proguard")
    }

    ProGuard expects unobfuscated class files as input. Therefore, other obfuscators such as R8 have to be disabled.

    android {
        ...
        buildTypes {
           release {
              // Deactivate R8.
              minifyEnabled false
           }
        }
    }
    android {
        ...
        buildTypes {
            getByName("release") {
                // Deactivate R8.
                isMinifyEnabled = false
            }
        }
    }

    ProGuard can be executed automatically whenever you build any of the configured variants. You can configure a variant using the proguard block in your module level build.gradle(.kts) files. This is a top-level block and should be placed outside the android block.

    For example, in the snippet below, ProGuard is configured to only process the release variant of the application, using a configuration provided by the user (proguard-project.txt) and a default configuration (proguard-android-optimize.txt).

    android {
        ...
    }
    
    proguard {
       configurations {
          release {
             defaultConfiguration 'proguard-android-optimize.txt'
             configuration 'proguard-project.txt'
          }
       }
    }
    android {
        ...
    }
    
    proguard {
       configurations {
          register("release") {
             defaultConfiguration("proguard-android-optimize.txt")
             configuration("proguard-project.txt")
          }
       }
    }

    You can then build your application as usual:

    ./gradlew assembleRelease
    gradlew assembleRelease

    Default configurations

    There are three default configurations available:

    Default configuration Description
    proguard-android.txt ProGuard will obfuscate and shrink your application
    proguard-android-optimize.txt ProGuard will obfuscate, shrink and optimize your application
    proguard-android-debug.txt ProGuard will process the application without any obfuscation,
    optimization or shrinking

    Example

    The example android-plugin has a small working Android project using the ProGuard Gradle Plugin.

    AGP Integrated ProGuard (AGP version <7)

    ProGuard is integrated with older versions of the Android Gradle plugin. If you have an Android Gradle project that uses such an AGP version, you can enable ProGuard instead of the default R8 obfuscator as follows:

    1. Disable R8 in your gradle.properties:

      android.enableR8=false
      android.enableR8.libraries=false

    2. Override the default version of ProGuard with the most recent one in your main build.gradle:

      buildscript {
          ...
          configurations.all {
              resolutionStrategy {
                  dependencySubstitution {
                      substitute module('net.sf.proguard:proguard-gradle') with module('com.guardsquare:proguard-gradle:7.1.0')
                  }
              }
          }
      }

    3. Enable minification as usual in your build.gradle:

      android {
          ...
          buildTypes {
              release {
                  minifyEnabled   true
                  shrinkResources true
                  proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
                  proguardFile 'proguard-project.txt'
              }
          }
      }

      There are two default configurations available when using the integrated ProGuard:

      Default configuration Description
      proguard-android.txt ProGuard will obfuscate and shrink your application
      proguard-android-optimize.txt ProGuard will obfuscate, shrink and optimize your application
    4. Add any necessary configuration to your proguard-project.txt.

    You can then build your application as usual:

    ./gradlew assembleRelease
    gradlew assembleRelease

    Example

    The example android-agp3-agp4 has a small working Android project for AGP < 7.