## Gradle Task

ProGuard can be run as a task in the Java-based build tool Gradle (version 2.1 or higher).

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:

    buildscript {
        repositories {
            flatDir dirs: '/usr/local/java/proguard/lib'
        }
        dependencies {
            classpath ':proguard:'
        }
    }

Please make sure the class path is set correctly for your system.

You can then define a task:

    task myProguardTask(type: proguard.gradle.ProGuardTask) {
        .....
    }

The embedded configuration is much like a standard ProGuard configuration. Notable similarities and differences:

- Like in ProGuard-style configurations, we're using all lower-case names for the settings.
- The options don't have a dash as prefix.
- Arguments typically have quotes.
- Some settings are specified as named arguments.

You can find some sample build files in the `examples/gradle` directory of the ProGuard distribution.

If you prefer a more verbose configuration derived from the Ant task, you can import the Ant task as a Gradle task.

## Settings

The ProGuard task supports the following settings in its closure:

`configuration` files
  Read and merge options from the given ProGuard-style configuration files. The files are resolved and parsed lazily, during the execution phase.
`injars` class_path
  Specifies the program jars (or aars, wars, ears, zips, apks, or directories). The files are resolved and read lazily, during the execution phase.
`outjars` class_path
  Specifies the names of the output jars (or aars, wars, ears, zips, apks, or directories). The files are resolved and written lazily, during the execution phase.
`libraryjars` class_path
  Specifies the library jars (or aars, wars, ears, zips, apks, or directories). The files are resolved and read lazily, during the execution phase.
`skipnonpubliclibraryclasses`
  Ignore non-public library classes.
`dontskipnonpubliclibraryclassmembers`
  Don't ignore package visible library class members.
`keepdirectories` ['directory_filter']
  Keep the specified directories in the output jars (or aars, wars, ears, zips, apks, or directories).
`target` 'version'
  Set the given version number in the processed classes.
`forceprocessing`
  Process the input, even if the output seems up to date.
`keep` [modifier,...] class_specification
  Preserve the specified classes and class members.
`keepclassmembers` [modifier,...] class_specification
  Preserve the specified class members, if their classes are preserved as well.
`keepclasseswithmembers` [modifier,...] class_specification
  Preserve the specified classes and class members, if all of the specified class members are present.
