java proguard:优化后库不起作用

发布于 2024-08-10 05:55:03 字数 979 浏览 5 评论 0原文

我正在用 proguard 优化一个 jar,但优化后它崩溃了。 这是我的 proguard 任务:

    <proguard>
        -injars     ${dist}/${jarname}
        -outjars    ${dist}-proguard/${jarname}

        -target 5

        -libraryjars '${java.home}/lib/rt.jar'

        -dontobfuscate            
        -optimizationpasses 4
        -overloadaggressively
        -repackageclasses ''
        -allowaccessmodification

        -keep public class * {
            public static void main(java.lang.String[]);
        }
    </proguard>

只要我输入 -dontoptimize 选项,它就会起作用。

根据异常堆栈,当使用空指针访问类的静态公共成员时,它会崩溃。这是代码:

public static Texture ring, dust, spikering, thinring, crystal, clouds;

public static void init() {
    Field [] fields = TexturePool.class.getDeclaredFields();

    for (Field field : fields) {
        if(field.getType() == Texture.class) {
            field.set( null, /*imagine new object here*/ );
        }
    }
}

谢谢!

i am optimizing a jar with proguard, but it crashes after optimization.
here is my proguard task:

    <proguard>
        -injars     ${dist}/${jarname}
        -outjars    ${dist}-proguard/${jarname}

        -target 5

        -libraryjars '${java.home}/lib/rt.jar'

        -dontobfuscate            
        -optimizationpasses 4
        -overloadaggressively
        -repackageclasses ''
        -allowaccessmodification

        -keep public class * {
            public static void main(java.lang.String[]);
        }
    </proguard>

as soon as i put in the -dontoptimize option, it works.

according to the stack of the exception it crashes when accessing a static public member of a class with a nullpointer. here is the code:

public static Texture ring, dust, spikering, thinring, crystal, clouds;

public static void init() {
    Field [] fields = TexturePool.class.getDeclaredFields();

    for (Field field : fields) {
        if(field.getType() == Texture.class) {
            field.set( null, /*imagine new object here*/ );
        }
    }
}

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

渡你暖光 2024-08-17 05:55:03

好吧,我自己才发现。我认为优化完全优化了类成员,因为它们在此类中不是直接访问的。如果我指定该选项:

        -keepclassmembers public class com.package.** {
            public static * ;
        }

即使经过优化它也可以工作。

ok, i just found out myself. i think the optimization completely optimized that classmembers away, since they are not directly accessed in this class. if i specify the option:

        -keepclassmembers public class com.package.** {
            public static * ;
        }

it works even with optimization.

夜未央樱花落 2024-08-17 05:55:03

根据 减少大小的最佳 Java 混淆应用程序

“我一直能够通过不使用 Proguard 参数“-overloadaggressively”来解决问题。

也许你也应该尝试一下?


编辑:问题很可能是分配被优化掉了。源代码中定义字段的初始化实际上是由编译器在静态代码块中完成的。看来优化可以解决这个问题。更少的优化次数会发生什么?

According to Best Java Obfuscation Application For Size Reduction:

"I was always able to fix the problem by not using the Proguard argument "-overloadaggressively"."

Perhaps you should try the same?


EDIT: The problem could easily be that an assignment is optimized away. The initializations happening in the source code, where a field is defined, is actually done by the compiler in a static code blokc. Appears that the optimizations tinker with that. What happens with fewer optimization passes?

℡寂寞咖啡 2024-08-17 05:55:03

我在 ProGuard 优化仅使用反射 API 修改的类字段时遇到了同样的问题。然而,建议的答案对我来说不起作用,因为代码库中分散了太多的类来指定类过滤器。

相反,禁用字段优化对我来说起到了作用:

-optimizations !field/*

I had the same issue with ProGuard optimizing away class fields that were modified using reflection API only. However, the suggested answer didn't work for me as there were too many classes scattered throughout the code base to specify class filter.

Instead, disabling field optimization did the trick for me:

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