简体   繁体   English

如何在Android项目中包含Java项目?

[英]How to include a Java project in an Android project?

I am using Eclipse to develop an Android board game. 我正在使用Eclipse开发Android棋盘游戏。 I have developed the UI in an Android project. 我在Android项目中开发了UI。

On the other hand, I have developed the AI in a regular Java project, because I wanted to be able to test it without all the constraints of the Android emulator (I didn't find any other way to run the code using my Windows JVM). 另一方面,我在常规Java项目中开发了AI,因为我希望能够在没有Android模拟器的所有约束的情况下测试它(我没有找到使用我的Windows JVM运行代码的任何其他方法)。

Now comes the times when I want to 'join' both projects (which work fine independently), that is to use the AI Java classes from the UI. 现在,我希望“加入”两个项目(独立工作正常),即使用UI中的AI Java类。

This is what I have tried: 这是我尝试过的:

In my Android project Properties > Projects References , I ticked the Java project. 在我的Android项目Properties > Projects References中 ,我勾选了Java项目。 This allows me to build without error the Android project (which instantiates an AI object). 这允许我构建没有错误的Android项目(实例化AI对象)。

But it fails at runtime with this error: 但它在运行时因此错误而失败:

Could not find class 'my.package.AI', referenced from method my.otherpackage.UI.onStart

What would be the correct way to include the AI Java Project into my Android Project ? 将AI Java项目纳入我的Android项目的正确方法是什么?

(NB: I still want to be able to develop and test the AI as a regular Java app, so I think using a jar or copying all the sources to the Android Project would not suit my needs) (注意:我仍然希望能够开发和测试AI作为常规Java应用程序,因此我认为使用jar或将所有源代码复制到Android项目将不适合我的需求)

EDIT: 编辑:

I was hoping that the new ADT v17 would solve the problem, but it didn't. 我希望新的ADT v17可以解决问题,但事实并非如此。 I have tried virtually every option available to include a project in my Android project: 我几乎尝试了所有可用的选项,在我的Android项目中包含一个项目:

  • Adding or linking a source folder 添加或链接源文件夹
  • Adding a Project (Java Build Path > Projects tab) 添加项目(Java构建路径>项目选项卡)
  • Adding a Class Folder or a Library (Java Build Path > Library tab) 添加类文件夹或库(Java构建路径>库选项卡)
  • Ticking all the previously imported projets/libraries as Exported 将所有先前导入的projets /库标记为已导出

The only way that allows compiling and running without error is adding the JAR of the Java Project to the Android project build path. 允许编译和运行而没有错误的唯一方法是将Java项目的JAR添加到Android项目构建路径。

Projects References only add the dependent project source code (your AI project) as a soft (perhaps weak is a more accurate word) reference, your AI project is not added into you Android Project Build path. Projects References仅将依赖项目源代码(您的AI项目)添加为软(可能是一个更准确的单词)参考,您的AI项目不会添加到您的Android项目构建路径中。 so the actual ai.jar is not exported into the final apk when Eclipse build your app. 因此,当Eclipse构建您的应用程序时,实际的ai.jar不会导出到最终的apk中。

You should add AI project into Android project build path: 您应该将AI项目添加到Android项目构建路径中:

  1. Right-click on your android project, select Build Path - Configure Build Path, in Projects tab (Required projects on the build path), add your AI project here. 右键单击您的android项目,在Projects选项卡(构建路径中的必需项目)中选择Build Path - Configure Build Path,在此处添加您的AI项目。
  2. Then in Order and Export tab (Build class path order and exported entries), tick your newly added AI project appeared int the list. 然后在“订购和导出”选项卡(构建类路径顺序和导出的条目)中,勾选列表中出现的新添加的AI项目。

Step 2 is probably optional, this should add AI project as a reference in Android project and export the ai.jar to final.apk when Eclipse build your Android project. 第2步可能是可选的,这应该在Android项目中添加AI项目作为参考,并在Eclipse构建Android项目时将ai.jar导出到final.apk。

Update from ADT 17.0.0: ADT 17.0.0更新:

Android Dev Team just release SDK r17 with ADT 17.0.0, which claims to handle this use cases properly now: Android Dev Team刚刚发布了带有ADT 17.0.0的SDK r17,它声称现在可以正确处理这些用例:

Eclipse specific changes Eclipse特定的更改

The dynamic classpath container called “Library Projects” has been renamed to “Android Dependencies” as it now contains more than just Library Projects. 名为“Library Projects”的动态类路径容器已重命名为“Android Dependencies”,因为它现在不仅包含Library Projects。

The container will now also be populated with Java-only projects that are referenced by Library Projects. 现在,容器也将填充由Library Projects引用的仅Java项目。 If those Java projects also reference other Java projects and/or jar files they will be added automatically (jar files referenced through user libraries are supported as well). 如果这些Java项目也引用其他Java项目和/或jar文件,它们将自动添加(也支持通过用户库引用的jar文件)。

Important: this only happens if the references are set to be exported in the referencing project. 重要说明:仅当引用设置为在引用项目中导出时才会发生这种情况。 Note that this is not the default when adding a project or jar file to a project build path. 请注意,将项目或jar文件添加到项目构建路径时,这不是默认设置。 Library Projects (and the content of their libs/*.jar files) is always exported. 始终导出库项目(及其libs / * .jar文件的内容)。 This change only impacts Java-only projects and their own jar files. 此更改仅影响仅Java项目及其自己的jar文件。

Again, duplicates (both projects and jar files) are detected and removed. 同样,检测并删除重复项(项目和jar文件)。

More in this link . 更多链接

Edit: After much testing, reading and found my solution. 编辑:经过大量测试,阅读并找到了我的解决方案。 Problem is (as of this writing) that you can't reference another project from an Android project, although it works fine for a normal Java application project. 问题是(在撰写本文时)您无法从Android项目引用另一个项目,尽管它适用于普通的Java应用程序项目。

Workaround for me (Windows with NTFS filesystem): 我的解决方法(带有NTFS文件系统的Windows):
In a prompt (admin rights) make a symbolic folder link using mklink /D command pointing to your source project folders, and refresh in Eclipse as needed. 在提示符(管理员权限)中,使用指向源项目文件夹的mklink /D命令创建符号文件夹链接,并根据需要在Eclipse中刷新。 The obvious limitation is you cant use the same package names in source project and target project, and then there is the problem with libs in the common project. 显而易见的限制是您无法在源项目和目标项目中使用相同的包名称,然后在公共项目中存在libs的问题。

example: 例:

cd \java\workspace\AndroidProject\src\your\package
mklink /D common c:\java\workspace\CommonProject\src\your\package\common

Clarification: You can reference another project from an Android project to make it compile, however when you run, the referenced classes from the included project are not put in the apk classes.dex (verified with dex decompiler). 澄清:可以从Android项目中引用另一个项目来进行编译,但是当您运行时,包含项目中引用的类不会放入apk classes.dex (使用dex反编译器验证)。

Using ADT 21.0.0 64bit on Windows 7 64bit. 在Windows 7 64位上使用ADT 21.0.0 64位。 I tried adding project to build path. 我尝试添加项目来构建路径。 Ticked in the Order and Export, played with order. 在订单和出口中打勾,玩了订单。 I read the official solution , the detailed explanation of that solution, exported as JAR, added to libs. 我阅读了官方解决方案 ,该解决方案详细解释 ,导出为JAR,添加到库中。 Did not work, while the source was without errors. 没有工作,而源没有错误。 Read in the comments that the solution didn't work for everyone. 请阅读评论中的解决方案并不适合所有人。

Was fed up with it and tried a simple but somewhat messier solution: linked the Java project's source to the Android project. 厌倦了它并尝试了一个简单但有点混乱的解决方案:将Java项目的源链接到Android项目。 That did the trick. 这就是诀窍。 Hope that this will help others too. 希望这也能帮助别人。

Don't add it to the project references area, instead go into the Android pane (in the Project Properties), scroll down to the bottom, and add it under Libraries. 不要将它添加到项目引用区域,而是进入Android窗格(在“项目属性”中),向下滚动到底部,然后将其添加到“库”下。

This sometimes works but sometimes produces bizarre Eclipse issues (not sure why), in which case copying the jar directly into the project seems to work best. 这有时会起作用,但有时会产生奇怪的Eclipse问题(不确定原因),在这种情况下,将jar直接复制到项目中似乎效果最好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM