How to stop ProGuard from stripping the Serializable interface from a class

前端 未结 2 1962
遇见更好的自我
遇见更好的自我 2021-02-19 07:31

Is there an explicit way to stop ProGuard from changing a class from implementing an interface?

I have a class that implements java.io.Serializable, let\'s

相关标签:
2条回答
  • 2021-02-19 08:03

    ProGuard doesn't ever strip interfaces that are defined in libraries (like Serializable) from classes in code that is processed (like Foo). Library code may be casting to those interfaces, so they can't be removed.

    I get null after I cast from Serializable to Foo

    This means that the instance must be null to start with. Your analysis would be correct if you'd get a ClassCastException. You can check that Foo still implements Serializable with javap. The problem probably lies elsewhere. For tips on serialization, you can look at the ProGuard manual > Examples > Processing serializable classes.

    Update:

    In this case, it turns out to be a configuration problem. ProGuard can only process class files if it knows everything about their hierarchy (just like a compiler). You really have to specify the runtime classes:

    -libraryjars <java.home>/lib/rt.jar
    

    or for Android:

    -libraryjars /usr/local/android-sdk/platforms/android-17/android.jar
    

    The Android Ant/Eclipse builds automatically specify all necessary -injars/-outjars/-libraryjars options for you, but in a custom build process, you have to specify them yourself. Cfr. ProGuard manual > Examples > A complete Android application.

    Note that the option -dontwarn makes the warnings go away, but not the problems. Only use it if really necessary.

    0 讨论(0)
  • 2021-02-19 08:05

    I had the same issue fixed using below config.

    -keepnames class * implements java.io.Serializable
    -keepclassmembers class * implements 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();
    }
    

    Official Documentation http://proguard.sourceforge.net/manual/examples.html#serializable

    0 讨论(0)
提交回复
热议问题