Android: ClassNotFoundException when passing serializable object to Activity
Asked Answered
D

3

15

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 have

public 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.this.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.5

Any new ideas?

Dragoman answered 16/5, 2011 at 8:32 Comment(6)
Is you object serializable at all?Cardialgia
We need some code to see what you are trying to do...Serration
@Flo: Yes, my object is SerializableDragoman
@Beasly: public void onCreate(Bundle savedInstanceState) { myObj = (MyObject) getIntent().getSerializableExtra("nameOfMyObject"); //here I am trying to read myObj's properties and app seems to crash here }Dragoman
Write a quick test class which serializes and then un-serializes your object just to confirm there's not a class further down in the chain that's causing the problem.Travers
@John J Smith I have done that and and run passed the Serializable object thousands of times but I was unable to reproduce the issue :S I guess that it is some kind of rare occurrence that only happens in some devices / situationsDragoman
D
43

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();
}
Dragoman answered 21/7, 2011 at 17:15 Comment(4)
after almost 2 years you saved my day buddy .. thanks +1. I also appreciate your attitude towards posting your own answerPickaback
Official ProGuard explaination: proguard.sourceforge.net/manual/examples.html#serializableJenellejenesia
what if proguard is not enabled? and this error is still occurringHim
I also faced this issue in some Samsung devices while Progurd is disabled.Ubangi
U
4

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

Ultimogeniture answered 23/5, 2011 at 11:43 Comment(1)
This was how I finally resolved a similar crash after trying the Proguard changes.Sedgemoor
C
1

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.

Cataract answered 23/5, 2011 at 3:0 Comment(1)
Thanks for the tip. I'll try this, but the main issue is that I'm not able to reproduce the problem anyway, so I can't even be sure that any solution that I find effectively solves the issue :(Dragoman

© 2022 - 2024 — McMap. All rights reserved.