【问题标题】:How to stop Proguard from deleting return types?如何阻止 Proguard 删除返回类型?
【发布时间】:2014-08-11 08:37:24
【问题描述】:

我最近首次启用了 Proguard 的混淆功能,它似乎可以找到我的 -keep 规则中的所有漏洞。

我的保留规则是使用注释定义的:注释的元素将被单独保留。随后的配置如下所示:

# Keep the annotation.
-keep @interface org.mozilla.gecko.mozglue.JNITarget

# Keep classes tagged with the annotation.
-keep @org.mozilla.gecko.mozglue.JNITarget class *

# Keep all members of an annotated class.
-keepclassmembers @org.mozilla.gecko.mozglue.JNITarget class * {
    *;
}

# Keep annotated members of any class.
-keepclassmembers class * {
    @org.mozilla.gecko.mozglue.JNITarget *;
}

# Keep classes which contain at least one annotated element. Split over two directives
# because, according to the developer of ProGuard, "the option -keepclasseswithmembers
# doesn't combine well with the '*' wildcard" (And, indeed, using it causes things to
# be deleted that we want to keep.)
-keepclasseswithmembers class * {
    @org.mozilla.gecko.mozglue.JNITarget <methods>;
}
-keepclasseswithmembers class * {
    @org.mozilla.gecko.mozglue.JNITarget <fields>;
}

从 Reflection/JNI/etc 到 Java 的所有入口点。使用此(或等效配置但名称更好的)注释进行注释。

不幸的是,这并不能阻止 Proguard 重命名用作方法的返回类型的类,从而更改其签名并破坏入口点。

例如,Javap 显示带有签名的方法:

 public org.mozilla.gecko.Tab loadUrl(java.lang.String);

从 Proguard 出来的样子:

 public mt loadUrl(java.lang.String);

尽管被注释了。

那么,保持依赖类的神秘 -keep 语法是什么?奇怪的是,告诉它我希望保留一个入口点,它无论如何都会破坏它......

【问题讨论】:

    标签: java proguard


    【解决方案1】:

    这是预期的行为。在某个时候,在早期版本中,ProGuard 会自动保留所有返回类型和参数类型,但事实证明它对许多开发人员来说是不受欢迎的和困惑的。

    例如,对于使用 Class#getMethod 的反射,返回类型不相关。

    如果没有保留这种类型,ProGuard 现在会打印出注释。然后,您仍然可以为其添加 -keep 行。

    参见 ProGuard 手册 > 疑难解答 > Note: the configuration keeps the entry point '...', but not the descriptor class '...'

    更新:

    ProGuard 5.0 beta3 及更高版本支持

    -keep,includedescriptorclasses .......
    

    还可以自动将类保留在指定的字段类型、返回类型和参数类型中。

    【讨论】:

    • 这通常是没有必要的。请注意,描述符类被保留;只是没有保留他们的名字。对于大多数带有反射的代码来说,这不是问题。如果它们也被反射引用,这只是一个问题,在这种情况下,它们的名称确实需要使用 -keep 保留。 JNI 可能是一个不方便的例子。
    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2013-04-10
    相关资源
    最近更新 更多