obfuscation with proguard vs. java.lang.reflect.Pr

2019-06-13 18:07发布

问题:

I use for debugging reasons the java.lang.reflect.Proxy stuff to have a generic way to implement all possible interfaces... but this seems to be difficult to get it working with proguard. Any suggestions?

THX -Marco

public class DebugLogListenerFactory {

public static IAirplaneListenerAll createStreamHandle(ICAirplane plane) {
    DebugLogListenerHandler handler = new DebugLogListenerHandler(plane);
    IAirplaneListenerAll proxy = (IAirplaneListenerAll) Proxy
            .newProxyInstance(IAirplaneListenerAll.class.getClassLoader(),
                    new Class[] { IAirplaneListenerAll.class }, handler);

    plane.addListener(proxy);
    return proxy;
}

private static class DebugLogListenerHandler implements InvocationHandler {


    private final Level levDef = Level.FINE;


    public DebugLogListenerHandler() {
    }

    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println("invoked" + method);
        String methodName = method.getName();
        String msg = methodName + ": ";
        if (args != null) {
            boolean first = true;
            for (Object o : args) {
                if (first) {
                    first = false;
                } else {
                    msg += " ,";
                }
                msg += o.toString();
            }
        }
        CDebug.getLog().log(levDef, msg);
        return null;
    }
}

}

回答1:

The easiest solution is probably to avoid shrinking/optimizing/obfuscating the interface and its methods:

-keep interface some.package.IAirplaneListenerAll {
  <methods>;
}

You might allow shrinking:

-keep,allowshrinking interface some.package.IAirplaneListenerAll {
  <methods>;
}

If the InvocationHandler can deal with obfuscated method names, you might also allow obfuscation:

-keep,allowshrinking,allowobfuscation interface some.package.IAirplaneListenerAll {
  <methods>;
}