Search code examples
androidprojects-and-solutions

Runtime error NoClassDefFoundError pointing to Parcelable is not found


Please help me solve this, I am stuck here, my application works on higher version devices, MOGO G3 and Moto XPlay , but when I test on devices having lower version (16) , it gives me error, that NoClassDefFoundError.

This is my sdk configuration :

minSdkVersion 16 targetSdkVersion 22

This is the class which android is not able to find during Runtime :

public class CategoryParcel implements Parcelable{

private Long id;
private int icon;
private int category_resource;
private String categories;
private String note;
private Integer price;
private Integer quantity;
private String url;

public CategoryParcel(Parcel in){
    id = in.readLong();
    category_resource = in.readInt();
    categories = in.readString();
    note = in.readString();
    price = in.readInt();
    quantity = in.readInt();
    icon = in.readInt();
    url = in.readString();
}

public CategoryParcel(){}

public CategoryParcel(Long id, String categories, String note, int price, int quantity, int icon){
    this.id = id;

    this.categories = categories;
    this.note = note;
    this.price = price;
    this.quantity = quantity;
    this.icon = icon;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public int getCategory_resource() {
    return category_resource;
}

public void setCategory_resource(int category_resource) {
    this.category_resource = category_resource;
}

public String getCategories() {
    return categories;
}

public void setCategories(String categories) {
    this.categories = categories;
}

public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}

public Integer getPrice() {
    return price;
}

public void setPrice(Integer price) {
    this.price = price;
}

public Integer getQuantity() {
    return quantity;
}

public void setQuantity(Integer quantity) {
    this.quantity = quantity;
}

public int getIcon() {
    return icon;
}

public void setIcon(int icon) {
    this.icon = icon;
}

@Override
public int describeContents() {
    return 0;
}


@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeLong(id);
    parcel.writeInt(category_resource);
    parcel.writeString(categories);
    parcel.writeString(note);
    parcel.writeInt(price);
    parcel.writeInt(quantity);
    parcel.writeInt(icon);
}

public static final Parcelable.Creator<CategoryParcel> CREATOR = new Parcelable.Creator<CategoryParcel>()
{
    public CategoryParcel createFromParcel(Parcel in)
    {
        return new CategoryParcel(in);
    }
    public CategoryParcel[] newArray(int size)
    {
        return new CategoryParcel[size];
    }
};

}

Error points to this line :

public static final Parcelable.Creator CREATOR = new Parcelable.Creator()

If you suggest I can share more details. I am clueless what I am doing wrong here.

Thanks for reading. Shashank


Solution

  • I solved the problem, this is related to

    compile 'com.android.support:multidex:1.0.1'

    Recently Google released this api to support lower version of android.

    Three things to check :

    1. dependencies in build.gradle

    { compile 'com.android.support:multidex:1.0.1' }

    1. add in defaultConfig in build.gradle

    defaultConfig { multiDexEnabled true }

    1. In AndroidManifest.xml file add
     <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.android.multidex.myapplication">
            <application
                ...
                android:name="android.support.multidex.MultiDexApplication">
                ...
            </application>
        </manifest>
    

    In my case i forgot to add point number 3.

    Now it works fine and it works on all intended devices.

    Thanks, Shashank Pratap