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

标签 java android eclipse

我正在使用 Eclipse 开发 Android 棋盘游戏。 我在一个 Android 项目中开发了 UI。

另一方面,我在常规 Java 项目中开发了 AI,因为我希望能够在不受 Android 模拟器所有限制的情况下测试它(我没有找到任何其他方式来运行代码我的 Windows JVM)。

现在我想“加入”这两个项目(独立工作良好),即使用 UI 中的 AI Java 类。

这是我试过的:

在我的 Android 项目 Properties> Projects References 中,我勾选了 Java 项目。 这使我能够毫无错误地构建 Android 项目(它实例化一个 AI 对象)。

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

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

将 AI Java 项目包含到我的 Android 项目中的正确方法是什么?

(注意:我仍然希望能够像普通 Java 应用程序一样开发和测试 AI,所以我认为使用 jar 或将所有源代码复制到 Android 项目不适合我需要)

编辑:

我希望新的 ADT v17 能够解决这个问题,但事实并非如此。我已经尝试了几乎所有可用于在我的 Android 项目中包含项目的选项:

  • 添加或链接源文件夹
  • 添加项目(Java 构建路径 > 项目选项卡)
  • 添加类文件夹或库(Java 构建路径 > 库选项卡)
  • 将所有先前导入的项目/库勾选为已导出

只有将Java Project的JAR包添加到Android工程的构建路径中,才能编译运行不报错。

最佳答案

Projects References 仅添加依赖项目源代码(您的 AI 项目)作为软(也许 weak 是一个更准确的词)引用,您的 AI 项目不会添加到您的 Android Project Build 路径中。因此当 Eclipse 构建您的应用程序时,实际的 ai.jar 不会导出到最终的 apk 中。

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

  1. 右键单击您的 android 项目,选择构建路径 - 配置构建路径,在项目选项卡(构建路径上的必需项目)中,在此处添加您的 AI 项目。
  2. 然后在 Order and Export 选项卡(Build class path order and exported entries)中,勾选出现在列表中的新添加的 AI 项目。

步骤 2 可能是可选的,这应该在 Android 项目中添加 AI 项目作为引用,并在 Eclipse 构建您的 Android 项目时将 ai.jar 导出到 final.apk。

从 ADT 17.0.0 更新:

Android 开发团队刚刚发布了带有 ADT 17.0.0 的 SDK r17,它声称现在可以正确处理这些用例:

Eclipse specific changes

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

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

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.

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

更多信息 this link .

关于java - 如何在Android项目中包含Java项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9649156/

相关文章:

java - java中如何将字节转换为二进制数而不是字符串

java - 使用 FFT 比较两个音频文件是唯一的方法吗?

java - 将正则表达式的 for 循环更改为 while 循环

java - 为什么我的 JsonObjectRequest 不工作?

java - 可以使用 lambda 表达作为 Function 的枚举的实现吗?

java - 返回当前的 android 布局或屏幕宽度以进行调试

android - 获取最小显示亮度

java - 有哪些不同的 Eclipse 快捷方式可用?

java - 在 Mac 上安装 Axis、Tomcat eclipse

c++ - 调用带有 void 参数的方法时的光标放置