java proguard 使用,java ProGuard删除(收缩)未使用的类

Let's say I have this Java app:

package com.site;

public class MyAppBase {}

package com.site.free;

import com.site.MyAppBase;

public class MyApp extends MyAppBase {}

package com.site.pro;

import com.site.MyAppBase;

public class MyApp extends MyAppBase {}

package com.site;

public class Edition

{

public static final int FREE = 1;

public static final int PRO = 2;

private static final int EDITION = PRO;

public static boolean is(final int edition)

{

return (EDITION == edition);

}

}

package com.site;

public class EditionFactory

{

public static MyAppBase get_app()

{

MyAppBase ret = null;

if (Edition.is(Edition.FREE))

ret = new com.site.free.MyApp();

else if (Edition.is(Edition.PRO))

ret = new com.site.pro.MyApp();

return ret;

}

}

Now, any combination of ProGuard configuration I'm trying to get rid of the non-selected edition (in this case it's FREE) doesn't work.

By "getting rid of" I mean make the actual class disappear (as well from the calling code).

In other words, a call like this:

final MyAppBase app = EditionFactory.get_app();

.. is currently being translated to, after ProGuarding it, this:

if (a.a(1))

localObject5 = new c(); // <<< FREE

else if (a.a(2))

localObject5 = new d(); // <<< PRO

.. while I'd wish it to be translated to this:

localObject5 = new d(); // <<< PRO only in the code (as set at Edition.EDITION)

Bottom line is (besides the fact that ProGuard is GREAT!!), I can't seem to make it "see through" and understand that Edition.is() is a boolean function returning a constant which makes it ok to remove some classes.

I've tried configurations like:

-keep,allowshrinking,allowoptimization public class * extends com.site.MyAppBase

-optimizationpasses 10

.. nothing works.

On the other hand, if I refer to Edition.EDITION and comparing it inlined (i.e without any "proxy" functions), the Java compiler (v1.6) detects it and remove the whole reference to the non-selected edition/class.

This results in that ProGuard removes/shrinks the unused class which is great.

The only issue here is about maintaining - I'd be happy to keep being able to use the EditionFactory style.

解决方案

The optimization isn't performed because ProGuard decides not to inline the method Edition#is, so it then can't simplify the resulting series of instructions. The method is not inlined because it is not very short and it is also invoked more than once. You could work around the first criterion with this undocumented JVM option for ProGuard:

-Dmaximum.inlined.code.length=16

Alternatively, you could work around the second criterion, by making sure the method is only invoked once:

return Edition.is(Edition.FREE) ?

new com.site.free.MyApp() :

new com.site.pro.MyApp();

Alternatively, you could probably create short functions isFree() and isPro(), because they would return constants, which would be inlined.

It's a good practice to check the processed code if you're expecting some particular optimizations, because they are often subject to complex constraints.

Nice to hear that you like ProGuard.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值