Android: ClassNotFoundException when passing serializable object to Activity
I have a few crash report logs from android market similar to this:
Exception class: java.lang.ClassNotFoundException
Source method: PathClassLoader.findClass()
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.app.package/my.app.package.MyActivity}: java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = my.app.package.a.ae)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
at android.app.ActivityThread.access$2300(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5068)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = my.app.package.a.ae)
at android.os.Parcel.readSerializable(Parcel.java:1951)
at android.os.Parcel.readValue(Parcel.java:1822)
at android.os.Parcel.readMapInternal(Parcel.java:2008)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getBundle(Bundle.java:1078)
at android.app.Activity.onRestoreInstanceState(Activity.java:861)
at android.app.Activity.performRestoreInstanceState(Activity.java:835)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2811)
... 11 more
Caused by: java.lang.ClassNotFoundException: my.app.package.a.ae
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:235)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2590)
at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1846)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:826)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2066)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
at android.os.Parcel.readSerializable(Parcel.java:1945)
... 19 more
Caused by: java.lang.NoClassDefFoundError: my.app.package.a.ae
... 29 more
Caused by: java.lang.ClassNotFoundException: my.app.package.a.ae in loader dalvik.system.PathClassLoader[.]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 29 more
Please note that I am using proguard -> that's why the "missing" class name is "a.ae".
Actually, "a.ae" is the name proguard gave to the class "MyObject", which is shown below.Any ideia on why this happens?
I don't think it has to do with proguard, because if it was a problem caused by proguard class renaming, it should happen all times and I would be able to reproduce this issue myself.
Does it have to do with passing a serializable object to MyActivity?
In MyActivity I havepublic void onCreate(Bundle savedInstanceState) {
myObj = (MyObject) getIntent().getSerializableExtra("nameOfMyObject");
//here I am trying to read myObj's properties and app seems to crash here
}
MyObject is just an entity with several types of class members, ex:
public class MyObject implements Serializable { /** Generated serialVersionUID */
static final long serialVersionUID = -267025879233327409L;
static final String CONSTANT1 = "xpto";
LinkedHashMap<String, Object> components;
ArrayList<String> prop1;
(...)
}
I am starting MyActivity with:
MyObject obj = new MyObj(stuff);
Intent intent;
intent = new Intent(CurrentActivity.this, MyActivity.class);
intent.putExtra("nameOfMyObject", obj);
CurrentActivity.th开发者_开发知识库is.startActivity(intent);
Do you think it has to do with the way the object is being passed (as a serializable extra)?
In the case that I don't find any other solution I think I will "serialize" the object myself to a String and then pass the String extra. What do you think about this?EDIT: I have tried to reproduce this issue, without success, in the emulator and in the following devices:
-Motorola Milestone 2 Android 2.2
-Samsung Galaxy Tab Android 2.2 -Vodafone 845 Android 2.1 -HTC Tatoo Android 1.6 -Xperia X10 Android 1.6 -Nexus One Android 2.2 -Google G1 Android 1.5Any new ideas?
Finally reached a conclusion on this issue: it was being caused by Proguard, or more specifically, because I didn't have a propper Proguard configuration.
It turns out Proguard was changing my Serializable
's classes names, which makes Class.forName(className)
fail.
I had to reconfigure my proguard.cfg
file adding the following lines:
-keep 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 <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
You should look into implementing Parcelable
.
These two links should get you started:
http://developer.android.com/reference/android/os/Parcel.html
http://developer.android.com/reference/android/os/Parcelable.html
LinkedHashMap
doesn't implement Serializable
so this may be causing the error. Try either implementing a small test to serialize this class or try removing the LinkedHashMap
and see if the error persists.
精彩评论