为什么proguard -keepclassmembers基于类说明符的行为不同?

时间:2014-03-15 02:08:42

标签: java android proguard

我一直在调试Android应用中的ProGuard问题,因为它删除了从JSON字符串中提取字段所需的字段名称。为了确保这些成员保留其原始名称,我补充说:

-keepclassmembers class com.example.common.regions.** {
    !static !final !transient <fields>;
}

这似乎有效,输出mapping.txt显示如下:

com.example.common.regions.RegionMapMeta -> com.example.b.d.b:
    java.lang.String mapVid -> mapVid
    int mapVersion -> mapVersion
    double topLeftLat -> topLeftLat
    double topLeftLong -> topLeftLong
    double topRightLat -> topRightLat
    double topRightLong -> topRightLong
    double bottomLeftLat -> bottomLeftLat
    double bottomLeftLong -> bottomLeftLong
    double bottomRightLat -> bottomRightLat
    double bottomRightLong -> bottomRightLong
    double boundingTopLat -> boundingTopLat
    double boundingBottomLat -> boundingBottomLat
    double boundingLeftLong -> boundingLeftLong
    double boundingRightLong -> boundingRightLong
    int minZoom -> minZoom
    int maxZoom -> maxZoom
    int maxZoomMinX -> maxZoomMinX
    int maxZoomMaxX -> maxZoomMaxX
    int maxZoomMinY -> maxZoomMinY
    int maxZoomMaxY -> maxZoomMaxY
    int stackOrder -> stackOrder
    java.lang.String thumbnailImage -> thumbnailImage
    java.util.Set mapLayers -> mapLayers

然后我认为我不想在ProGuard中指定目录,并且最好有一个注释来区分这些类。

@Target(ElementType.TYPE)
public @interface ProguardKeepClassMembers {
}

然后我用@ProguardKeepClassMembers注释我的类(不是成员,只是类),并将ProGuard规则更改为:

-keepclassmembers @com.example.common.ProguardKeepClassMembers class * {
    !static !final !transient <fields>;
}

但是,的工作方式相同。 mapping.txt文件具有相同的字段,但现在它们被混淆了。

com.example.common.regions.RegionMapMeta -> com.example.b.d.b:
    java.lang.String mapVid -> a
    int mapVersion -> b
    double topLeftLat -> c
    double topLeftLong -> d
    double topRightLat -> e
    double topRightLong -> f
    double bottomLeftLat -> g
    double bottomLeftLong -> h
    double bottomRightLat -> i
    double bottomRightLong -> j
    double boundingTopLat -> k
    double boundingBottomLat -> l
    double boundingLeftLong -> m
    double boundingRightLong -> n
    int minZoom -> o
    int maxZoom -> p
    int maxZoomMinX -> q
    int maxZoomMaxX -> r
    int maxZoomMinY -> s
    int maxZoomMaxY -> t
    int stackOrder -> u
    java.lang.String thumbnailImage -> v
    java.util.Set mapLayers -> w

所以... 为什么-keepclassmembers只在类匹配器发生变化时表现不同

1 个答案:

答案 0 :(得分:0)

ProGuard可能正在删除注释本身,因为代码没有使用它。你应该保留它:

-keep @interface com.example.common.ProguardKeepClassMembers

或者,您可以在jar annotations.jar中收集此类注释并将其指定为库(不将其留在源代码或库中):

-libraryjars annotations.jar

然后,注释类将不在结果代码中,这很好。这是示例examples / annotations / examples.pro中的方法(例如在android-sdk / tools / proguard中)。

另见ProGuard手册&gt;疑难解答&gt;处理后的意外观察&gt; Classes or class members not being kept