简体   繁体   中英

How to prevent proguard from modifying the fields in my method

How do I prevent proguard from modifying the field names in my method?

I know we can use -keep , -keepclassmembers ..etc but these seems to only preserve the member names and NOT the variables that are used inside.

Here's what happened:

Before:

private static URL getPluginImageURL(Object plugin, String name) throws Exception {
    // try to work with 'plugin' as with OSGI BundleContext
    try {
        Class<?> BundleClass = Class.forName("org.osgi.framework.Bundle"); //$NON-NLS-1$
        Class<?> BundleContextClass = Class.forName("org.osgi.framework.BundleContext"); //$NON-NLS-1$
        if (BundleContextClass.isAssignableFrom(plugin.getClass())) {
            Method getBundleMethod = BundleContextClass.getMethod("getBundle", new Class[0]); //$NON-NLS-1$
            Object bundle = getBundleMethod.invoke(plugin, new Object[0]);
            //
            Class<?> PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
            Constructor<?> pathConstructor = PathClass.getConstructor(new Class[]{String.class});
            Object path = pathConstructor.newInstance(new Object[]{name});
            //
            Class<?> IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
            Class<?> PlatformClass = Class.forName("org.eclipse.core.runtime.Platform"); //$NON-NLS-1$
            Method findMethod = PlatformClass.getMethod("find", new Class[]{BundleClass, IPathClass}); //$NON-NLS-1$
            return (URL) findMethod.invoke(null, new Object[]{bundle, path});
        }
    } catch (Throwable e) {
        // Ignore any exceptions
    }
    // else work with 'plugin' as with usual Eclipse plugin
    {
        Class<?> PluginClass = Class.forName("org.eclipse.core.runtime.Plugin"); //$NON-NLS-1$
        if (PluginClass.isAssignableFrom(plugin.getClass())) {
            //
            Class<?> PathClass = Class.forName("org.eclipse.core.runtime.Path"); //$NON-NLS-1$
            Constructor<?> pathConstructor = PathClass.getConstructor(new Class[]{String.class});
            Object path = pathConstructor.newInstance(new Object[]{name});
            //
            Class<?> IPathClass = Class.forName("org.eclipse.core.runtime.IPath"); //$NON-NLS-1$
            Method findMethod = PluginClass.getMethod("find", new Class[]{IPathClass}); //$NON-NLS-1$
            return (URL) findMethod.invoke(plugin, new Object[]{path});
        }
    }
    return null;
}

After proguard:

private static URL getPluginImageURL(Object plugin, String name)
throws Exception
{
try
{
  localClass1 = Class.forName("org.osgi.framework.Bundle");
  localClass2 = Class.forName("org.osgi.framework.BundleContext");
  if (localClass2.isAssignableFrom(plugin.getClass())) {
    localObject1 = localClass2.getMethod("getBundle", new Class[0]);
    localObject2 = ((Method)localObject1).invoke(plugin, new Object[0]);

    localClass3 = Class.forName("org.eclipse.core.runtime.Path");
    localObject3 = localClass3.getConstructor(new Class[] { String.class });
    Object localObject4 = ((Constructor)localObject3).newInstance(new Object[] { name });

    Class localClass4 = Class.forName("org.eclipse.core.runtime.IPath");
    Class localClass5 = Class.forName("org.eclipse.core.runtime.Platform");
    Method localMethod = localClass5.getMethod("find", new Class[] { localClass1, localClass4 });
    return (URL)localMethod.invoke(null, new Object[] { localObject2, localObject4 });
  }
}
catch (Throwable localThrowable)
{
  Class localClass2;
  Object localObject1;
  Object localObject2;
  Class localClass3;
  Object localObject3;
  Class localClass1 = Class.forName("org.eclipse.core.runtime.Plugin");
  if (localClass1.isAssignableFrom(plugin.getClass()))
  {
    localClass2 = Class.forName("org.eclipse.core.runtime.Path");
    localObject1 = localClass2.getConstructor(new Class[] { String.class });
    localObject2 = ((Constructor)localObject1).newInstance(new Object[] { name });

    localClass3 = Class.forName("org.eclipse.core.runtime.IPath");
    localObject3 = localClass1.getMethod("find", new Class[] { localClass3 });
    return (URL)((Method)localObject3).invoke(plugin, new Object[] { localObject2 });
  }
}
return null;

}

Notice how localObject1 and localObject2 no longer have Class declaration? Doesn't this make the code syntactically incorrect?

Help...

It's your decompiler that produces the syntactically incorrect code. You could try a different decompiler.

ProGuard indeed removes the local variable names, since they are just optional debugging information. They are not required by the virtual machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM