com.apple.eawt.Application not working after obfus

2019-07-09 01:49发布

问题:

I have written an application in Java for both Mac OSx and Windows, but I've developed it on Windows. Therefore I used java.lang.reflect.* in order to implement the 'About' and 'Quit' handler without receiving exceptions.

I've exported it as an Executable Jar and everything works fine on both operating systems. However, I want to obfuscate the application and I'm using ProGaurd to do so. I've specified the necessary libraries for it to work on Windows (rt.jar, jsse.jar, jce.jar) and it still does work fine on Windows, however ProGaurd is telling me that there is two unresolved dynamic references to classes or interfaces, and the obfuscated application no longer works properly on Mac (the About and Quit Handler don't work).

I read somewhere that the ui.jar was required, so I copied it accross from my Mac and specified it in ProGaurd but that did not seem to solve the problem! Any solutions appreciated then...

Thanks in advance

UPDATE

I have now used AppleJavaExtensions in order to implement the handlers using com.apple.eawt.Application rather than reflect. However, I'm still getting the exact same problem! Here is the code that I am currently using:

package ap;
import com.apple.eawt.AboutHandler;
import com.apple.eawt.AppEvent;
import com.apple.eawt.AppEvent.QuitEvent;
import com.apple.eawt.Application;
import com.apple.eawt.QuitHandler;
import com.apple.eawt.QuitResponse;
import com.gui.Tabs;

public class xa implements AboutHandler, QuitHandler {

    // Constructor to register/install the necessary handler's
    public xa(){
        Application.getApplication().setAboutHandler(this);
        Application.getApplication().setQuitHandler(this);
    }

    // Implemented method to catch the About menu item
    @Override
    public void handleAbout(AppEvent.AboutEvent e) {
        Tabs.switchAbout();

    }

    // Implemented method to catch the Quit menu item
    @Override
    public void handleQuitRequestWith(QuitEvent arg0, QuitResponse arg1) {
        Tabs.quit();        
    }
}

AND I am using the ProGuard GUI to obfuscate my application. In Obsfucation 'tab' at the bottom I've added class ap.xa and specified that it extends/implements AboutHandler, QuitHandler. I've also added the Class com.gui.Tabs but it the problem still occurs.

回答1:

Maybe you could try Apple Java Extensions.

Using the Apple Java Extensions, you could develop on Windows without using java.lang.reflect.* classes, and maybe it will be easier for ProGuard.



回答2:

Would it help if you tell ProGuard not to obfuscate the classes that implement the About and Quit handlers?



回答3:

Try the -dontoptimize option. Some methods are removed when they have "no effect." And executing ProGuard on Windows... for Mac. Or a dumb alternative: try obfuscating on a Mac and deliver two versions.



回答4:

Opps, I've solved the problem... And I only made a silly mistake

Because I'm using the ProGuard GUI, I failed to see the "Class members" part in the addition screen. Therefore, whilst ProGuard wasn't obfuscating the class itself, it was obfuscating the methods, hence the listeners not working. So, I've ended up 'keeping' ap.xa and all it's methods (the class containing the actually listeners), and com.apple.** (the JavaAppleExtensions package)

However, there is still one thing occurring. When I run the obfuscated JAR file from Terminal I receive the following output:

Andrews-MacBook-Pro:~ Andy$ java -jar /Users/Andy/Desktop/YLIT\ Final\ 2.jar
Jun  9 20:50:29 Andrews-MacBook-Pro.local java[959] <Error>: CGContextGetCTM: invalid context 0x0
Jun  9 20:50:29 Andrews-MacBook-Pro.local java[959] <Error>: CGContextSetBaseCTM: invalid context 0x0
Jun  9 20:50:29 Andrews-MacBook-Pro.local java[959] <Error>: CGContextGetCTM: invalid context 0x0
Jun  9 20:50:29 Andrews-MacBook-Pro.local java[959] <Error>: CGContextSetBaseCTM: invalid context 0x0