在 eclipse 中,无法在另一个 android 项目中引用 android 库项目

在我写这个问题的时候,我设法解决了这个问题,所以为了其他人的利益,在这里重复一遍。以下是最初的问题:

我已经创建了一个非常简单的图书馆项目,我想在另一个项目中参考。我以前这样做没有问题,所以真的不知道为什么它不工作这一次。我有:

  1. 通过项目属性标记了库项目

  2. 在我的另一个项目中,通过项目属性添加了对库项目的引用。default.properties文件按预期添加了引用即 android.library.reference.1=K:/android_test_ws/applicationRegistrar

  3. 引用的图书馆项目的绿色标记从绿色开始,然后变成红色十字。

这意味着在库项目中一定有什么错误或遗漏了什么,但我不知道是什么。这次我的图书馆项目比我之前创建的项目要简单得多。

75067 次浏览

OK Here is the solution which I found when I was looking for the default.properies file of the referencing project (not the library) in my file system. Although the referencing project was in the same eclipse workspace as the library project, the actual files were somewhere else in the file system ie they were'nt in the same parent folder of the library project. As soon as I placed the referencing project in the same physical folder as the library project it all went fine.

I guess that this must be something to do with android using ant underneath the covers.

Edit: The project name needs match the folder name on the file system. What you are seeing in the Project Properties->Android->Library Reference is a relative file system path.

When you have a look at the reference-path before and after, it comes from i.e. "C:/workspace/mylib" and goes to "../../mylib" when copied to the correct location, quite interesting.

Simplest way to get the library paths paths correct is to use the GUI from Eclipse to add the library as shown in the following screenshot and let Eclipse take care of putting the correct relative paths in project.properties. Its a common setup to have your library projects hosted at directories vastly different than your main projects that uses the library. This method will work if the "libary project" and the project using it are in the same eclipse "workspace" (they "need not" be in same parent folder): enter image description here

Please ensure that the library project is marked as "Is Library" - right click on the library project - properties - Android - mark the "Is Library" checkbox - in project.properties of the library project you should have a new entry:"android.library=true. Now add it into the project you want as described in the post below.(the post with image integrated - from Nilesh Pawar).

the same problem will occur if your library project is in different partition from your current workspace. I have the same problem just now. My git source is in C: and I just move my workspace to D: and everything start to collapse.

Yet another observation on the same issue.

For me the two projects where on the same parent folder, and were both local inside the workspace. Even then the issue was still happening.

The I edited the "project.properties" file and put the absolute path(with forward slashes '/' for seperator) of the library project. Saved and closed it. Then went to the project properties dialog, removed the library(which was still showing the cross icon but with abs path) and added it back as usual. Surprisingly the issue is resolved, and the project compiles and runs.

This is really strange and must be a bug with the ADT. I am using ADT version 20.0.2

when developer referencing the facebook or any other library project then first of all clean the project from eclipse->project->clean project. that want allow the error of red cross in referencing screen.

This bug is referenced several times here 27199, 35786, 36460 & 38052

Maybe by voting for them, it will be fixed one day...

FYI,

What worked for me was to delete the 'library' projects (the actual projects) from my workspace (without deleting the files), and then re-importing them using the wizard (import existing android project from source code).

Workaround for me was to

  1. Create a new workspace
  2. Import Library Project in that workspace
  3. Import The desired project in that workspace
  4. Having both project and library project the same target Android OS version
  5. Reference library project in my project

solved my problem

Thanks for posting the question. I had exactly the same problem while integrating Facebook with my Android application. I fixed the issue by moving my development project to the same Windows drive in which library project was located. Somehow Eclipse is unable to read the library project's location properly from default.properties file if it is in a different drive.

Make Sure both the projects are present in same work space. To Do it, while importing the projects make sure "copy project into work space" check box is checked.

For me, I just restart the eclipse and the added library works fine.

I mean first time it showing red marks after adding the library project. Though eclipse main project and library project are in same workspace folder and no resources files are in outside of the project folder.

So, you can try with to restart your eclipse. Happy coding....

Similar to Sufi Khan's post I also solved this issue with a reboot. My case differed in that when I first accessed Properties->Android and added the library I got a lovely green checkmark. When I closed the dialog Eclipse was still showing class-not-found type errors. When I checked the properties again I saw the red X. But Mr. Kahn's solution (delete the bad lib, restart Eclipse, add the lib again) worked fine.

I'm using the 0702 version of the ADT bundle (starts with "cluster", rhymes with "duck").

I followed the accepted answer but also had to make sure my "project.properties" file was readable.

If the file is readonly (checked into source control) eclipse will not edit it. Adding the library reference will succeed, but the change won't be persisted after hitting OK.

If closing the preferences window and reopening in again removes the library you just added, this may be your solution.

In case your library project still doesn't show up try adding library flag in your library project properties

Add android.library=true

project.properties

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt


# Project target.
target=android-17
android.library=true

i had the same problem there when i try to change my workspace so this my solution:

  1. import and copy all project data including library project into workspace
  2. delete the old project reference by Right-click on the project-->Properties-->Android-->Library, and select corrupted library(so that waht i call it) and choose Remove
  3. clean project first (to refresh ur project properties)
  4. go to library project Properties-->Android-->Library and check the is library if it does'nt click Apply then OK
  5. if the library project is library is already checked, first Unchecked it then Clean the library project after that do the Step 4 again
  6. go to project that u want the library are in then Right-click on the project-->Properties-->Android-->Library, Add then choose the library project (it should be there) and click Apply then OK
  7. if still doesn't appear clean the project once more time and that should do

Just restart your eclipse. It's solve my problem