开发者

java proguard: library doesnt work after optimization

开发者 https://www.devze.com 2022-12-11 01:02 出处:网络
i am optimizing a jar with proguard, but it crashes after optimization. here is my proguard task: <proguard>

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!


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.


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?


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/*
0

精彩评论

暂无评论...
验证码 换一张
取 消