Share sourcecode between android- and javaprojects - java

There is a class, that compiles with the android platform and the 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.
Thanks for all answers.

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!!!

Create a 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:
android project
other project
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>

Related

How do I make use of a third-party java library github project?

When there's no obvious compiled jars provided on a github project page, I've been downloading the clear code and kludging them into subdirectories in my codebase to make use of third party code (or, most of the time, just not using the code at all). I know that's not the right way to go about this; what is?
There's a popular post about importing github projects into Eclipse:
Importing a GitHub project into Eclipse
...and I've recently learned how to make my own projects depend on one another. Is this the practice, then, to fill up your workspace with projects you're not actually going to work on, so that you can declare dependencies? I remember making dependency references to web resources in the past (itch.io, maybe?) in some configuration, where I didn't have to go download the code. Is there a more elegant solution like that for github?
I'm using Eclipse, and the current library I'd like to use is: https://github.com/FasterXML/jackson
If you're trying to just use FasterXML, the jars are hosted on MvnRepository as well as a lot of other projects.
If a github project's artifact isn't available on MvnRepository or some other online repository you could potentially clone, build the war/jar file yourself, and then manually import it into your project instead of copy and pasting code directly into your project. This would give you access to the library.

Multi project gradle build deployed as 2 separate applications (one is a play app)

I am working on migrating a java multi project build based on net beans into a gradle build.
The motivation is not to be dependant on the IDE and having gradle manage all the dependencies.
we currently have one folder that contains all the projects and they have their inter dependencies.
These projects are deployed in two separate applications. One (lets call it App1) in a play framework application (v1.4) that depends on some other projects, and the other (App2) is a regular java application deployed on a separate machine.
Some projects are common to both applications.
I currently managed to build App2 using gradle, so I have a build.gradle & settings.gradle files in the top folder
now I want to add the build files for App1 (the play application).
Questions:
1. I couldn't find any explanations on combining play 1.x with gradle. Does anyone know how to do it?
2. How should I arrange my top level gradle files for building 2 separate applications
3. I want to be able to use the IDE for refactoring code that is common to both apps. how do I do that
Thanks
Answer to your questions
Suppose your projects structure looks like in below image
Please include all sub projects in top level project's settings.gradle like below image
Use the dependencies in sub projects like below image

How to manage native shared library dependencies in separate Android Eclipse projects?

I have an android project in Eclipse which simply contains a "jni" directory containing c++ code which gets compiled into a native shared library, let's say libmod1.so. Then I have another android "front-end" project, which also contains a "jni" directory. I've set that project up to create a native shared library (let's say libwrapper.so) which I want to act as an interface to access libmod1.so, calling its functions etc.
I've seen a few questions about writing Android.mk files to express shared library dependencies but none I've seen refer to linking custom made libraries in separate Eclipse projects.
Rather than rely on a complicated make file though, if possible I'd like to instruct Eclipse to compile and copy the libmod1.so library into the "jni" directory of the "front-end" project whenever its built. From there I can compile it into the libwrapper.so library.
Does anyone know how to achieve this in Eclipse?
Or, can anyone suggest a better method of achieving the result I'm after? I'd prefer separate projects because I want to use the libmod1.so library with various "front-end" projects in future and so I want it to be in its own project.

Refactor classes into a separate project

I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.

Prevent JAR from being imported into Eclipse Java project

I have a Java project which is heavily used by all sorts of other Java and Android projects. The project contains some JAR libraries which shall be used by all projects, except for the Android one (in fact the Android project is a Android library project to be precise).
I marked the JARs as "export" in the Eclipse build path preferences of the Java project. However, the Android project shouldn't import these libraries (as they are Java libraries which make use of some classes which are not available on Android), but it shall import the rest of the code (which doesn't really use the libraries, but they are stored in there for convenience reasons and to ensure, that all other projects use the same library.
How can I prevent the JARs from being exported to the Android projects?
You can prevent all jars from being exported so that only the common project is a dependency for each project that needs it.
Then you can change the build path of each project to only include its necessary jars through the add jar.. dialog in build properties.
That's the easiest way.
A more extreme way would be to move to maven and then eclipse will only include the jars you specify in the pom - though that's a load of extra work for not much gain.
Alternatively, you could split the android specific code into a android-common separate project and then make your common project depend on it and export it - then your android project could rely on this android-common project instead of the existing common project.

Categories