18

Since the latest android sdk i am unable to run my android applications from eclipse anymore because they cant find classes which i have in other eclipse projects, which are references via the 'normal' build path.

this has definitely worked before. and there are no compile errors in eclipse.

could it be that, now you have to mark those projects as android library projects in case they are referenced by an android project.

edit: it seems that this is the reason why it is broken: http://tools.android.com/recent/dealingwithdependenciesinandroidprojects

but i still have to figure out how i should now reference my "normal" java projects to the android app project.

clamp
  • 33,000
  • 75
  • 203
  • 299
  • What is the runtime error? ClassNotFound, or ClassDefNotFound? – Ognyan Mar 27 '12 at 14:04
  • 1
    java.lang.NoClassDefFoundError – clamp Mar 27 '12 at 14:28
  • 2
    _this has definitely worked before_, I don't know how you make it work. I also have android project that reference other regular java project, and the apk works only if I explicitly add the regular java project generated jar into android project build path, Looks like the latest SDK r17 completely screw this behaviour and now only accept library jars from the predefined libs folder. I haven't found a workaround yet. – yorkw Mar 28 '12 at 02:50
  • It's odd that this used to work for me, too, and now broke. – ashes999 Jun 24 '13 at 01:45
  • This seems to work now - adt version 22.6.1. – AlikElzin-kilaka Apr 07 '14 at 13:59

5 Answers5

18

In the link @clamp provided there is the following sentence:

The container will now also be populated with Java-only projects that are referenced by Library Projects.

This means, that we can use a empty library project as a "glue" project between the android project and the regular eclipse project. Just set it up like this:

android --> "glue" project --> regular project

--> means depends on

The glue project has to export the regular project and has to be marked as library. In the android project only the library project has to be referenced.

Now using a regular eclipse project works again for me with the minimal overhead of the "glue" project.

thalador
  • 834
  • 8
  • 23
  • while i dont like this solution, i will still accept it as the answer, since it works. – clamp Dec 27 '12 at 13:19
  • 1
    This does not work for me. Since I upgraded to version 21 I can't manage to add external Java projects to Android projects. Tried the direct approach and this indirect one, but without any luck, still getting the error, and .apk file doesn't contain required classes. – Andras Balázs Lajtha Mar 18 '13 at 16:02
  • 1
    Does this still work for you? I couldn't get it to work. I create an Android GlueProject which requires my Java LibProject (under Properties>Java Build Path>Projects tab). I also ensure I export the LibProject by checking it (under Properties>Java Build Path>Order and Export), and have the GlueProject marked as a Library project by checking "Is Library" (under Properties>Android). In my Android project where I want to use LibProject, I add a reference to GlueProject (under Properties>Android). My Android project compiles, but gets a NoClassDefFoundError when it runs :/ – Felix May 26 '13 at 21:36
  • 3
    Scratch that, your solution works perfectly :) The problem was that my Java project was using Java 7 not 6. Switching to 6 made everything work. – Felix May 26 '13 at 22:11
  • 1
    This just doesn't work for me. The glue project's jar file doesn't contain the the class files of the referenced Java project and consequently a NoClassDefFoundError. I even tried to reference the bin folder directly - still no success. And Yes - I checked the boxes in the 'Order and Export' tab. Can someone post the `.classpath` file? – AlikElzin-kilaka Oct 19 '13 at 18:57
12

Since the latest update to the eclipse build tools you have to also tick the referenced projects in the 'Order and Export' tab in 'Java Build Path'. This fixed the same problem for me. Hope that helps!

