Search code examples
javaeclipseproguardobfuscation

ProGuard doesn't like anonymous classes inside other anonymous classes after latest Eclipse update


I have encountered a strange issue with ProGuard that it seems people have encountered before in slightly different contexts (e.g. Android, not related to Eclipse, etc.), but I have not found a satisfactory solution to this issue in my context.

When attempting to obfuscate a .jar file with ProGuard (version 7.4.1), it gives me the following error: "Warning: CompiledClassName: can't find enclosing method 'method name' in program class CompiledClassName. Warning: there were 2 unresolved references to program class members. Your input classes appear to be inconsistent. You may need to recompile the code. (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)"

That hyperlink included in the error doesn't provide much help, except to say that this is an issue with the compiled class files. After some digging, I have found that ProGuard is complaining because, at one place in the code, I have an anonymous class inside of another anonymous class. It must now suddently find this problematic, but it didn't before.

So, here is the strange part: I have been using ProGuard to obfuscate this program for years and have never had this issue, and the offending anonymous class has been in this code also for many years and also obfuscated successfully until now. I even went back to older versions of ProGuard (as far back as 5.2.1) and I still get the same error. Telling ProGuard to ignore warnings does let is bypass the error, but it doesn't solve it, and I would rather not have ProGuard always ignoring all warnings.

I looked into other questions on this platform, namely here and here, as well as other pages on Guardsquare's forum, but none of those seem to have a solution besides things that I tried that did not seem to resolve the issue (various attempts at using the -keep command or -keepattributes command). I don't believe the issue is with my ".pro" configuration file because it used to obfuscate just fine before.

What is perplexing is that nothing in this code has changed from when it did work to when it didn't. Nothing has changed with regard to the ".pro" configuration file or even the version of ProGuard being used. The only thing that appears to have changed is that I updated Eclipse from version 2023-09 to version 2023-12. So, I asked a couple colleagues to run this exact same ProGuard script on their machines with the exact same code. My colleague who has Eclipse 2023-12 installed got the same error I am getting. However, a colleague who has an older version of Eclipse installed (2021-12) had the obfuscation script complete without error/warning.

That makes me think that the update of Eclipse is somehow, strangely, inexplicably, relevant to what is happening here. I wouldn't have thought the Java Runtime or the way the code is being compiled should have changed, but at this point it's an odd correlation that is evading discovery of the cause.

Apart from the code I'm actually trying to obfuscate, here is a minimum viable example of code that now refuses to obfuscate that did obfuscate before in older versions of Eclipse:

public class Main {

public static void main(String[] args) {
    Main main = new Main();
    main.run();
}

public void run() {
    FirstClass fc = new FirstClass() {
        SecondClass sc = new SecondClass() {
            @Override
            public void print() {
                System.out.println("Second Class Override.");
            }
        };
        
        @Override
        public void print() {
            System.out.println("First Class Override.");
            sc.print();
        }
    };
    fc.print();
}

public class FirstClass {
    public void print() {
        System.out.println("First Class");
    }
}

public class SecondClass {
    public void print() {
        System.out.println("Second Class");
    }
}

Again, I realize that I can just "ignore all warnings" and it works fine, but since the ProGuard ".pro" configuration file is a tracked file, and since we don't want to miss potential other warnings, it is not ideal to keep changing that line every time I want to build and obfuscate a .jar file. I could also rewrite the class to not have nested anonymous classes, but something feels wrong about having to refactor code just because ProGuard suddenly doesn't like it after an Eclipse update.


Solution

  • Well, it appears this may have been an issue with Eclipse version 2023-12 and not with ProGuard or my configuration. I have just updated to the latest 2024-03 version of Eclipse, and the problem does not occur anymore. Even using ProGuard 5.2.1, the project builds and obfuscates fine with no warnings about anonymous classes that have to be ignored for the obfuscation to complete successfully.