Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android project referencing "normal" java project in eclipse since sdk tools update 17

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.

like image 279
clamp Avatar asked Mar 27 '12 12:03

clamp


4 Answers

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.

like image 68
thalador Avatar answered Nov 15 '22 20:11

thalador


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!

like image 12
kingraam Avatar answered Nov 15 '22 18:11

kingraam


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>

like image 3
Andi Avatar answered Nov 15 '22 20:11

Andi


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.
like image 1
AlikElzin-kilaka Avatar answered Nov 15 '22 20:11

AlikElzin-kilaka