在Eclipse Android Project中引用外部项目时的Java运行时错误“NoClassDefFoundError”

时间:2022-04-26 09:15:41

I am struggling with this issue for some time now and I think it is time that I appeal to the SO community.

我现在正在努力解决这个问题一段时间,我认为现在是时候向SO社区呼吁了。

When creating a new Android Project in Eclipse I am trying to reference an external project [for the time being, it is a simple class with a function that returns a string]. I have tried adding a reference to the project itself, I have tried exporting a .JAR file [adding it to the libs folder] but none of these solutions work, though at compile time the classes are seen, at run time I get the dreaded NoClassDefFoundError.

在Eclipse中创建一个新的Android项目时,我试图引用一个外部项目[暂时,它是一个带有返回字符串的函数的简单类]。我已经尝试添加对项目本身的引用,我已经尝试导出.JAR文件[将其添加到libs文件夹]但这些解决方案都不起作用,但是在编译时看到类,在运行时我得到了可怕的NoClassDefFoundError错误。

Because the JAR file is present both in the filesystem and the .classpath file, I believe there might be an issue with the way I exported the JAR.

因为JAR文件存在于文件系统和.classpath文件中,所以我认为导出JAR的方式可能存在问题。

I am not sure why it doesn't work or what the proper workflow is for adding such [non-Android] modules to a project. Any help would be greatly appreciated!

我不确定为什么它不起作用或者将这样的[非Android]模块添加到项目的正确工作流程是什么。任何帮助将不胜感激!

I have come across this issue in a lot of SO questions but I could not find any solutions that would work

我在很多问题中遇到过这个问题,但我找不到任何可行的解决方案

1 个解决方案

#1


0  

I've actually had the same problem before. There are a couple things you can try.

我以前遇到过同样的问题。你可以试试几件事。

Make sure that in the project properties for your external project that you have the the "Is Library" checked in the Android tab.

确保在外部项目的项目属性中,在Android选项卡中选中了“Is Library”。

Then export the project as a jar file. (make sure you don't include and files other than the actual java class files. No AndroidManifest.xml etc...)

然后将项目导出为jar文件。 (确保你没有包含和实际java类文件以外的文件。没有AndroidManifest.xml等...)

Include the jar file into your other project in the "libs" folder like you were doing before.

像以前一样,将jar文件包含在“libs”文件夹中的其他项目中。

and you should be able to reference the class from the jar now.

你现在应该可以从jar中引用这个类了。

If that still doesn't work, you might have an ordering issue, so right click the jar file, and select "Build Path" -> "add to build path", then go to the project properties of your non-library project and select Java Build Path -> Order and Export and move the jar file to the top of the list.

如果仍然无效,您可能会遇到排序问题,因此右键单击jar文件,选择“Build Path” - >“add to build path”,然后转到非库项目的项目属性,选择Java Build Path - > Order and Export并将jar文件移动到列表顶部。

Hopefully this helps!

希望这有帮助!

Cheers

#1


0  

I've actually had the same problem before. There are a couple things you can try.

我以前遇到过同样的问题。你可以试试几件事。

Make sure that in the project properties for your external project that you have the the "Is Library" checked in the Android tab.

确保在外部项目的项目属性中,在Android选项卡中选中了“Is Library”。

Then export the project as a jar file. (make sure you don't include and files other than the actual java class files. No AndroidManifest.xml etc...)

然后将项目导出为jar文件。 (确保你没有包含和实际java类文件以外的文件。没有AndroidManifest.xml等...)

Include the jar file into your other project in the "libs" folder like you were doing before.

像以前一样,将jar文件包含在“libs”文件夹中的其他项目中。

and you should be able to reference the class from the jar now.

你现在应该可以从jar中引用这个类了。

If that still doesn't work, you might have an ordering issue, so right click the jar file, and select "Build Path" -> "add to build path", then go to the project properties of your non-library project and select Java Build Path -> Order and Export and move the jar file to the top of the list.

如果仍然无效,您可能会遇到排序问题,因此右键单击jar文件,选择“Build Path” - >“add to build path”,然后转到非库项目的项目属性,选择Java Build Path - > Order and Export并将jar文件移动到列表顶部。

Hopefully this helps!

希望这有帮助!

Cheers