16

I have an android application that utilizes another project as a project reference. i've gone into the application options, build path, and then selected the project in project references.

everything builds and deploys fine, but when i try to run the application and instantiate a class in that project, i'm getting a java.lang.NoClassDefFoundError errrrrr.

i'm no java project expert, so i assume i've cocked something simple up. any ideas?

bryan costanich
  • 1,719
  • 6
  • 20
  • 33
  • 1
    with some more investigation, i believe i have to mark the referenced projects as "Is Library" under the android tab in project options, but every time i click that, it doesn't persist. if i open options again, it has it unchecked. – bryan costanich Dec 31 '10 at 01:59
  • ok, i checked out the entire tree (TFS + eclipse seems to be tricky) and finally got the Is Library to persist, but still getting the NoClassDefFoundError. :( – bryan costanich Dec 31 '10 at 02:04

8 Answers8

23

On Eclipse, go to "Android-Project" -> Properties -> Java Build Path -> Order and Expert, then select the java project you wish to import and move it on top of all the entries.

Matthew
  • 619
  • 1
  • 6
  • 5
10

There is some workaround for this problem. You may check you library as Android library: Right click on project/properties/Android tab - "is library".

And then in your main project: Right click on project/properties/Android tab -> Add (and add your library project).

However, your library has to be created as Eclipse Android project first (so that "Android" section would be available in the project properties).

I have done that and I don't have NoClassDefFoundError anymore. Still this solution is good for early development. If you library is stable, you should make a jar and include it in you main project (and remove references to Android library).

Kacper86
  • 448
  • 1
  • 8
  • 16
2

I am having the same problem. Little research led me to Library Project, mentioned also in this post. But mind this:

"You can also designate an Android project as a library project, which allows it to be shared with other projects that depend on it. Once an Android project is designated as a library project, it cannot be installed onto a device."

You can find more info:

http://developer.android.com/tools/projects/projects-eclipse.html

and also

https://groups.google.com/forum/?fromgroups#!topic/android-developers/C0pddRNGZVs

MSquare
  • 6,311
  • 4
  • 31
  • 37
  • Adding the library with the Android tab did the trick. It is such a pain that there are so many recipes for the exact same thing on this page – Eric Apr 02 '13 at 22:43
2

I am having similar problems. I have an old project that I use as a namespace for some maths and 3D functions - I'd like to share this between android and my other java code. The trouble is, the damn thing crashes as soon as I instansiate even a POD type from my library. My external project doesn't generate a jar file; it's only used as a namespace. I assume that there's some run-time linking issue that's causing this but android, eclipse and java are collectively giving me a grand total of no help whatsoever.

Luther
  • 1,786
  • 3
  • 21
  • 38
  • 1
    I've since managed to get my code to work, sharing code between existing projects. Android clearly doesn't like referencing another project externally but if you got to the project properties and link the source code of your library, it can do whatever internal gubbins it needs to get your code to work. ->properties->Java Build Path->link source... and then add your library from its root directory. – Luther Jan 01 '11 at 07:38
  • Tim, when you did that, did it screw up your packages? i did that and it's now expecting my package names fromt the referenced libraries be src.com... rather than com... because it's enforcing the folder/package convention. i tried browsing to the /src folder instead and linking that, but it says "folder is already a source folder," even though i've removed all references to that project. – bryan costanich Jan 01 '11 at 18:23
  • I haven't encountered that problem, although I did have some issues with some config files conflicts from multiple included source directories that I couldn't resolve, no matter what I tried (there's an option to ignore some files which doesn't seem to work at all). My projects follow the package/folder convention internally so that's probably why I'm not seeing your problem. Whilst Eclipse is great to work with once it's all set up, the initial set up phase can be a real pain! – Luther Jan 04 '11 at 14:35
1

I recently came across a very similar and possibly the same problem. Android doesn't like references to projects and will throw these reflection exceptions if you try to have your android project refer to another project.

It also doesnt like having jars on the build path, what you need to do is export the projects you want to include with your android project and place them in the /libs folder. This folder is usually created when the project is created, if not just make it yourself, it goes in the root of your project. Android will automatically pick them up if theyre in there.

Also very important, if your external project also depends on other projects you need to export those other projects as well, and also place the jars in the /libs folder of the android project.

This is the case for the newest release of android as of 22/07/2012. It may not apply if your'e using an older version of android.

Vegapunk
  • 11
  • 1
0

If your external project depends on other jars in its classpath make sure they are exported and push them to the top of the export list in Eclipse. I had the same NoClassDef error thrown on a class that was in a project but the real problem was with the export order of the jars it depended on. Worked fine after I fixed this.

PhilH
  • 61
  • 1
  • 4
0

Context: Developing a JSP website which interfaces with multiple Android devices across the web. Requires common classes.

Steps taken:

For the java project containing your common classes:

1) (do nothing)

For your Android App project:

1) Project properties->Java Build Path->Projects tab->Add...

2) Project properties->Project References->set check-box accordingly

3) [As Matthew says above (and it was just what I needed)] "Android-Project" -> Properties -> Java Build Path -> Order and Expert, then select the java project you wish to import and move it on top of all the entries.

For your JSP website project:

1) Project properties->Java Build Path->Projects tab->Add...

2) Project properties->Project References->set check-box accordingly

Galleon
  • 19
  • 1
0

You may need to include a JAR of the compiled source of your dependent project alongside the parent. Android will need to have the class files so it can recompile them into Dalvik bytecode and then make them available on the classpath.

This video may help you, taken from this question.

Community
  • 1
  • 1
Alex
  • 8,093
  • 6
  • 49
  • 79
  • 1
    The problem with adding a .jar file is that it doesn't automatically get updated when the referenced project is changed. The referenced project is in active development, and it would be a hideous pain in the ass to have to export it into a jar file every time i make a change. i thought that was the entire point of referencing the project, not the jar. visual studio handles this with ease. – bryan costanich Dec 31 '10 at 17:38
  • 1
    Then introduce into your work processes a proper build system that supports dependency resolution such as Maven, so the JAR that's included is updated each time you build – Alex Jan 01 '11 at 20:29