Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get Link of class failed importing a jar into an Android project in Eclipse?

In Eclipse I wrote a package of classes for use with Android and tested them in an android project, keeping the test code in a second package. Then I used the command line to create a jar file from all the casses in my project's bin/classes directory (just for my library package, not the test package).Running "jar tf" shows all the classes correctly prefixed with my package name.

I then created another Android project with an activity that imported my first package and used the methods, like this:

import uk.me.stevewaring.nestedsettings.NestedSettingsCommon;
import uk.me.stevewaring.nestedsettings.NestedSettingsReformat;
public class ShowNestedSettings extends Activity implements NestedSettingsReformat
{...

I then right clicked on my new project and used Build Path to add my jar. My jar shows at the top of the java build path under Libraries, and at the bottom under Order and Export.

Once I added my jar, all the red squiggly lines from lint complaining about the methods in my package vanished.

However when I try to debug my project, in the LogCat I get:

04-27 05:45:44.180: I/dalvikvm(14576): Failed resolving Lcom/example/shownestedsettings/ShowNestedSettings; interface 553 'Luk/me/stevewaring/nestedsettings/NestedSettingsReformat;'    
04-27 05:45:44.210: W/dalvikvm(14576): Link of class 'Lcom/example/shownestedsettings/ShowNestedSettings;' failed

and then the run fails because the class to run does not exist.

When I built the original project, I did not tick the box "Mark this project as a library", could that be what the problem is? I'm not sure what effect that check box has. If that is the problem, is there any way to retrospectively apply that action, or would I have to create a complete new project and copy my package into it before using it to create a jar. If that is not the problem, then what have I done wrong?

Further information: I removed the implementation of the interface, then when I tried to debug, I received this:

04-27 05:52:10.919: I/dalvikvm(14677): Could not find method uk.me.stevewaring.nestedsettings.NestedSettingsCommon.initialise, referenced from method com.example.shownestedsettings.ShowNestedSettings.onCreate
04-27 05:52:10.949: W/dalvikvm(14677): VFY: unable to resolve static method 3566: Luk/me/stevewaring/nestedsettings/NestedSettingsCommon;.initialise (Landroid/app/Activity;)V
04-27 05:52:10.949: D/dalvikvm(14677): VFY: replacing opcode 0x71 at 0x0003

which did not surprise me, NestedSettingsCommon.initialise was the first call my activity made to code in my Jar.

I also tried exporting the Jar from in Eclipse instead of making it from the command line. That made no difference either.

like image 347
Steve Waring Avatar asked Dec 06 '22 07:12

Steve Waring


1 Answers

Make sure the JAR is set to be exported with the application:

Project > Properties > Java Build Path > Order and Export

like image 118
Cruceo Avatar answered May 10 '23 23:05

Cruceo