如何包含在Android项目中的Java项目? [英] How to include a Java project in an Android project?

查看:218
本文介绍了如何包含在Android项目中的Java项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse来开发一个Android的棋盘游戏。 我已经开发了用户界面在Android项目。

I am using Eclipse to develop an Android board game. I have developed the UI in an Android project.

在另一方面,我所研制的AI在一个普通的Java项目,因为我希望能够没有Android模拟器的所有限制,以测试它(我没有找到运行$ C任何其他方式$ c。使用我的Windows JVM)。

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).

现在谈到的时候,我想'加入'两个项目(其中细独立工作),这是使用AI Java类的用户界面。

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.

这是我曾尝试:

在我的Andr​​oid项目的属性的>的项目参考的,我选中的Java项目。 这使我能够建立一个没有错误的Andr​​oid项目(其中实例化一个AI对象)。

In my Android project Properties > Projects References, I ticked the Java project. This allows me to build without error the Android project (which instantiates an AI object).

但它在运行时失败,出现此错误:

But it fails at runtime with this error:

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

什么是正确的方法,包括人工智能Java项目到我的Andr​​oid项目?

(注:我还是希望能够开发和测试的AI作为一个普通的Java应用程序,所以我认为使用的的或复制所有来源的Andr​​oid项目将不适合我需要)

(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)

编辑:

我希望,新的ADT V17可以解决问题,但事实并非如此。我已经试过几乎所有可用的选项,包括项目在我的Andr​​oid项目:

I was hoping that the new ADT v17 would solve the problem, but it didn't. I have tried virtually every option available to include a project in my Android project:

  • 添加或链接的源文件夹
  • 添加项目的(Java构建路径>项目标签)
  • 添加类文件夹或库的(Java构建路径>库选项卡)
  • 在滴答作响所有的previously进口projets /库,导出
  • Adding or linking a source folder
  • Adding a Project (Java Build Path > Projects tab)
  • Adding a Class Folder or a Library (Java Build Path > Library tab)
  • Ticking all the previously imported projets/libraries as Exported

这允许编译和运行没有错误添加Java项目的JAR到Android项目构建路径。

The only way that allows compiling and running without error is adding the JAR of the Java Project to the Android project build path.

推荐答案

项目只引用添加相关的项目源$ C ​​$ C(你的AI项目)作为软(也许薄弱是一个更准确的字)的参考,你的爱项目未添加到您的Andr​​oid项目构建路径。所以实际ai.jar没有导出到当Eclipse构建应用程序的最终的apk。

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. so the actual ai.jar is not exported into the final apk when Eclipse build your app.

您应该添加AI项目到Android项目构建路径:

You should add AI project into Android project build path:

  1. 右键点击你的android项目,选择构建路径 - 配置构建路径,在项目选项卡(在构建路径中必需项目),此处添加您的AI项目
  2. 然后按顺序和导出选项卡(建设类路径顺序和出口项),勾选新添加的AI项目出现INT列表。

第2步可能是可选的,这应该添加AI项目在Android项目参考,并出口ai.jar到final.apk当Eclipse构建您的Andr​​oid项目。

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.

从ADT 17.0.0的更新:

Update from ADT 17.0.0:

Android开发团队刚刚发布的SDK R17与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:

Eclipse的具体修改

Eclipse specific changes

所谓的库项目动态类路径容器已被重命名为Android的依赖,因为它现在包含的不仅仅是库项目。

The dynamic classpath container called "Library Projects" has been renamed to "Android Dependencies" as it now contains more than just Library Projects.

容器将现在也可以填充的是由库项目引用的Java-只项目。如果这些Java项目还引用其他的Java项目和/或jar文件,他们将被自动添加(通过用户库引用的jar文件以及支持)。

The container will now also be populated with Java-only projects that are referenced by Library Projects. 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).

重要提示:如果引用被设置在引用的项目要导出这只是发生。请注意,这不是默认添加一个项目或JAR文件添加到项目构建路径时。   库项目(和它们的库/ *。jar文件的内容)始终导出。这种变化只会影响纯Java的项目和自己的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. Library Projects (and the content of their libs/*.jar files) is always exported. This change only impacts Java-only projects and their own jar files.

再次重复(这两个项目和JAR文件)被检测到并删除。

Again, duplicates (both projects and jar files) are detected and removed.

更多此链接

这篇关于如何包含在Android项目中的Java项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