`keepnames` class_specification
  Preserve the names of the specified classes and class members (if they aren't removed in the shrinking step).
`keepclassmembernames` class_specification
  Preserve the names of the specified class members (if they aren't removed in the shrinking step).
`keepclasseswithmembernames` class_specification
  Preserve the names of the specified classes and class members, if all of the specified class members are present (after the shrinking step).
`printseeds` [file]
  List classes and class members matched by the various `keep` commands, to the standard output or to the given file.
`dontshrink`
  Don't shrink the input class files.
`printusage` [file]
  List dead code of the input class files, to the standard output or to the given file.
`whyareyoukeeping` class_specification
  Print details on why the given classes and class members are being kept in the shrinking step.
`dontoptimize`
  Don't optimize the input class files.
`optimizations` 'optimization_filter'
  Perform only the specified optimizations.
`optimizationpasses` n
  The number of optimization passes to be performed.
`assumenosideeffects` class_specification
  Assume that the specified methods don't have any side effects, while optimizing. Only use this option if you know what you're doing!
`allowaccessmodification`
  Allow the access modifiers of classes and class members to be modified, while optimizing.
`mergeinterfacesaggressively`
  Allow any interfaces to be merged, while optimizing.
`dontobfuscate`
  Don't obfuscate the input class files.
`printmapping` [file]
  Print the mapping from old names to new names for classes and class members that have been renamed, to the standard output or to the given file.
`applymapping` file
  Reuse the given mapping, for incremental obfuscation.
`obfuscationdictionary` file
  Use the words in the given text file as obfuscated field names and method names.
`classobfuscationdictionary` file
  Use the words in the given text file as obfuscated class names.
`packageobfuscationdictionary` file
  Use the words in the given text file as obfuscated package names.
`overloadaggressively`
  Apply aggressive overloading while obfuscating.
`useuniqueclassmembernames`
  Ensure uniform obfuscated class member names for subsequent incremental obfuscation.
`dontusemixedcaseclassnames`
  Don't generate mixed-case class names while obfuscating.
`keeppackagenames` ['package_filter']
  Keep the specified package names from being obfuscated. If no name is given, all package names are preserved.
`flattenpackagehierarchy` 'package_name'
  Repackage all packages that are renamed into the single given parent package.
`repackageclasses` ['package_name']
  Repackage all class files that are renamed into the single given package.
`keepattributes` ['attribute_filter']
  Preserve the specified optional Java bytecode attributes, with optional wildcards. If no name is given, all attributes are preserved.
`keepparameternames`
  Keep the parameter names and types of methods that are kept.
`renamesourcefileattribute` ['string']
  Put the given constant string in the `SourceFile` attributes.
`adaptclassstrings` ['class_filter']
  Adapt string constants in the specified classes, based on the obfuscated names of any corresponding classes.
`adaptresourcefilenames` ['file_filter']
  Rename the specified resource files, based on the obfuscated names of the corresponding class files.
`adaptresourcefilecontents` ['file_filter']
  Update the contents of the specified resource files, based on the obfuscated names of the processed classes.
`dontpreverify`
  Don't preverify the processed class files if they are targeted at Java Micro Edition or at Java 6 or higher.
`microedition`
  Target the processed class files at Java Micro Edition.
`verbose`
  Write out some more information during processing.
`dontnote` 'class_filter'
  Don't print notes about classes matching the specified class name filter.
`dontwarn` 'class_filter'
  Don't print warnings about classes matching the specified class name filter. Only use this option if you know what you're doing!
`ignorewarnings`
  Print warnings about unresolved references, but continue processing anyhow. Only use this option if you know what you're doing!
`printconfiguration` [file]
  Write out the entire configuration in traditional ProGuard style, to the standard output or to the given file. Useful to replace unreadable XML configurations.
`dump` [file]
  Write out the internal structure of the processed class files, to the standard output or to the given file.

## Class Paths

Class paths are specified as Gradle file collections, which means they can be specified as simple strings, with `files(Object)`, etc.

In addition, they can have ProGuard-style filters, specified as comma-separated named arguments after the file:

`filter:` 'file_filter'
  An optional filter for all class file names and resource file names that are encountered.
`apkfilter:` 'file_filter'
  An optional filter for all apk names that are encountered.
`jarfilter:` 'file_filter'
  An optional filter for all jar names that are encountered.
`aarfilter:` 'file_filter'
  An optional filter for all aar names that are encountered.
`warfilter:` 'file_filter'
  An optional filter for all war names that are encountered.
`earfilter:` 'file_filter'
  An optional filter for all ear names that are encountered.
`zipfilter:` 'file_filter'
  An optional filter for all zip names that are encountered.

## Files

Files are specified as Gradle files, which means they can be specified as simple strings, as File instances, with `file(Object)`, etc.

In Gradle, file names (any strings really) in double quotes can contain properties or code inside `${...}`. These are automatically expanded.

For example, `"${System.getProperty('java.home')}/lib/rt.jar"` is expanded to something like `'/usr/local/java/jdk/jre/lib/rt.jar'`. Similarly, `System.getProperty('user.home')` is expanded to the user's home directory, and `System.getProperty('user.dir')` is expanded to the current working directory.

## Keep Modifiers

The keep settings can have the following named arguments that modify their behaviors:

`includedescriptorclasses:` boolean (default = false)
  Specifies whether the classes of the fields and methods specified in the keep tag must be kept as well.
`allowshrinking:` boolean (default = false)
  Specifies whether the entry points specified in the keep tag may be shrunk.
`allowoptimization:` boolean (default = false)
  Specifies whether the entry points specified in the keep tag may be optimized.
`allowobfuscation:` boolean (default = false)
  Specifies whether the entry points specified in the keep tag may be obfuscated.

Names arguments are comma-separated, as usual.

## Class Specifications

A class specification is a template of classes and class members (fields and methods). There are two alternative ways to specify such a template:

1. As a string containing a ProGuard-style class specification. This is the most compact and most readable way. The specification looks like a Java declaration of a class with fields and methods. For example:

       keep 'public class mypackage.MyMainClass {  \
           public static void main(java.lang.String[]);  \
       }'

2. As a Gradle-style setting: a method calls with named arguments and a closure. This is more verbose, but it might be useful for programmatic specifications. For example:

       keep access: 'public',
            name:   'mypackage.MyMainClass', {
           method access:     'public static',
                  type:       'void',
                  name:       'main',
                  parameters: 'java.lang.String[]'
       }

The ProGuard-style class specification is described on the traditional Usage page.

A Gradle-style class specification can have the following named arguments:

`access:` 'access_modifiers'
  The optional access modifiers of the class. Any space-separated list of "public", "final", and "abstract", with optional negators "!".
`annotation:` 'annotation_name'
  The optional fully qualified name of an annotation of the class, with optional wildcards.
`type:` 'type'
  The optional type of the class: one of "class", "interface", or "!interface".
`name:` 'class_name'
  The optional fully qualified name of the class, with optional wildcards.
`extendsannotation:` 'annotation_name'
  The optional fully qualified name of an annotation of the the class that the specified classes must extend, with optional wildcards.
`'extends':` 'class_name'
  The optional fully qualified name of the class the specified classes must extend, with optional wildcards.
`'implements':` 'class_name'
  The optional fully qualified name of the class the specified classes must implement, with optional wildcards.

The named arguments are optional. Without any arguments, there are no constraints, so the settings match all classes.

### Gradle-style Class Member Specifications

The closure of a Gradle-style class specification can specify class members with these settings:

`field` field_constraints
  Specifies a field.
`method` method_constraints
  Specifies a method.
`constructor` constructor_constraints
  Specifies a constructor.

A class member setting can have the following named arguments to express constraints:

`access:` 'access_modifiers'
  The optional access modifiers of the class. Any space-separated list of "public", "protected", "private", "static", etc., with optional negators "!".
`'annotation':` 'annotation_name'
  The optional fully qualified name of an annotation of the class member, with optional wildcards.
`type:` 'type'
  The optional fully qualified type of the class member, with optional wildcards. Not applicable for constructors, but required for methods for which the `parameters` argument is specified.
`name:` 'name'
  The optional name of the class member, with optional wildcards. Not applicable for constructors.
`parameters:` 'parameters'
  The optional comma-separated list of fully qualified method parameters, with optional wildcards. Not applicable for fields, but required for constructors, and for methods for which the `type` argument is specified.

The named arguments are optional. Without any arguments, there are no constraints, so the settings match all constructors, fields, or methods.

A class member setting doesn't have a closure.

## Alternative: imported Ant task

Instead of using the Gradle task, you could also integrate the Ant task in your Gradle build file:

    ant.project.basedir = '../..'

    ant.taskdef(resource: 'proguard/ant/task.properties',
                classpath: '/usr/local/java/proguard/lib/proguard.jar')

Gradle automatically converts the elements and attributes to Groovy methods, so converting the configuration is essentially mechanical. The one-on-one mapping can be useful, but the resulting configuration is more verbose. For instance:

    task proguard << {
      ant.proguard(printmapping: 'proguard.map',
                   overloadaggressively: 'on',
                   repackageclasses: '',
                   renamesourcefileattribute: 'SourceFile') {

        injar(file: 'application.jar')
        injar(file: 'gui.jar', filter: '!META-INF/**')

        .....
      }
    }