3

I'm writing an Android application and there's some Java code in it that's somewhat sophisticated and therefore hard to verify the correctness of in the Android environment. I would like to run this code in a desktop environment where I have more tools with which to examine the output of this code while still using it in my Android application.

My attempted solution is to have three different projects in Eclipse. My Android project and two plain (non-Android) Java projects. One Java project has the sophisticated code that I want to use in Android and the other is a test program that verifies the correctness of the former project. The latter project has already been useful in debugging the former.

However, so far, my attempts to use the Java project in my Android project appears to work in the IDE but when I actually run the Android application, the NoClassDefFoundError exception is thrown whenever I try to access any of the classes. Obviously, that code is not being recompiled into the .dex file but why not?

I could go into detail about what I've done so far but I can't help but think that what I'm doing is a pretty standard and simple thing and there's a plain way of doing it, even though I can't find anyone doing quite what I'm trying. Can someone describe to me how this is done?

3
  • Do you have all of your activities declared in the manifest. I'm confused on why you need to create a separate project. Whatever IDE you are using while following the Android docs docsshould make it simple enough to check the correctness of your code. It's hard to know exactly what's going on without code but your classes obviously aren't defined somewhere Commented Nov 6, 2012 at 18:10
  • The code that's in the plain Java project is not Android code. If it were, surely it couldn't be in a plain Java project. I would have, at most, simply made another Android project and use it as a library project. More probably, I'd just keep all the Android code in the same project. There's more to verifying code correctness than just looking at your code, which is why the Eclipse is not good enough. In my case, I need to actually run the code and look at its output with tools not available in the Android environment, hence the desktop test program... Commented Nov 6, 2012 at 18:59
  • In the Android project, You also need add your plain old java projects to the Order and Export list (Properties -> Java Build Path -> Order and Export) Commented Nov 6, 2012 at 20:27

2 Answers 2

1

Luckily, I found the answer to my own question and I thought I'd share it here to help others in the same situation. It turned out to be very simple...

What I was already doing would have normally worked, which should have been a big clue to me since I have actually done this before, successfully. All you have to do is, under your Android project's Properties > Java Build Path > Projects, add the plain Java project to your "Required projects on the build path" and then under Properties > Java Build Path > Order and Export, check the checkbox of that same project in the "Build class path order and exported entries" list and everything should just work.

From within Eclipse, there's nothing else you need to do to get this setup to work. It's only when you're compiling from the command line that you need to build Java Jars and import them as libraries but I'm not doing that (yet).

Finally, this wasn't working for me because I just happened to be compiling my plain Java project under JDK 1.7 compliance, while my Android project was compiled under JDK 1.6. This is verified by the output on the Console pane, reporting "Dx bad class file magic (cafebabe) or version." This error message goes away when both projects are compiled under the same compliance level and, not coincidentally, the Android program runs properly.

Thank you to everyone who tried to help and I hope this answer is helpful to someone out there!

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

Comments

0

Would it not work if you made your other plain java project into an Android project and use it to monitor the output on the device?

1 Comment

Unfortunately, no. The tools I need to monitor my output only exist on my desktop system. If they were available in Android, I would have simply put all my Android code into the same Android project and use said tools. That's why I have two plain Java projects: one to use as a library which can be used with either an Android project or a desktop project and the other as a plain desktop application which is susceptible to my desktop tools...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.