Home | History | Annotate | Download | only in gradle
      1 //
      2 // This Gradle build file illustrates how to process a program
      3 // library, such that it remains usable as a library.
      4 // Usage:
      5 //     gradle -b library.gradle proguard
      6 //
      7 
      8 // Tell Gradle where to find the ProGuard task.
      9 
     10 buildscript {
     11     repositories {
     12         flatDir dirs: '../../lib'
     13     }
     14     dependencies {
     15         classpath ':proguard'
     16     }
     17 }
     18 
     19 // Define a ProGuard task.
     20 
     21 task proguard(type: proguard.gradle.ProGuardTask) {
     22 
     23     // You should probably import a more compact ProGuard-style configuration
     24     // file for all static settings, but we're specifying them all here, for
     25     // the sake of the example.
     26     //configuration 'configuration.pro'
     27 
     28     // Specify the input jars, output jars, and library jars.
     29     // In this case, the input jar is the program library that we want to process.
     30 
     31     injars  'in.jar'
     32     outjars 'out.jar'
     33 
     34     libraryjars  "${System.getProperty('java.home')}/lib/rt.jar"
     35 
     36     // Save the obfuscation mapping to a file, so we can de-obfuscate any stack
     37     // traces later on. Keep a fixed source file attribute and all line number
     38     // tables to get line numbers in the stack traces.
     39     // You can comment this out if you're not interested in stack traces.
     40 
     41     printmapping 'out.map'
     42     keepparameternames
     43     renamesourcefileattribute 'SourceFile'
     44     keepattributes 'Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,EnclosingMethod'
     45 
     46     // Preserve all annotations.
     47 
     48     keepattributes '*Annotation*'
     49 
     50     // Preserve all public classes, and their public and protected fields and
     51     // methods.
     52 
     53     keep 'public class * { \
     54         public protected *; \
     55     }'
     56 
     57     // Preserve all .class method names.
     58 
     59     keepclassmembernames 'class * { \
     60         java.lang.Class class$(java.lang.String); \
     61         java.lang.Class class$(java.lang.String, boolean); \
     62     }'
     63 
     64     // Preserve all native method names and the names of their classes.
     65 
     66     keepclasseswithmembernames includedescriptorclasses:true, 'class * { \
     67         native <methods>; \
     68     }'
     69 
     70     // Preserve the special static methods that are required in all enumeration
     71     // classes.
     72 
     73     keepclassmembers allowshrinking:true, 'enum * { \
     74         public static **[] values(); \
     75         public static ** valueOf(java.lang.String); \
     76     }'
     77 
     78     // Explicitly preserve all serialization members. The Serializable interface
     79     // is only a marker interface, so it wouldn't save them.
     80     // You can comment this out if your library doesn't use serialization.
     81     // If your code contains serializable classes that have to be backward
     82     // compatible, please refer to the manual.
     83 
     84     keepclassmembers 'class * implements java.io.Serializable { \
     85         static final long serialVersionUID; \
     86         static final java.io.ObjectStreamField[] serialPersistentFields; \
     87         private void writeObject(java.io.ObjectOutputStream); \
     88         private void readObject(java.io.ObjectInputStream); \
     89         java.lang.Object writeReplace(); \
     90         java.lang.Object readResolve(); \
     91     }'
     92 
     93     // Your library may contain more items that need to be preserved; 
     94     // typically classes that are dynamically created using Class.forName:
     95 
     96     // keep 'public class mypackage.MyClass'
     97     // keep 'public interface mypackage.MyInterface'
     98     // keep 'public class * implements mypackage.MyInterface'
     99 }
    100