Proguard、Android 和抽象类实例化

发布于 2024-09-29 23:48:45 字数 1393 浏览 5 评论 0原文

“抽象类实例化,”你说。 “不可能的!”

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-10-06 23:48:45

ProGuard 正在从代码中删除构造函数,因为它似乎未使用。 ProGuard 无法检测到构造函数是通过反射调用的。因此,您必须明确保留它:

-keepclassmembers class com.bat.baz.FooBar {
  <init>(java.lang.String, java.lang.String);
}

请注意,您可能有更好的机会在 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:

-keepclassmembers class com.bat.baz.FooBar {
  <init>(java.lang.String, java.lang.String);
}

Please note that you may have better chances with questions like these on ProGuard's help forum.

匿名。 2024-10-06 23:48:45

您是否还添加语句以将内容保留在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文