java ProGuard删除(收缩)未使用的类 [英] java ProGuard remove (shrink) unused classes

查看:263
本文介绍了java ProGuard删除(收缩)未使用的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个Java应用程序:

  package com.site; 

公共类MyAppBase {}






  package com.site.free; 

import com.site.MyAppBase;

公共类MyApp扩展MyAppBase {}






  package com.site.pro; 

import com.site.MyAppBase;

公共类MyApp扩展MyAppBase {}






  package com.site; 

公共类版本
{

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();

返回ret;
}
}






现在,ProGuard配置的任何组合我试图摆脱未选择的版本(在这种情况下它是免费的)不起作用。



通过获取摆脱我的意思是让实际的类消失(以及来自调用代码)。



换句话说,这样的调用:

 最终MyAppBase app = EditionFactory.get_app(); 

..目前正在转换为ProGuarding之后:

  if(aa(1))
localObject5 = new c(); //<<<如果(a.a(2))
localObject5 = new d();则
else。 //<<< PRO

..虽然我希望将其翻译成:

  localObject5 = new d(); //<<< PRO仅在代码中(在Edition.EDITION中设置)

底线为 除了ProGuard很棒!! ),我似乎无法让它透视并明白Edition.is()是一个boolean函数返回一个常量,可以删除一些类。



我尝试过以下配置:

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

..没有效果。



另一方面,如果我参考Edition.EDITION并且比较它内联(即没有任何代理函数),Java编译器(v1.6)检测它并删除对非选定版本/类的整个引用。

这导致ProGuard移除/缩小未使用的类很棒。



这里唯一的问题是关于维护 - 我很乐意k eep能够使用EditionFactory样式。

解决方案

不执行优化,因为ProGuard决定不内联方法版本#是,因此它无法简化最终的一系列指令。该方法没有内联,因为它不是很短,并且它也被多次调用。您可以使用ProGuard的这个未记录的JVM选项解决第一个标准:

  -Dmaximum.inlined.code.length = 16 

或者,您可以通过确保仅调用一次方法来解决第二个标准:

 返回Edition.is(Edition.FREE)? 
new com.site.free.MyApp():
new com.site.pro.MyApp();

或者,你可以创建短函数isFree()和isPro(),因为它们会返回常量如果您期望进行某些特定的优化,那么检查已处理的代码是一个很好的做法,因为它们经常受到复杂的约束。



p>

很高兴听到您喜欢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.

这篇关于java ProGuard删除(收缩)未使用的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