Search code examples
javagradleproguardjava-platform-module-system

How to prevent proguard from removing "module-info.class" from the output jar?


I'm upgrading a Java desktop application to JDK10 and need to leverage modules to use the javapackager to build a native package.

Everything was working great until I added an obfuscation step using Proguard (6.0.2). Once I enabled obfuscation (using a working proguard configuration file from the < JDK9 project) it works as expected but Proguard removes the module-info.class from the output JAR which prevents javapackager from finding the module.

According to Proguard's documentation for the injars parameter

By default, any non-class files will be copied without changes.

The problem here is that module-info.class is a "class" file (albeit a weird one). The "keep" rules depend on class names so I don't think there is any rule I can use to prevent this removal.

How can I get Proguard to keep the module-info.class file?


Solution

  • After filing a bug report, Proguard's author confirmed that we need to treat module-info as just another class and add a rule to keep it, like this:

    -keep class module-info

    and possibly using

    -keepattributes Module*

    In my case, I actually had tried it before posting the question but it didn't work for me on the project I was using it with (I got a NullPointerException when I tried it, more details in the bug report)

    After the author's response, I tried it on a simpler project and the "keep" rule above worked fine so it meant that something specific to my first project was causing the issue.

    I worked around the issue in that project for now by using gradle to copy the module-info.class from the unobfuscated jar into the obfuscated one. It's not pretty but it works.

    Hope this helps anyone in the same situation.