12

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.

4 Answers 4

10

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:

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

More in this link.

Sign up to request clarification or add additional context in comments.

4 Comments

I still get the ClassNotFound exception when using your method. The only way I have found that allows running the app without error, is adding AI.jar manually to the build path (only then I see the apk size significantly increase, which means AI classes are exported in the apk)
@Sebastien, you are probably right, Eclipse ADT plugin has a very inflexible build life cycle, apparently it doesn't support auto build any dependent project except Android library project then auto add the generated lib.jar into build path. The only way is explicitly add the external lib.jar into project's build path. I know some build tools like Maven is more flexible and provide powerful configuration on Android build life cycle.
@Sebastien, wroth to check out r17 release.
None of the above is working for me. Java project is not displayed in the Library list. Even if I add project to the build path and tick it on the Order and Export tab I get the java.lang.NoClassDefFoundError error for the class in the other project. I have a 21.0.0 release of the ADT.
3

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.

Workaround for me (Windows with NTFS filesystem):
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. 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.

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

Comments

0

Using ADT 21.0.0 64bit on Windows 7 64bit. 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. 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. That did the trick. Hope that this will help others too.

Comments

-1

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.

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.

2 Comments

Tried this, but as it is not an Android library project, I cannot add it to the Library area you're mentioning.
Ah, I see. This looks like it might help geekswithblogs.net/cyberycon/archive/2011/05/17/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.