6

Is it possible to import an existing Java project which is in my workspace, in an Android project which is also in my workspace? The Java project doesn't use stuff that is incompatible with Android. I could do it using a .JAR, but since I'm modifying the Java project a lot, I would like to import the whole source code and change it once in a while. Is it possible? Thanks

Paulo Barros
  • 2,600
  • 7
  • 24
  • 35

3 Answers3

4

This is somewhat of a hack, but worked for my project:

  1. Add both Android project and plain Java project to workspace
  2. Go to properties for Android project -> Java Build Path -> Source
  3. Click Link Source, in "Folder name", make sure to select the root directory of the plain Java project's src tree
  4. Use a descriptive name for "Linked folder location", like "java_project_src".
  5. Press Finish and compile

This setup will compile the plain-java code as Dalvik bytecode. In my case, I had pure java code with very limited dependencies, but wanted to share the pure java code with non-Android projects, instead of separately maintaining these files in every project.

raider33
  • 1,533
  • 1
  • 17
  • 20
1

I think you are talking about referencing other projects into the current project, if this is the case then you can do this by Right click on project, properties->Project Reference.

Paresh Mayani
  • 122,920
  • 69
  • 234
  • 290
  • 1
    If I have 2 Java projects it works. But I have one Android project and one Java project. I link the java project in the Android project but I still can't use it's content. I tried creating a Java project and did the same and it worked. – Paulo Barros Oct 12 '11 at 07:44
  • 1
    -1, this does not work when referencing a Java project into an Android project, which was clearly stated in the question. Works on Java to Java only. – tbraun Jul 15 '14 at 09:32
  • 1
    @tbraun hmm personally I haven't tried it by importing Java projects! but ready to know what can be a best resolution for this question. – Paresh Mayani Jul 15 '14 at 09:39
  • @PareshMayani yeah sorry to have downvoted your answer, I'm just trying to make other people save some time. Couldn't find a solution either, have to generate jar and import, which is not great :( – tbraun Jul 15 '14 at 10:04
  • @tbraun no no it's totally OK. Feel free to update my answer with the details you know, and it could help others too! – Paresh Mayani Jul 15 '14 at 10:08
0

You can link another projects to your current project. Right click on your main project, choose Build Path -> Configure Build Path... In the dialog choose the Projects tab, then click Add.. button. Now choose the project you want to link and click OK. You'll now be able to use this project's sources inside your main project. Hope this helps.

Egor
  • 37,239
  • 10
  • 109
  • 127
  • -1, this does not work when referencing a Java project into an Android project, which was clearly stated in the question. Works on Java to Java only. – tbraun Jul 15 '14 at 09:32