|
| 1 | + |
| 2 | +This page will guide you through to the basic steps of processing your Android application or library with ProGuard. |
| 3 | + |
| 4 | +!!! tip "Java / Kotlin desktop or server projects" |
| 5 | + If you have a Java / Kotlin desktop or server project, you can find instructions [here](gradle.md). |
| 6 | + |
| 7 | +## ProGuard Gradle Plugin (AGP version 4.x - AGP 7.x) |
| 8 | + |
| 9 | +You can add the ProGuard plugin to your project by |
| 10 | +including the following in your root level `build.gradle(.kts)` file: |
| 11 | + |
| 12 | +=== "Groovy" |
| 13 | + ```Groovy |
| 14 | + buildscript { |
| 15 | + repositories { |
| 16 | + google() // For the Android Gradle plugin. |
| 17 | + mavenCentral() // For the ProGuard Gradle Plugin and anything else. |
| 18 | + } |
| 19 | + dependencies { |
| 20 | + classpath 'com.android.tools.build:gradle:x.y.z' // The Android Gradle plugin. |
| 21 | + classpath 'com.guardsquare:proguard-gradle:7.1.0' // The ProGuard Gradle plugin. |
| 22 | + } |
| 23 | + } |
| 24 | + ``` |
| 25 | +=== "Kotlin" |
| 26 | + ```kotlin |
| 27 | + buildscript { |
| 28 | + repositories { |
| 29 | + google() // For the Android Gradle plugin. |
| 30 | + mavenCentral() // For the ProGuard Gradle Plugin and anything else. |
| 31 | + } |
| 32 | + dependencies { |
| 33 | + classpath("com.android.tools.build:gradle:x.y.z") // The Android Gradle plugin. |
| 34 | + classpath("com.guardsquare:proguard-gradle:7.1.0") // The ProGuard Gradle plugin. |
| 35 | + } |
| 36 | + } |
| 37 | + ``` |
| 38 | + |
| 39 | +To actually apply the plugin to your project, |
| 40 | +just add the line to your module level `build.gradle(.kts)` file after applying the Android Gradle plugin as shown below. |
| 41 | + |
| 42 | +=== "Groovy" |
| 43 | + ```Groovy |
| 44 | + apply plugin: 'com.android.application' |
| 45 | + apply plugin: 'com.guardsquare.proguard' |
| 46 | + ``` |
| 47 | +=== "Kotlin" |
| 48 | + ```kotlin |
| 49 | + plugins { |
| 50 | + id("com.android.application") |
| 51 | + id("proguard") |
| 52 | + } |
| 53 | + ``` |
| 54 | + |
| 55 | +ProGuard expects unobfuscated class files as input. Therefore, other obfuscators such as R8 have to be disabled. |
| 56 | + |
| 57 | +=== "Groovy" |
| 58 | + ```Groovy |
| 59 | + android { |
| 60 | + ... |
| 61 | + buildTypes { |
| 62 | + release { |
| 63 | + // Deactivate R8. |
| 64 | + minifyEnabled false |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + ``` |
| 69 | +=== "Kotlin" |
| 70 | + ```kotlin |
| 71 | + android { |
| 72 | + ... |
| 73 | + buildTypes { |
| 74 | + getByName("release") { |
| 75 | + // Deactivate R8. |
| 76 | + isMinifyEnabled = false |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + ``` |
| 81 | + |
| 82 | +ProGuard can be executed automatically whenever you build any of the configured variants. |
| 83 | +You can configure a variant using the `proguard` block in your module level `build.gradle(.kts)` files. |
| 84 | +This is a top-level block and should be placed outside the `android` block. |
| 85 | + |
| 86 | +For example, in the snippet below, ProGuard is configured to only process the release variant of the application, |
| 87 | +using a configuration provided by the user (`proguard-project.txt`) and a [default configuration](#default-configurations) (`proguard-android-optimize.txt`). |
| 88 | + |
| 89 | +=== "Groovy" |
| 90 | + ```Groovy |
| 91 | + android { |
| 92 | + ... |
| 93 | + } |
| 94 | + |
| 95 | + proguard { |
| 96 | + configurations { |
| 97 | + release { |
| 98 | + defaultConfiguration 'proguard-android-optimize.txt' |
| 99 | + configuration 'proguard-project.txt' |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + ``` |
| 104 | +=== "Kotlin" |
| 105 | + ```kotlin |
| 106 | + android { |
| 107 | + ... |
| 108 | + } |
| 109 | + |
| 110 | + proguard { |
| 111 | + configurations { |
| 112 | + register("release") { |
| 113 | + defaultConfiguration("proguard-android-optimize.txt") |
| 114 | + configuration("proguard-project.txt") |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + ``` |
| 119 | + |
| 120 | +You can then build your application as usual: |
| 121 | + |
| 122 | +=== "Linux/macOS" |
| 123 | + ```sh |
| 124 | + ./gradlew assembleRelease |
| 125 | + ``` |
| 126 | +=== "Windows" |
| 127 | + ``` |
| 128 | + gradlew assembleRelease |
| 129 | + ``` |
| 130 | + |
| 131 | +### Default configurations |
| 132 | + |
| 133 | +There are three default configurations available: |
| 134 | + |
| 135 | +| Default configuration | Description | |
| 136 | +|-----------------------|-------------| |
| 137 | +| `proguard-android.txt` | ProGuard will obfuscate and shrink your application | |
| 138 | +| `proguard-android-optimize.txt` | ProGuard will obfuscate, shrink and optimize your application | |
| 139 | +| `proguard-android-debug.txt` | ProGuard will process the application without any obfuscation,<br>optimization or shrinking | |
| 140 | + |
| 141 | +### Consumer rules |
| 142 | + |
| 143 | +ProGuard will apply the consumer rules provided by library dependencies. If you need to exclude these rules, |
| 144 | +you can use the `consumerRuleFilter`. |
| 145 | + |
| 146 | +#### consumerRuleFilter |
| 147 | + |
| 148 | +The `consumerRuleFilter` option allows you to specify a list of maven group and |
| 149 | +module name pairs to filter out the ProGuard consumer rules of the dependencies |
| 150 | +that match the specified group and module pairs. |
| 151 | + |
| 152 | +A group and module name pair is very similar to the maven coordinates you write |
| 153 | +when specifying the dependencies in the `dependencies` block, but without the |
| 154 | +version part. |
| 155 | + |
| 156 | +=== "Groovy" |
| 157 | + ```Groovy |
| 158 | + proguard { |
| 159 | + configurations { |
| 160 | + release { |
| 161 | + consumerRuleFilter 'groupName:moduleName', 'anotherGroupName:anotherModuleName' |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + ``` |
| 166 | +=== "Kotlin" |
| 167 | + ```Kotlin |
| 168 | + proguard { |
| 169 | + configurations { |
| 170 | + register("release") { |
| 171 | + consumerRuleFilter("groupName:moduleName", "anotherGroupName:anotherModuleName") |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + ``` |
| 176 | + |
| 177 | +### Example |
| 178 | + |
| 179 | +The example [`android-plugin`](https://github.com/Guardsquare/proguard/tree/master/examples/android-plugin) |
| 180 | +has a small working Android project using the ProGuard Gradle Plugin. |
| 181 | + |
| 182 | +## AGP Integrated ProGuard (AGP version <7) |
| 183 | + |
| 184 | +ProGuard is integrated with older versions of the Android Gradle plugin. |
| 185 | +If you have an Android Gradle project that uses such an AGP version, |
| 186 | +you can enable ProGuard instead of the default `R8` obfuscator as follows: |
| 187 | + |
| 188 | +1. Disable R8 in your `gradle.properties`: |
| 189 | +```ini |
| 190 | +android.enableR8=false |
| 191 | +android.enableR8.libraries=false |
| 192 | +``` |
| 193 | + |
| 194 | +2. Override the default version of ProGuard with the most recent one in your |
| 195 | + main `build.gradle`: |
| 196 | +```Groovy |
| 197 | +buildscript { |
| 198 | + ... |
| 199 | + configurations.all { |
| 200 | + resolutionStrategy { |
| 201 | + dependencySubstitution { |
| 202 | + substitute module('net.sf.proguard:proguard-gradle') with module('com.guardsquare:proguard-gradle:7.1.0') |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +3. Enable minification as usual in your `build.gradle`: |
| 210 | +```Groovy |
| 211 | +android { |
| 212 | + ... |
| 213 | + buildTypes { |
| 214 | + release { |
| 215 | + minifyEnabled true |
| 216 | + shrinkResources true |
| 217 | + proguardFile getDefaultProguardFile('proguard-android-optimize.txt') |
| 218 | + proguardFile 'proguard-project.txt' |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | + There are two default configurations available when using the integrated ProGuard: |
| 225 | + |
| 226 | + | Default configuration | Description | |
| 227 | + |-----------------------|-------------| |
| 228 | + | `proguard-android.txt` | ProGuard will obfuscate and shrink your application | |
| 229 | + | `proguard-android-optimize.txt` | ProGuard will obfuscate, shrink and optimize your application | |
| 230 | + |
| 231 | +4. Add any necessary configuration to your `proguard-project.txt`. |
| 232 | + |
| 233 | +You can then build your application as usual: |
| 234 | + |
| 235 | +=== "Linux/macOS" |
| 236 | + ```sh |
| 237 | + ./gradlew assembleRelease |
| 238 | + ``` |
| 239 | +=== "Windows" |
| 240 | + ``` |
| 241 | + gradlew assembleRelease |
| 242 | + ``` |
| 243 | + |
| 244 | +### Example |
| 245 | + |
| 246 | +The example [`android-agp3-agp4`](https://github.com/Guardsquare/proguard/tree/master/examples/android-agp3-agp4) |
| 247 | +has a small working Android project for AGP < 7. |
| 248 | + |
| 249 | + |
| 250 | + |
0 commit comments