Search code examples
androidobfuscationproguardxstream

Problems with Proguard and XStream Annotations


I have been up and down the various proguard issues on here so far and so far have had no luck. After proguarding my app, when I try and run a POJO class through XStream, the following crash happens:

09-09 23:01:51.188: E/AndroidRuntime(18692): java.lang.annotation.IncompleteAnnotationException: The element value is not complete for the annotation com.thoughtworks.xstream.annotations.XStreamConverter
09-09 23:01:51.188: E/AndroidRuntime(18692):    at libcore.reflect.AnnotationFactory.invoke(AnnotationFactory.java:301)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at java.lang.reflect.Proxy.invoke(Proxy.java:379)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at $Proxy2.value(Unknown Source)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at com.thoughtworks.xstream.mapper.AnnotationMapper.cacheConverter(SourceFile:515)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at com.thoughtworks.xstream.mapper.AnnotationMapper.processLocalConverterAnnotation(SourceFile:472)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at com.thoughtworks.xstream.mapper.AnnotationMapper.processTypes(SourceFile:222)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at com.thoughtworks.xstream.mapper.AnnotationMapper.processAnnotations(SourceFile:164)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at com.thoughtworks.xstream.XStream.processAnnotations(SourceFile:1971)
09-09 23:01:51.188: E/AndroidRuntime(18692):    at com.thoughtworks.xstream.XStream.processAnnotations(SourceFile:1982)

Having looked at the various other SO posts for this as well as the Proguard documentation, I have the following in my proguard configuration file:

-keepattributes SourceFile, LineNumberTable, *Annotation*, Signature, EnclosingMethod, InnerClasses
-dontwarn com.thoughtworks.**

# For serializable classes
-keepnames class * extends java.io.Serializable

# For serializable classes
-keepclassmembers class * extends java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Keep everything in the XStream Package
-keep class com.thoughtworks.** {
    *;
}
# Keep my XStreamExporter class
-keep class com.mypackage_name.data.exporters.XStreamExporter {
    *;
}

The serialization is keeping my POJO class intact as confirmed by the seeds.txt output. I have tried everything I can think of/find regarding this issue. What did I miss?


Solution

  • If you are using the Proguard included in the Android SDK, you may be using an older version(4.5? I think).

    Proguard prior to version 4.9 might have had a problem with annotations being removed.

    Fixed overly aggressive shrinking of class annotations. - http://proguard.sourceforge.net/downloads.html, 6th bullet in the list of changes for version 4.9.

    Try the latest version of Proguard by replacing the jar in the SDK with the latest version, 5.0 (http://sourceforge.net/projects/proguard/files/proguard/). The jar to replace is located in android-sdk/tools/proguard/lib/proguard.jar.