Android项目属于JAVA项目吗,如何包含在Android项目中的Java项目?

I am using Eclipse to develop an Android board game.

I have developed the UI in an Android project.

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

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.

This is what I have tried:

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

What would be the correct way to include the AI Java Project into my Android Project ?

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

EDIT:

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:

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

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

解决方案

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.

You should add AI project into Android project build path:

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.

Then in Order and Export tab (Build class path order and exported entries), tick your newly added AI project appeared int the list.

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.

Update from 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 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值