-2

I've got some sources that do not depend on android. I want to use them as a library in android app projects as well as in non-android java projects. In Eclipse, I don't seem to find the right buttons to make this work. I have read about integrating jars in my project but thats not what I want. I need the sources to be used, not a resulting library. The lib is still under development and I don't want to have to recompile it, export it as jar and copy it into the host project everytime something is changed! Also I dont want to copy the library sources somewhere. I just want to use those exact sources in one development project as well as in another one. In .NET this is a no brainer. After what I googled, in Eclipse this is between hard and impossible.

I would appreciate further ideas on what to do or what to expect!

Jens Mander
  • 430
  • 7
  • 15
  • This is likely not an Eclipse-exclusive problem. How are you handling your dependency management - Gradle, Maven? – Makoto Jul 25 '13 at 00:05
  • -4 in 5 minutes because I mentioned .NET? OMG...! What do you mean by "eclipse-exclusive"? I am not using gradle or maven. This is a simple project. If Ant isn't doing something in the background I am not using any dependency managing. At least not beyond configuring build paths or copying jars manually... – Jens Mander Jul 25 '13 at 00:15
  • These answers may help you ... [How to express inter project dependencies in Eclipse PDE](http://stackoverflow.com/questions/1390074/how-to-express-inter-project-dependencies-in-eclipse-pde) [multiple dependent android projects in eclipse](http://stackoverflow.com/questions/3041183/multiple-dependent-android-projects-in-eclipse) – andy256 Jul 25 '13 at 00:25

3 Answers3

1

You say you want to use this code as a library in multiple projects. The way to do this is to do what you're opposed to doing: make it a project, have it build as a JAR, and have the build technology you're using (which sounds like Eclipse) manage the fact that it needs to be packaged into the resulting final output.

However, that doesn't mean that you'll build the JAR and drop it into your project's lib directory, which seems to be the thing you're afraid of. Eclipse/Maven/Ant can all be configured to rebuild the dependent libraries as needed and then package them automatically into the build artifact of the primary target, without any pain for you.

Tim
  • 2,027
  • 15
  • 24
  • That sounds promising! How do I do that? I already tried to reference my java library through Android/Library, Java Build Path/Projects, Java Build Path/Libraries and played with Order and Export, all to no avail. The android runtime gives me "Could not find class..." every time. (I can't believe, I have to manually configure xml-files. That would seem like a very big anachronism to me.) – Jens Mander Jul 28 '13 at 18:31
  • Is your library project of type Android or Java? If it's of type Android, convert to a Java project, and then it should get packaged in properly (via Java Build Path/Projects). More info at http://stackoverflow.com/questions/2047105/including-other-eclipse-projects-in-an-android-application-project. – Tim Jul 29 '13 at 18:59
  • 1
    Library is a java project (two classes) that I want to use in an android app. Unfortunately, it doesn't seem to find its way into the apk. Runtime says "Could not find class...", refering to one of the classes from the library. It doesn't matter how I reference the lib from the app. I don't think I have a strange config here. Just a relatively fresh install of ADT. If I put the two classes into an android lib and refer to that from the app via Android/Library, everything works as expected. But since I'm pretty sure I will use similar setups all the time, I am looking for the overall solution. – Jens Mander Jul 30 '13 at 00:11
1

without any dependency management (meaning: built-in tools from eclipse should manage dependencies) , the probably easiest way would be:

  1. name your sourcefolders different (e.g. appsrc, libsrc, etc).
  2. use workspace relative linking of a new folder (rightclick on project -> new folder -> advanced -> link to alternative location -> Variables WORKSPACE -> extend -> select your libraries project source -> ok. -> finish
  3. rightclick your project -> new source folder -> browse -> use your linked folder.

default package name is discouraged (and didn't work for me for a quick test). any usage of libraries inside your library project may or may not result in unexcepted behaviour with parsing errors or runtime errors, i never tried it.

never used this in a more complex project setting, but that should do it for you.

Christian R.
  • 1,528
  • 9
  • 16
  • if you are gonna use libs for the lib project, you will most probably have to add them to both projects, but as said, i never used it that way. what i did until now was nothing else than linking basic models and interfaces that were used inside a android client and a java server. – Christian R. Jul 25 '13 at 00:38
0

I link the java source folders that I want to the android project from Project Properties -> Java Build Path -> Source -> Link Source. You can link the same folder to multiple projects, so you can reuse code without duplicating it and without needing to compile it separately.

The only problem is that when I search for one of those classes in Eclipse, it lists it multiple times, once for each link to that folder. So the more I reuse a folder, the more it clutters my search results.

When I first started with Android, I could include another Eclipse project from Java Build Path -> Projects. I liked that because it worked the same way as non-android eclipse projects did. But it stopped working when I upgraded the Android SDK. The compile still works, but when I try to run the application, it crashes with class-not-found exceptions.

user13097
  • 343
  • 3
  • 12