'Obfuscate Class Names With Main Methods

I'm using ProGuard to obfuscate executable .jar files. When I decompile the code using Procyon the classes with main methods still have their original names. This is due to

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

in the default configuration.

If I remove this, ProGuard won't process. Is there a way to obfuscate class names with main methods as well or is there a good reason against it?



Solution 1:[1]

If you obfuscate the classname with the main method, you can no longer call that class to run the jar.

In theory, you could modify the MANIFEST.MF in the jar to refer to the obfuscated classname, but I'm not sure the benefit of that, since it is pretty clear what you are calling at that point.

Further, you can never obfuscate the main(String[]) method name itself, or java can't find and run your application at all. That's a pretty good reason against it :)

If you want to obfuscate the rest of the class members, but keep the classname and main method itself, you can do that with

-keep public class mypackage.MyMain {
    public static void main(java.lang.String[]);
}

as per the first example in proguard manual.

Solution 2:[2]

Add allowobfuscation to rename your class and keepclassmembers to keep main method name:

-keep,allowobfuscation public class * { public static void main(java.lang.String[]); }
-keepclassmembers public class * { public static void main(java.lang.String[]); }

And remember to -adaptresourcefilecontents also to make your JAR runnable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben
Solution 2