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
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.
- Create a "Shared1" Java Library containing the shared classes & their source code
- Add the "Shared1" library to the desktop Java project
- Create a "Shared" Android project... in Properties, tick "Is Library" and set up a Custom location "../Shared1.dist"
- Add the "Shared" project to the Libraries in the Android application project
- 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!!!
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.
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.
Unfortunately I do not really understand what does it mean project with 2 modules.
Typical solution for this problem is 3 projects:
- proyecto android
- otro proyecto
- 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:
<dependency> <groupId>com.mycompany</groupId> <artifactId>util</artifactId> <version>1.0</version> </dependency>