kingraam
  • 1,521
  • 11
  • 18
  • Ah shame, i was sure that was it. Do you definitely have all your referenced java projects in 'Projects', and have them all ticked in 'Order and Export' along with 'Android Dependencies'. Also make sure any library jars you are using in your project are in a folder called 'libs' (as long as they are in there you don't need to include them manually in the build path, the new eclipse tools will include them automatically). – kingraam Mar 27 '12 at 15:21
  • yep, i just doublechecked that and actually i would be surprised if this would have any influence on the issue. since order and export is only for projects which should forward their dependencies to any other projects that have it references. (like a 2-layer dependency hierarchy) – clamp Mar 27 '12 at 15:26
  • I think the solution you suggested only work for Java Project referenced by Android Library Project, as stated in the [changelog](http://tools.android.com/recent/dealingwithdependenciesinandroidprojects), but not in this case: Java Project directly referenced by the main Android Project. – yorkw Mar 28 '12 at 02:52
  • After banging my head finding a solution with no luck, this simple step worked for me. Thanks. – Federico Cristina Feb 15 '13 at 11:03
  • This + change in projects that are libraries to java6 did the trick. Thanks. – Marcin Waśniowski Jun 26 '14 at 21:34
3

As already mentioned, Android will not directly compile and use your Java project files. Instead it takes only precompiled class files.

Here is my workaround, which I prefer because I need no "glue" project just for Android libraries. The main idea is to provide a pre-built JAR to Android, while still using the source code of the libraries while working in Eclipse:

  1. First of all make sure, that the library projects are compiled using Java 6. Java 7 will not work, Android will silently ignore those libraries. So check both the workspace settings (in Preferences -> Java -> Compiler) and the individual project settings. Choose 1.6 as the compiler compliance level.
  2. Right click your Android project -> Properties -> Java Build Path -> Projects. Add all Java projects you want to use in your Android project.
  3. Now, Eclipse knows the classes (but not yet the final Android build), so build errors are resolved and jumping into a class leads directly to the original source file of your Java project.
  4. Add an ant buildscript in the directory of your Android project. This buildscript will create a JAR containing all the .class files of your projects into the libs subdirectory (and this is the step which lets Android know your .class files!). See below for the code.
  5. Now, and always when the Java projects have changed, rebuild the JAR file by executing the buildscript. This can also be automated, see for example "Want an eclipse java project to run ant build files automatically"

Here is the code:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_jar" name="Create Jar with classes from Java projects">
<target name="create_jar">
    <jar destfile="libs/java-projects.jar">
        <!-- "bin" is the class output folder -->
        <fileset dir="path/to/first/java/project/bin"/> 
        <fileset dir="path/to/second/java/project/bin"/>

        <fileset dir="path/to/last/java/project/bin"/>
    </jar>
</target>

Community
  • 1
  • 1
Andi
  • 3,234
  • 4
  • 32
  • 37
  • Individual projects > Java compiler > JDK compliance for the particular project should suffice, no ? And setting the compiler to a 1-6 execution environment (pointing to an actual 1.6 JVM). You may gave some insights for my question : http://stackoverflow.com/questions/18981837/android-library-project-does-not-export-its-class-dependencies – Mr_and_Mrs_D Sep 24 '13 at 13:24
1

I added an ant project builder that copies the class files from the dependent project. The ant file resides in the android project and copies the class files from the referenced project. Here's the ant:

<?xml version="1.0" encoding="UTF-8"?>
<project name="CommonsCopy" default="copy" basedir=".">
    <target name="copy">
        <copy todir="bin/classes">
            <fileset dir="../Commons/bin" includes="**/*.class">
            </fileset>
        </copy>
    </target>
</project>

2 things to remember:

  1. Find a way to copy the jar files that the referenced java project uses, or just put them directly into the Android libs folder.
  2. The referenced project must be compiled with in the 1.6 compliance level.
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • Mind that the big disadvantage here is that that if you change the java project, you'll need to change some file in the Android project to initiate the ant builder. – AlikElzin-kilaka Apr 07 '14 at 14:00
0

You can define two projects (one Android, one vanilla Java) and have one refer to the other project's source files using mklink command in Windows to create a directory junction or a file hard link. I don't know the linux equivalent but there must be one. If you edit a source file from one project then the other project will be out of sync and you just have to refresh it. This way you don't have to mess with jars or the Eclipse build process.

Neddie
  • 141
  • 1
  • 6