12

I included a jar file in my Android project as explained in How can I use external JARs in an Android project?. With both methods described by MannyNS and Vinayak B. in this post I get the error "Could not find class 'test.libraryCalc.Calc" which is the class provided by the library. The following code illustrates the problem:

Example class provided via library: 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);
    }
}

I exported the jar file containing Calc.java to LibraryTest\libs\calc.jar

enter image description here

and added a reference to it using the "Add JARs..." button in the Java Build Path of LibraryTest

enter image description here The library shows up in the Referenced libraries in LibraryTest

enter image description here

LibraryTest has no build problems but when running it on the emulator the following is shown in 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

What needs to be done to get this working? Thanks for all suggestions.

Community
  • 1
  • 1
Lorenz
  • 165
  • 1
  • 1
  • 10
  • If you think pictures are helpful and you don't have permission yet, feel free to upload them to a simple external imagehoster and add the links instead. Usually a user with edit and picture permissions will stop by and embed them if he thinks these are appropriate. Edit: Ok upvoted over 10. Now you can post them on your own. :) –  Dec 27 '11 at 14:02
  • Also make sure the jar you are adding was compiled with Java 6 or earlier. Android does NOT support Java 7 as of this posting (July 2012) –  Jul 26 '12 at 01:00

1 Answers1

9

I think that the problem is that you try to add jar that contains Android code. You cannot do this. To include Android code you should create Android library. Simply create an Android project and in the project-properties Android section set that this is library project. After that you'll be able to add this library to your projects. For more about Android libraries you can read here.

Update: I've tried your code now. It works in my case. The only difference that I've made is during export of Jar I've checked Export Java source files and resources. Hope this will help you. Try it!

noelicus
  • 14,468
  • 3
  • 92
  • 111
Yury
  • 20,618
  • 7
  • 58
  • 86
  • Thanks Yury for your answer. Why do you think there is Android code in the jar? I created the project using the "Java Project" wizard in Eclipse. – Lorenz Dec 27 '11 at 16:18
  • 1
    Because LibraryTestActivity extends Activity ) – Yury Dec 27 '11 at 16:36
  • OK, I see where I created some confusion. LibraryTest is the main application that uses the Calc class from library LibraryCalc. – Lorenz Dec 27 '11 at 16:58
  • Thanks again Yury. Unfortenately that didn't solve the problem. Did a project clean on both LibraryCalc and LibraryTest but still have the same problem. – Lorenz Dec 27 '11 at 18:31
  • Try also to add Jar with the command "Add external jar" and specify the path to it. And you are not obliged to put it into libs folder. In this folder usually native libraries are stored. Just store it somewhere in the file system. – Yury Dec 27 '11 at 18:34
  • I see a warning on the picture in your LibraryTestActivity. What is the warning? – Yury Dec 27 '11 at 20:43
  • 7
    Hey, I think I've found the problem ) You use JavaSE-1.7 You have to use for JavaSE-1.6. Create Jar library and Android project with JavaSE-1.6 . – Yury Dec 27 '11 at 20:59
  • Hi Yury, thank you very much. That solved the problem! My environment was set up in such a way that Android projects use a different compiler compliance level by default then standard Java projects. – Lorenz Dec 27 '11 at 21:57
  • @Yury plz look at this problem http://stackoverflow.com/questions/18327657/class-not-found-exception-in-android – C Sharper Aug 20 '13 at 10:46