NoClassDefFoundError when using objenesis on Android

我的梦境 提交于 2019-12-13 14:54:11

问题


I'm developing an android app which use the com.rits.cloning and org.objenesis.* libs to deep clone object. The purpose is to add same object to my tree structure class twice or more without worrying that the original object is referencing to the same object. Since the regular clone() just shallow copy the object, I use the mentioned libs.

Before develop the android project, I created a java project to implement my tree and it worked fine using those libs. Then I imported it to the android project (and also add com.rits.cloning, org.objenesis.* as external lib and then check both libs in the Java Build Path > Order and Export). But when I run it, right at the line where I call deepClone(), this error showed up in the LogCat and the app is force closed:

E/AndroidRuntime(280): FATAL EXCEPTION: main
E/AndroidRuntime(280): java.lang.NoClassDefFoundError: sun.reflect.ReflectionFactory
E/AndroidRuntime(280):  at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.<init>(SunReflectionFactoryInstantiator.java:40)
E/AndroidRuntime(280):  at org.objenesis.strategy.StdInstantiatorStrategy.newInstantiatorOf(StdInstantiatorStrategy.java:85)
E/AndroidRuntime(280):  at org.objenesis.ObjenesisBase.getInstantiatorOf(ObjenesisBase.java:90)
E/AndroidRuntime(280):  at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
E/AndroidRuntime(280):  at com.rits.cloning.Cloner.newInstance(Cloner.java:291)
E/AndroidRuntime(280):  at com.rits.cloning.Cloner.cloneInternal(Cloner.java:468)
E/AndroidRuntime(280):  at com.rits.cloning.Cloner.deepClone(Cloner.java:327)
E/AndroidRuntime(280):  at com.susterblonde.project_monitoring.project.WBS.add(WBS.java:35)
E/AndroidRuntime(280):  at com.susterblonde.project_monitoring.project.ProjectUtility.demoPlan(ProjectUtility.java:101)
E/AndroidRuntime(280):  at com.susterblonde.project_monitoring.ProjectSelection.onCreate(ProjectSelection.java:45)
E/AndroidRuntime(280):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(280):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(280):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(280):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(280):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(280):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(280):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(280):  at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(280):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(280):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(280):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(280):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(280):  at dalvik.system.NativeStart.main(Native Method)

I googled and have tried this separately:

  • clean the project
  • remove com.rits.cloning and org.objenesis.* from the build path and readd
  • reorder those libs so that they on right below the Android Dependencies
  • reorder those libs so that they on top of all libs

I don't think it's because of eclipse update because I updated it weeks before this happen.

Here's the source code:

package com.susterblonde.tree;

import java.util.ArrayList;
import com.rits.cloning.Cloner;

public class MyTree {
    Data o;
    MyTree parent;
    ArrayList<MyTree> child = new ArrayList<MyTree>();

    public void add(MyTree tree) {
        Cloner c = new Cloner();

        MyTree temp =   c.deepClone(tree); //this is where the app crashed
        temp.parent = this;
        child.add(temp);
    }

    public static void main(String[] args) {
        MyTree tree1 = new MyTree();
        MyTree tree2 = new MyTree();

        tree1.add(tree2);
        tree1.add(tree2);
        tree1.add(tree2);
        //The result wanted here is tree1 has 3 different but identical child objects
        //NOT 3 child which refer to the same one object
    }
}

class Data {
    double value;
}

Question:

  • Why do I got that error and how do I get rid of it?
  • Is there any other way to implement my tree?

Pardon my english.

Thank you :)


回答1:


In Eclipse when you add an external library in the Java Build Path dialog, don't forget to also switch to the Order and Export tab and tick that library's name in the list. This is needed so that the library is found at run-time, not only at compile time.

NoClassDefFoundError when running Instrumentation test with ant



来源:https://stackoverflow.com/questions/12333342/noclassdeffounderror-when-using-objenesis-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!