Skip to content

Commit

Permalink
Update ProGuard Gradle plugin to be compatible with AGP 7
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjameshamilton authored and maqsoodahmadjan committed May 25, 2021
1 parent e00da10 commit 5f69246
Show file tree
Hide file tree
Showing 60 changed files with 1,953 additions and 1,406 deletions.
2 changes: 1 addition & 1 deletion docs/md/manual/home.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Welcome to the manual for **ProGuard** version 7.0.2 ([what's new?](releasenotes.md)).
Welcome to the manual for **ProGuard** version 7.1 ([what's new?](releasenotes.md)).

ProGuard is an open-sourced Java class file shrinker, optimizer, obfuscator, and
preverifier. As a result, ProGuard processed applications and libraries are smaller, faster, and somewhat hardened against reverse engineering.
Expand Down
11 changes: 4 additions & 7 deletions docs/md/manual/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,21 @@ ProGuard can then be executed directly from the command line by calling a script
-outjars path/to/obfuscated-application.jar ^
-libraryjars path/to/java/home/lib/rt.jar
```



For more detailed information see [standalone mode](setup/standalone.md).


## Integrated

The ProGuard artifacts are hosted at [Maven Central](https://search.maven.org/search?q=g:com.guardsquare).

### Android Gradle project

When working on your Android app (apk, aab) or library (aar), you can include ProGuard in your Gradle build as follows:
When working on your Android application (apk, aab) or library (aar), you can include ProGuard in your Gradle build by:

1. Either directly use ProGuard by disabling R8 in your `gradle.properties`.
2. Or use ProGuard's built-in Gradle plugin, which you can apply in your `build.gradle`.
- Using ProGuard's Gradle plugin, which you can apply in your `build.gradle` file.
- Using the integrated ProGuard by disabling R8 in your `gradle.properties` (only applicable for AGP < 7).

For more detailed information see [Android Gradle](setup/gradleplugin.md).
For more detailed information see [Android Gradle](setup/gradleplugin.md).

### Java or Kotlin Gradle project

Expand Down
1 change: 1 addition & 0 deletions docs/md/manual/releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

| Version| Issue | Module | Explanation
|--------|----------|----------|----------------------------------
| 7.1.0 | | GRADLE | New AGP 7.0 compatible [Gradle plugin](setup/gradleplugin.md).
| 7.1.0 | DGD-3264 | CORE | Reduced false positives when using `-addconfigurationdebugging`.
| 7.1.0 | DGD-0013 | CORE | Improved error message when missing classes result in an incomplete class hierarchy.
| 7.1.0 | PGD-0106 | GRADLE | Fixed Gradle task error when using an existing `-outjar` directory.
Expand Down
6 changes: 5 additions & 1 deletion docs/md/manual/setup/gradle.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
**ProGuard** can be run as a task in the Java-based build tool Gradle
(version 2.1 or higher).

!!! android "Android projects"
If you have an Android project, you can find instructions [here](gradleplugin.md).


Before you can use the **`proguard`** task, you have to make sure Gradle can
find it in its class path at build time. One way is to add the following
line to your **`build.gradle`** file:
Expand All @@ -10,7 +14,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.guardsquare:proguard-gradle:7.0.1'
classpath 'com.guardsquare:proguard-gradle:7.1.0'
}
}
```
Expand Down
272 changes: 169 additions & 103 deletions docs/md/manual/setup/gradleplugin.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,153 @@
ProGuard is integrated in Google's Android SDK. If you have an Android Gradle
project you can enable ProGuard instead of the default R8 compiler:

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

!!! tip "Java / Kotlin desktop or server projects"
If you have a Java / Kotlin desktop or server project, you can find instructions [here](gradle.md).

## 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:

=== "Groovy"
```Groovy
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.
}
}
```
=== "Kotlin"
```kotlin
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.

=== "Groovy"
```Groovy
apply plugin: 'com.android.application'
apply plugin: 'proguard'
```
=== "Kotlin"
```kotlin
plugins {
id("com.android.application")
id("proguard")
}
```

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

=== "Groovy"
```Groovy
android {
...
buildTypes {
release {
// Deactivate R8.
minifyEnabled false
}
}
}
```
=== "Kotlin"
```kotlin
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](#default-configurations) (`proguard-android-optimize.txt`).

=== "Groovy"
```Groovy
android {
...
}

proguard {
configurations {
release {
defaultConfiguration 'proguard-android-optimize.txt'
configuration 'proguard-project.txt'
}
}
}
```
=== "Kotlin"
```kotlin
android {
...
}

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

You can then build your application as usual:

=== "Linux/macOS"
```sh
./gradlew assembleRelease
```
=== "Windows"
```
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,<br>optimization or shrinking |

### Example

The example [`android-plugin`](https://github.com/Guardsquare/proguard/tree/master/examples/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`:
```ini
Expand All @@ -15,7 +163,7 @@ buildscript {
configurations.all {
resolutionStrategy {
dependencySubstitution {
substitute module('net.sf.proguard:proguard-gradle') with module('com.guardsquare:proguard-gradle:7.0.1')
substitute module('net.sf.proguard:proguard-gradle') with module('com.guardsquare:proguard-gradle:7.1.0')
}
}
}
Expand All @@ -37,112 +185,30 @@ android {
}
```

4. Add any necessary configuration to your `proguard-project.txt`.

You can then build your application as usual:
```sh
gradle assembleRelease
```
There are two default configurations available when using the integrated ProGuard:

The repository contains some sample configurations in the [examples](examples)
directory. Notably, [examples/android](examples/android) has a small working
Android project.
You can enable ProGuard in your Android Gradle build process, by enabling
| 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 Gradle Plugin

Alternatively, you can enable ProGuard in the Android Gradle build process
using ProGuard's own tuned plugin, by changing the `build.gradle` file of your
project as follows:

```Groovy
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.guardsquare:proguard-gradle:7.0.1'
}
}
apply plugin: 'com.android.application'
apply plugin: 'proguard'
```

Or if you want to use your local copy of the plugin:

```Groovy
buildscript {
repositories {
flatDir dirs: '/usr/local/java/proguard/lib'
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath ':proguard:'
}
}
apply plugin: 'com.android.application'
apply plugin: 'proguard'
```

Please make sure the repository path in the build script is set correctly for
your system.

Each build type that should be processed by ProGuard needs to have a set of
configuration files, as shown below:

```Groovy
android {
.....
buildTypes {
debug {
minifyEnabled false
proguardFile getTunedProGuardFile('proguard-android-debug.pro')
proguardFile 'proguard-project.txt'
}
release {
minifyEnabled false
proguardFile getTunedProGuardFile('proguard-android-release.pro')
proguardFile 'proguard-project.txt'
}
}
}
```

The setting "`minifyEnabled=false`" is needed to disable the
obfuscation/shrinking capability of the standard gradle plugin to avoid that
the project is obfuscated multiple times.

The lines with "`proguardFile getTunedProGuardFile`" are important. They apply
optimized minimal settings for the Android platform. Your own configuration
files then only need to contain project-specific settings.
4. Add any necessary configuration to your `proguard-project.txt`.

You can find a complete sample project in `examples/android-plugin` in the
ProGuard distribution.
You can then build your application as usual:

## Settings {: #proguard}
=== "Linux/macOS"
```sh
./gradlew assembleRelease
```
=== "Windows"
```
gradlew assembleRelease
```

The **ProGuard plugin** supports various settings that can be added to the
*build.gradle* file to control its behavior:
### Example

```Groovy
proguard {
incremental false
transformExternalLibraries true
transformSubprojects true
}
```
The example [`android-agp3-agp4`](https://github.com/Guardsquare/proguard/tree/master/examples/android-agp3-agp4)
has a small working Android project for AGP < 7.

`incremental:`
: Support incremental builds, default: `false`

`transformExternalLibraries:`
: Processing also all external libraries, default: `true`

`transformSubproject:`
: Processing also all subprojects, default: `true`
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// This build file illustrates how to apply ProGuard in the Android build
// process with AGP < 7, by swapping the built-in version of ProGuard for a newer version.

// This process relies on setting `android.enableR8=false` in `gradle.properties`,
// which is deprecated. For AGP7, please see the `android-plugin` example.

buildscript {
repositories {
mavenLocal() // For local testing
google() // For the Android plugin.
mavenCentral() // For anything else.
}
Expand All @@ -10,7 +17,7 @@ buildscript {
resolutionStrategy {
// Override the default version of ProGuard with the most recent one.
dependencySubstitution {
substitute module('net.sf.proguard:proguard-gradle') with module('com.guardsquare:proguard-gradle:7.0.1')
substitute module('net.sf.proguard:proguard-gradle') with module('com.guardsquare:proguard-gradle:7.1.0-beta5')
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions examples/android-plugin/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="15" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
Expand Down
Loading

0 comments on commit 5f69246

Please sign in to comment.