Ant Task

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

Before you can use the proguard task, you have to tell Ant about this new task. The easiest way is to add the following line to your build.xml file:

<taskdef resource="proguard/ant/task.properties"
         classpath="/usr/local/java/proguard/lib/proguard.jar" />

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

There are three ways to configure the ProGuard task: using an external configuration file, using embedded ProGuard configuration options, and using the equivalent XML configuration tags. These three ways can be combined, depending on practical circumstances and personal preference.

1. An external ProGuard configuration file

The simplest way to use the ProGuard task in an Ant build file is to keep your ProGuard configuration file, and include it from Ant. You can include your ProGuard configuration file by setting the configuration attribute of your proguard task. Your ant build file will then look like this:

<taskdef resource="proguard/ant/task.properties"
         classpath="/usr/local/java/proguard/lib/proguard.jar" />
<proguard configuration="myconfigfile.pro"/>

This is a convenient option if you prefer ProGuard's configuration style over XML, if you want to keep your build file small, or if you have to share your configuration with developers who don't use Ant.

2. Embedded ProGuard configuration options

Instead of keeping an external ProGuard configuration file, you can also copy the contents of the file into the nested text of the proguard task (the PCDATA area). Your Ant build file will then look like this:

<taskdef resource="proguard/ant/task.properties"
         classpath="/usr/local/java/proguard/lib/proguard.jar" />
<proguard>
  -libraryjars ${java.home}/lib/rt.jar
  -injars      in.jar
  -outjars     out.jar

  -keepclasseswithmembers public class * {
      public static void main(java.lang.String[]);
  }
</proguard>

Some minor syntactical changes are required in order to conform with the XML standard.

Firstly, the # chararacter cannot be used for comments in an XML file. Comments must be enclosed by an opening <!-- and a closing -->. All occurrences of the # chararacter can be removed.

Secondly, the use of < and > characters would upset the structure of the XML build file. Environment variables are now enclosed by an opening ${ and a closing }. This syntax also allows you to use Ant properties within the ProGuard configuration. Other occurrences of < and > have to be encoded as &lt; and &gt;.

3. XML configuration tags

If you really prefer a full-blown XML configuration, you can replace the ProGuard configuration options by the equivalent XML configuration tags. The remainder of this page presents the supported tags. For a more extensive discussion of their meaning, please refer to the traditional Usage section.

 

Task Attributes and Nested Elements

The <proguard> task and the <proguardconfiguration> task can have the following attributes (only for <proguard>) and nested elements:
configuration = "filename"
Read and merge options from the given ProGuard-style configuration file.
skipnonpubliclibraryclasses = "boolean" (default = true)
Ignore non-public library classes.
skipnonpubliclibraryclassmembers = "boolean" (default = true)
Ignore package visible library class members.
printseeds = "boolean or filename" (default = false)
List classes and class members matched by the various keep commands, to the standard output or to the given file.
shrink = "boolean" (default = true)
Shrink the input class files.
printusage = "boolean or filename" (default = false)
List dead code of the input class files, to the standard output or to the given file.
optimize = "boolean" (default = true)
Optimize the input class files.
allowaccessmodification = "boolean" (default = false)
Allow the access modifiers of classes and class members to be modified, while optimizing.
obfuscate = "boolean" (default = true)
Obfuscate the input class files.
printmapping = "boolean or filename" (default = false)
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 = "filename"
Reuse the given mapping, for incremental obfuscation.
obfuscationdictionary = "filename"
Use the words in the given text file as obfuscated method names.
overloadaggressively = "boolean" (default = false)
Apply aggressive overloading while obfuscating.
defaultpackage = "package_name"
Repackage all class files that are renamed into the single given package.
usemixedcaseclassnames = "boolean" (default = true)
Generate mixed-case class names while obfuscating.
renamesourcefileattribute = "string"
Put the given constant string in the SourceFile attributes. The default is to keep the original string.
verbose = "boolean" (default = false)
Write out some more information during processing.
note = "boolean" (default = true)
Print notes about class casts of variable dynamically created objects.
warn = "boolean" (default = true)
Warn about unresolved references at all.
ignorewarnings = "boolean" (default = false)
Print warnings about unresolved references, but continue processing anyhow. The default is not to ignore warnings. Only use this option if you know what you're doing!
dump = "boolean or filename" (default = false)
Write out the internal structure of the processed class files, to the standard output or to the given file.
<injar class_path />
Specifies the program jars (or wars, ears, zips, or directories).
<outjar class_path />
Specifies the name of the output jars (or wars, ears, zips, or directories).
<libraryjar class_path />
Specifies the library jars (or wars, ears, zips, or directories).
<keep class_specification > class_member_specifications </keep>
Preserve the specified classes and class members.
<keepclassmembers class_specification > class_member_specifications </keepclassmembers>
Preserve the specified class members, if their classes are preserved as well.
<keepclasseswithmembers class_specification > class_member_specifications </keepclasseswithmembers>
Preserve the specified classes and class members, if all of the specified class members are present.
<keepnames class_specification > class_member_specifications </keepnames>
Preserve the names of the specified classes and class members (if they aren't removed in the shrinking step).
<keepclassmembernames class_specification > class_member_specifications </keepclassmembernames>
Preserve the names of the specified class members (if they aren't removed in the shrinking step).
<keepclasseswithmembernames class_specification > class_member_specifications </keepclasseswithmembernames>
Preserve the names of the specified classes and class members, if all of the specified class members are present (after the shrinking step).
<assumenosideeffects class_specification > class_member_specifications </assumenosideeffects>
Assume that the specified methods don't have any side effects, while optimizing.
<keepattribute name = "attribute_name" />
Preserve the given optional Java bytecode attribute, with optional wildcards. If no name is given, all attributes are preserved.
<configuration refid = "ref_id" />
Includes the configuration specified in the <proguardconfiguration> task (or <proguard> task) with the attribute id = "ref_id". Note that only the nested elements of this configuration are considered, not the attributes.
 

Class Path Attributes and Nested Elements

The jar tags are path tags, so they can have any of the path attributes (or nested elements). The most common attributes are:
path = "path"
The names of the jars (or wars, ears, zips, or directories), separated by the path separator.
location = "name" (or file = "name", or dir = "name", or name = "name")
Alternatively, the name of a single jar (or war, ear, zip, or directory).
refid = "ref_id"
Alternatively, a reference to the path or file set with the attribute id = "ref_id".
In addition, the jar tags can have ProGuard-style filter attributes (as described in the section on Filters):
filter = "filter"
An optional filter for all class file names and resource file names that are encountered.
jarfilter = "jar_filter"
An optional filter for all jar names that are encountered.
warfilter = "war_filter"
An optional filter for all war names that are encountered.
earfilter = "ear_filter"
An optional filter for all ear names that are encountered.
zipfilter = "zip_filter"
An optional filter for all zip names that are encountered.
 

Class Specification Attributes and Nested Elements

The keep tags can have the following class_specification attributes and class_member_specifications nested elements:
access = "access_modifiers"
The optional access modifiers of the class. Any space-separated list of "public", "final", and "abstract", with optional negators "!".
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.
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.
<field class_member_specification />
Specifies a field.
<method class_member_specification />
Specifies a method.
<constructor class_member_specification />
Specifies a constructor.
 

Class Member Specification Attributes

The class member tags can have the following class_member_specification attributes:
access = "access_modifiers"
The optional access modifiers of the class. Any space-separated list of "public", "protected", "private", "static", etc., with optional negators "!".
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 attribute 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 attribute is specified.

Copyright © 2002-2004 Eric Lafortune.