Comparta el código fuente entre android y javaprojects

There is a class, that compiles with the android platform y jdk. Now, the idea is, to use that class in two projects, one is a android one and the other result in a java application.

So, I cretae a project with two modules, one for the android and one for the java solution. All works fine, with copy&paste the class-source between the two modules, but that is not very comfortable to use.

So, the question is, how it can be done without copy&paste. I would prefer a intellij solution, but I am willing to change to eclipse if needed.

Gracias por todas las respuestas.

preguntado el 31 de enero de 12 a las 08:01

4 Respuestas

FWIW, here's what I have working using NetBeans 7.1.1 ... it's possible that some of these steps aren't needed, but after a couple of days experimenting this seems to work.

  1. Create a "Shared1" Java Library containing the shared classes & their source code
  2. Add the "Shared1" library to the desktop Java project
  3. Create a "Shared" Android project... in Properties, tick "Is Library" and set up a Custom location "../Shared1.dist"
  4. Add the "Shared" project to the Libraries in the Android application project
  5. Add "Shared1" as a JAR library (Custom location "../Shared1.dist") in the Android application project

The classes in the "Shared1" project are now usable in both the desktop environment and the Android environment.

BEWARE!!!! Using java.awt classes in the "Shared" library classes doesn't generate any compiler warnings, but causes the Android application to crash. Subsequent runs also crash, even if the java.awt reference has been removed and Clean & Build done on everything!!!

Respondido 04 Abr '12, 05:04

Crear un MyLibrary project containing the shared class, package it as a jar (MyLibrary.jar), and add this jar as a library of the Java and Android projects.

Respondido el 31 de enero de 12 a las 12:01

There was a similar answer earlier in that thread. I didnt want to repeat. - Urss

Since you say the class is "common", that implies it doesn't use any Android-specific classes. In this case, you can just distribute the common code as a JAR library to the java and Android projects.

Respondido el 31 de enero de 12 a las 12:01

Yes, it works well with making a JAR library. But doesn't that result in a lose of performance? The JAR have to translate to dalvik (or what ever, dont know exactly). - Urss

A class is a class. Whether it is in your android project directly or in a jar, Android has to do the same work. - JB Nizet

No loss in runtime performance. This is because Android SDK (specifically, the dex tool) will compile all your your .class files together into Dalvik bytecode (.dex file). So, yes, there is loss in compile time performance; but this is the standard procedure followed. - curioso

It'd be great if you could share your solution; and upvote or accept the answers here if they helped you. - curioso

I cant vote cause I need more reputation. And here is the solution for intellij: 1) Create a new empty project. 2) add java module for the lib 3) add java module for the desktopapp and set moduledepencies to the lib 4) add android module for the androidapp and set depencies to the lib - Urss

Unfortunately I do not really understand what does it mean project with 2 modules.

Typical solution for this problem is 3 projects:

  1. proyecto android
  2. otro proyecto
  3. shared (utility) project

Projects 1 and 2 depend on project 3.

If you are using maven it is pretty simple to do: you just have to add appropriate dependency tag. For example if your utility project's artifact is "com.mycompany.util" add definition like the following to pom.xml of your dependent projects:


Respondido el 31 de enero de 12 a las 12:01

The Intellij IDE only loads one project at the same time. I don't know if other ide can handle more than one. So if I dont want to use more than one IDE window, I have to use modules. Afaik, modules are similar to projects, but the ide can load many of them. You can create a new module, depending on a existing project. - Urss

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.