Android:如何从 Eclipse ADT 应用程序项目中引用库项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3774902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to reference a library project from an Eclipse ADT application project?
提问by Anm
I have a project that is compiles but throws a NoClassDefFoundError on a classname included from a sibling library project, but only when built from Eclipse. When I build the project from the ant file, it runs just fine.
我有一个项目可以编译但在同级库项目中包含的类名上抛出 NoClassDefFoundError ,但仅当从 Eclipse 构建时。当我从 ant 文件构建项目时,它运行得很好。
How do I tell the Eclipse build system that my application's .apk needs a copy of the .class files from the library project.
我如何告诉 Eclipse 构建系统我的应用程序的 .apk 需要来自库项目的 .class 文件的副本。
This page... http://developer.android.com/guide/developing/eclipse-adt.html#libraryReferencemakes is sound like adding the library should "just work" with no other steps. And I see it has updated my default.properties with the necessary link that makes my ant build work.
这个页面... http://developer.android.com/guide/developing/eclipse-adt.html#libraryReference听起来就像添加库应该“正常工作”,没有其他步骤。我看到它已经更新了我的 default.properties,其中包含使我的 ant 构建工作的必要链接。
Despite following those steps (my library project is listed in the Android page of my application's project properties), Eclipse doesn't recognize the new classes in the editor. So I did the standard Eclipse step of adding the library to the Java Build Path. Now the compiler and IDE recognize the class, but my app still crashes with NoClassDefFoundError, even after checking the library in the Java Build Path, Order and Export.
尽管遵循了这些步骤(我的库项目列在我的应用程序项目属性的 Android 页面中),Eclipse 无法识别编辑器中的新类。因此,我执行了将库添加到 Java 构建路径的标准 Eclipse 步骤。现在编译器和 IDE 可以识别该类,但我的应用程序仍然因 NoClassDefFoundError 崩溃,即使在 Java Build Path、Order 和 Export 中检查了库之后也是如此。
What step am I missing?
我错过了什么步骤?
Some background...I built the Eclipse project first. Then, in a separate directory, I used android create project ...
with all the parameters to create an identically configured project. Finally, I copied the ant build.xml and *.properties files to the Eclipse project directory. I did the same thing with the library project I'm trying to reference.
一些背景...我首先构建了 Eclipse 项目。然后,在一个单独的目录中,我使用android create project ...
所有参数来创建一个相同配置的项目。最后,我将ant build.xml 和*.properties 文件复制到Eclipse 项目目录中。我对我试图引用的图书馆项目做了同样的事情。
回答by evok
I have this every once in a while and not sure what causes it. To fix this, I have to link to the source of my library projects. In the build path of your main project, link to the "src" folder of your library project that you are trying to include.
我每隔一段时间就会出现这种情况,但不确定是什么原因造成的。为了解决这个问题,我必须链接到我的库项目的源代码。在主项目的构建路径中,链接到您尝试包含的库项目的“src”文件夹。
回答by Roger Heim
We had the same issue (worked in IntelliJ and with Ant but not with Eclipse.) There was an installation of Aptana Studio installed in Eclipse. Removing that allowed the library project reference to work in Eclipse as advertised.
我们遇到了同样的问题(在 IntelliJ 和 Ant 中工作,但在 Eclipse 中没有。)在 Eclipse 中安装了 Aptana Studio。删除允许库项目引用在 Eclipse 中如宣传的那样工作。
回答by Udit Jain
1) Right click on your Android project.
1) 右键单击您的 Android 项目。
2) Go to its properties
2)转到其属性
3) Choose Android option from the menu displayed on the left
3) 从左侧显示的菜单中选择 Android 选项
4) See the library panel which is on the right side, below the list of APIs.
4) 查看右侧的库面板,位于 API 列表下方。
5) Click the "Add.." button to add the library project.
5) 点击“Add..”按钮,添加库项目。
Keep in mind that the seperate project you are trying to add as a ibrary for your current project should be marked as "IsLibrary".
请记住,您尝试添加为当前项目的图书馆的单独项目应标记为“IsLibrary”。
回答by Girish K Gupta
I got similar a issue. Even after adding a library project the required files were not included. It seems there is a bug as discussed in this thread.
我遇到了类似的问题。即使在添加库项目之后,所需的文件也没有包含在内。似乎在此线程中讨论了一个错误。
I found that in Eclipse's default.propertiesfile the library project location was not being read because my working project was in a different drive than library project (I was doing the development in Windows). After moving my project to same drive as library project it started working. This might work as a work around till the bug is fixed.
我发现在 Eclipse 的default.properties文件中,库项目位置未被读取,因为我的工作项目与库项目位于不同的驱动器中(我在 Windows 中进行开发)。将我的项目移动到与库项目相同的驱动器后,它开始工作。在修复错误之前,这可能会作为一种解决方法。