Eclipse Android project, how to reference library

2019-01-06 14:43发布

问题:

I followed some steps I found here (can't find the URL right now, sorry) to convert my Android project in Eclipse to a layout where 99.9% of my code is in a library project, and then I have 2 other shell projects under the same workspace that are mostly just the AndroidManifest.xml files, and a few resource files. This was done so I can support 2 builds of the same project, with just some minor text/icon changes between the 2. The application name is also different so I can publish both on the Android Market at the same time.

Ever since I did this, about every 10 times I compile, maybe once every day or two, I get "dalvik error 1" and something about "Access already exists" (Access being the name of the first Java unit in my library project).

To "work around" the issue I go in to the Java Build path for my stub-project that I am trying to build, and remove the JAR file from my main library from the libraries tab. Then I can build without the error.

Then a while later (maybe 1 or 2 days) I'll get an error about missing classes when I compile my stub-project (not my library). So I'll go back to the Java Build path and put the reference to the JAR file back in, and all is good again for 1 or 2 days, then I'm back to the same error as before.

Is this just a known issue and something I need to do, or can I resolve by a restructure of my projects/workspaces? Currently I have:

Lib Project - only has 2 libs on build path: Android 2.1 and com.android.ide.eclipse.adt.LIBRARIES

First stub project that uses above lib - has the same 2 libs as above project, plus sometimes I use "Add JAR" to include the JAR from the above project's bin\ folder.

2nd stub project - same libs as first stub project

Should I reference the JAR from my "lib project" using one of the other tabs under build path options? Maybe the "Project" tab instead, or the "Source" tab? I don't currently have it under any of those other areas.

When I get in to the weird state, doing a "Clean project" also doesn't help, I've tried that several times and open/close the IDE between cleaning, to no resolve.

At this point we are in the final testing stages, so my normal daily task is:

  1. Make a minor update (bug fix) in the LIB project

  2. Use the publish wizard to export both projects and update Android Market and other places we keep the APK files

So I'd like those steps to stay simple, without having to open/close multiple workspaces or go through a lot of build steps if possible.

回答1:

The classical Eclipse/Java way

Add a (workspace) library

  • Right click the project you want to insert in.
  • Click Properties.
  • Select Java Build Path.
  • Select the Libraries tab.

Now, it depends how you compile your library. So either do an Add JARs... or an Add Class Folder. Choose the external variant to use an external source. That's it.

Add a workspace project

  • Right click the project you want to insert in.
  • Click Properties.
  • Select Java Build Path.
  • Select the Projects tab.

Click Add... on the right side and you are done.

The additional Android way

Two steps are necessary:

  • Mark the project you want to use as library project
  • Reference the marked project

Mark the library project

Right click your project and select Properties. Select Android on the left and tick the checkbox IsLibrary. Done.

Reference the marked project

Right click your project and select Properties. Select Android on the left and Add... your marked project. It will be added to the list and is ready to use. Now you are able to access all classes and ressources (e.g. drawables, strings) from the referenced, marked project. Awesome, eh? :)