Proguard、Android 和抽象类实例化
“抽象类实例化,”你说。 “不可能的!”
这是我的代码:
public static AbstractFoo getAbstractFoo(Context context) {
try {
Class<?> klass = Class
.forName("com.bat.baz.FooBar");
Constructor<?> constructor = klass.getDeclaredConstructor(
String.class, String.class);
constructor.setAccessible(true);
AbstractFoo foo = (AbstractFoo) constructor.newInstance(
"1", "2");
return foo;
} catch (ClassNotFoundException e) {
// Ignore
} catch (NoSuchMethodException e) {
throw new InflateException(
"No matching constructor");
} catch (IllegalAccessException e) {
throw new InflateException("Could not create foo", e);
} catch (InvocationTargetException e) {
throw new InflateException("Could not create foo", e);
} catch (InstantiationException e) {
throw new InflateException("Could not create foo", e);
}
return null;
}
com.bat.baz.FooBar 是一个扩展 AbstractFoo 的私有类。如果没有 Proguard,此代码可以在我的 Android 设备上运行。有了它,它就会在 NoSuchMethodException try/catch 上失败。
我继续向我的 Proguard 配置文件添加语句,例如
-keep public abstract class AbstractFoo
{public *;protected *; private *;}
但这并不能解决问题。我怎样才能让 Proguard 接受这是实例化 AbstractFoo 对象的有效方法?至少,如果它在没有 Proguard 的情况下也能工作,那么它应该可以与它一起工作。
"Abstract class instantiation," you say. "Impossible!"
Here's my code:
public static AbstractFoo getAbstractFoo(Context context) {
try {
Class<?> klass = Class
.forName("com.bat.baz.FooBar");
Constructor<?> constructor = klass.getDeclaredConstructor(
String.class, String.class);
constructor.setAccessible(true);
AbstractFoo foo = (AbstractFoo) constructor.newInstance(
"1", "2");
return foo;
} catch (ClassNotFoundException e) {
// Ignore
} catch (NoSuchMethodException e) {
throw new InflateException(
"No matching constructor");
} catch (IllegalAccessException e) {
throw new InflateException("Could not create foo", e);
} catch (InvocationTargetException e) {
throw new InflateException("Could not create foo", e);
} catch (InstantiationException e) {
throw new InflateException("Could not create foo", e);
}
return null;
}
com.bat.baz.FooBar is a private class that extends AbstractFoo. Without Proguard, this code runs on my Android device. With it, it fails on the NoSuchMethodException try/catch.
I continue to add statements to my Proguard config file like
-keep public abstract class AbstractFoo
{public *;protected *; private *;}
But that doesn't solve the problem. How can I get Proguard to accept that this is a valid way to instantiate the AbstractFoo object? At least, if it works without Proguard, it should work with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ProGuard 正在从代码中删除构造函数,因为它似乎未使用。 ProGuard 无法检测到构造函数是通过反射调用的。因此,您必须明确保留它:
请注意,您可能有更好的机会在 ProGuard 的帮助论坛上回答此类问题。
ProGuard is removing the constructor from the code, because it appears unused. ProGuard can't detect that the constructor is being called by reflection. So, you have to keep it explicitly:
Please note that you may have better chances with questions like these on ProGuard's help forum.
您是否还添加语句以将内容保留在 FooBar 中?
您可以使用“dexdump”或“dexlist”查看实际生成到 .dex 文件中的类和方法的列表。比较使用和不使用 proguard 所得到的结果可能会很有启发。
Are you also adding statements to keep the stuff in FooBar?
You can use "dexdump" or "dexlist" to see the list of classes and methods that are actually generated into the .dex file. Comparing what you get with and without proguard could be illuminating.