Search code examples
javaproguard

Pass parameter to Proguard configuration file


I need to pass a parameter to my proguard configuration. In this case the parameter represents the version of the program to be obfuscated. This is my configuration file:

-injars 'C:\Users\jmurray\Desktop\OBF\MyProgram\MyProgram-1.0.0-RELEASE\MyProgram-1.0.0-RELEASE.jar'
-outjars 'C:\Users\jmurray\Desktop\OBF\MyProgram\MyProgram-1.0.0-RELEASE\MyProgram-1.0.0-RELEASE-OBF.jar'

-libraryjars 'C:\Program Files\Java\jdk-11\jmods\java.base.jmod'
-libraryjars 'C:\Program Files\Java\jdk-11\jmods\java.desktop.jmod'
-libraryjars 'C:\Program Files\Java\jdk-11\jmods\java.datatransfer.jmod'
-libraryjars 'C:\Users\jmurray\Desktop\OBF\MyProgram\MyProgram-1.0.0-RELEASE\lib'

-dontusemixedcaseclassnames
-dontnote
-dontwarn

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------

-keep public class com.jmurray.MyProgram.main.* {
    public <fields>;
    public <methods>;
}

As you can see the version (1.0.0-RELEASE) is hardcoded into the configuration file. I need to pass it from outside


Solution

  • According to the documentation for ProGuard, you can use Java System variables.

    -libraryjars  <java.home>/jmods/java.base.jmod
    

    Note the use of the <java.home> system property. ProGuard automatically replaces it when parsing the file.

    So in your case, you can create <library.version>

    And then make sure it is set as a JVM System Variable:

    java -Dlibrary.version=1234
    

    https://www.guardsquare.com/manual/configuration/examples