Android,从导入的jar文件中找不到类

时间:2011-12-27 13:58:52

标签: android import jar

我在我的Android项目中包含了一个jar文件,如How can I use external JARs in an Android project?中所述。使用MannyNS和Vinayak B.在本文中描述的两种方法,我得到错误“找不到类'test.libraryCalc.Calc”,这是该库提供的类。以下代码说明了问题:

通过库提供的示例类:Calc.java

package test.libraryCalc;

public class Calc {
    public int add(int a, int b){
        return a + b;
    }
}

LibraryTestActivity.java

package test.library;

import test.libraryCalc.Calc;
import android.app.Activity;
import android.os.Bundle;

public class LibraryTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calc calc = new Calc();
        int c = calc.add(3, 4);
    }
}

我将包含Calc.java的jar文件导出到LibraryTest \ libs \ calc.jar

enter image description here

并使用LibraryTest的Java Build Path中的“Add JARs ...”按钮添加对它的引用

enter image description here 该库显示在LibraryTest的Referenced库中

enter image description here

LibraryTest没有构建问题,但在模拟器上运行时,LogCat中显示以下内容:

12-27 14:01:33.965: E/dalvikvm(747): Could not find class 'test.libraryCalc.Calc',               referenced from method test.library.LibraryTestActivity.onCreate
12-27 14:01:33.965: W/dalvikvm(747): VFY: unable to resolve new-instance 13   (Ltest/libraryCalc/Calc;) in Ltest/library/LibraryTestActivity;
12-27 14:01:33.995: D/dalvikvm(747): VFY: replacing opcode 0x22 at 0x0008
12-27 14:01:33.995: D/dalvikvm(747): VFY: dead code 0x000a-0013 in Ltest/library/LibraryTestActivity;.onCreate (Landroid/os/Bundle;)V
12-27 14:01:34.065: D/AndroidRuntime(747): Shutting down VM
12-27 14:01:34.065: W/dalvikvm(747): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-27 14:01:34.075: E/AndroidRuntime(747): FATAL EXCEPTION: main
12-27 14:01:34.075: E/AndroidRuntime(747): java.lang.NoClassDefFoundError: test.libraryCalc.Calc
12-27 14:01:34.075: E/AndroidRuntime(747):  at     test.library.LibraryTestActivity.onCreate(LibraryTestActivity.java:14)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.os.Looper.loop(Looper.java:123)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-27 14:01:34.075: E/AndroidRuntime(747):  at java.lang.reflect.Method.invokeNative(Native Method)
12-27 14:01:34.075: E/AndroidRuntime(747):  at java.lang.reflect.Method.invoke(Method.java:521)
12-27 14:01:34.075: E/AndroidRuntime(747):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-27 14:01:34.075: E/AndroidRuntime(747):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-27 14:01:34.075: E/AndroidRuntime(747):  at dalvik.system.NativeStart.main(Native Method)
12-27 14:06:34.170: I/Process(747): Sending signal. PID: 747 SIG: 9

要使这项工作需要做些什么?感谢所有建议。

1 个答案:

答案 0 :(得分:9)

我认为问题在于您尝试添加包含Android代码的jar。你不能做这个。要包含Android代码,您应该创建Android库。只需创建一个Android项目,并在项目属性Android部分设置这是库项目。之后,您将能够将此库添加到项目中。有关Android库的更多信息,请阅读here

更新:我现在已经尝试过您的代码了。它适用于我的情况。我所做的唯一区别是在导出Jar时我检查了导出Java源文件和资源。希望这会帮助你。试试吧!