Sharing Java model classes between Eclipse (Maven) and Android Studio (Gradle) - java

I have created a Java webservice in Eclipse which provides a REST API and an Android project (in Android Studio) which consumes said API. The problem I am facing right now is that I want to use the same model/domain classes in both the Java webservice and the Android application (the webservice uses Maven whereas the Android application uses Gradle).
At the moment I am manually mirroring any changes I make to the model classes in both the Java webservice and the Android project. This is however very tedious and obviously not a good solution.
My questions is how I can solve this issue. I thought about creating a Maven module containing the model classes and importing them in the Java webservice and the Android project. However I am uncertain if this a solution which will work.
Are there any better (and maybe obvious) solutions I am not seeing? Thanks in advance for any help!
Edit: Is it actually smart to share the whole model classes? I clearly don't want to use all the attributes from the webservice in the Android project (e.g. passwords will never be stored in the Android project but have to be stored in the webservice).

You could create a library for your model, and share it between the provider (REST API) and the consumer (your Android app). That could be done with a multi-module maven project, where you can have a module for your model layer and another module for your REST API layer that uses the previous model artifact.
Doing so, you could install the model library in your local repository (or deploy it in whatever centralized repository you may need) and use it from your Android application.
But I would advice you against sharing the model that way, because you would be tight-coupling your consumer and provider at the model level. That would make it harder to evolve them independently.

Related

Android client and java server in the same git project

I am developing a simple Android application and it should communicate with server using REST api. Both client and server use kotlin and serialize classes to json with Jackson and API looks like:
class xRequest { .. }
class xResponse { .. }
Client and server have their own git repositories and I use Android Studio and Idea to work with them separately. This leads to class declaration duplication as they both need to know API.
What's the best way to get rid of duplication? I could move API to some third project and then build - publish - add dependency on it but that's a lot of work during development.
Is it a good idea to move them to the single project so it will look the way:
my project
api
android-client
server
Thanks
It's a classic duplication problem. What we usually do in this case?
Extract duplicated code to the separate entity and reuse it.
So, I would introduce a new pure java project called core and add it as a dependency to android-client and server.

Gradle: Building different jars for Java and Android

I recently completed the initial work on a Firebase library. This library is currently implemented for use with Firebase-server (Java), but I would also like to build a version for use with Firebase-android. Making the Android version should be a simple matter of changing the Firebase SDK being used and implementing the Android version of my Firebase wrapper interfaces.
When building the project, I would like it to create a separate artifact for both Java servers and Android. I imagine this is something that could be done using Gradle. How would I go about doing this?
As a followup question, would it be necessary or a good idea to include the respective Firebase SDK dependencies in my build artifacts?
I believe the process I was looking for is known as creating a multi-project Gradle build.
I created three modules: core, server, and android. The core module contains the code required by the two derivative modules, while the derivative modules import the Firebase SDKs they need and implement the core interfaces in the way they require.

How to share Java classes with Android project

We are building an Android application with a server backend which is a simple Java project. These two projects should now share some classes.
We are developing the Android app with Android Studio and the server backend with Intellij IDEA.
We have been unable to properly import the backend project or use it as a dependency properly. How do you do this?
Usually when I have situation like this, I create new module/project called "Commons" and put there all classes shared between Client and Backend. Then I add it as a dependency for both Client and Backend.
As long as you don't use sophisticated Java tricks that are not supported by Android (like lambdas introduced in Java 8), everything will work fine.
It's much better than creating jar library on your own, since you don't have to update it manually everytime you change something in it.
Please write a comment if you have any further questions :)
I suppose that you connect the android app with service via REST or similar.
For example, you could have a User class in the server and User class in the app. This class might be the same and I supose that you want import service classes in the app because of this. That isn't the correct because you might have properties in the service that you don't want share with the app.
In real developement you can build an app that use a external Service and you never going to have the classes that the service developers are using.
In conclusion, the correct way is that you have your classes for the service and new others for app.
Usually android app connects with backend via network connection, so you need not to import the whole backend as a dependency. To share some classes, form that classes as a library and use it as a dependency in both android and backend projects.

Creating Android Modular Applications on Eclipse

I am currently porting a framework for building applications on J2ME to Android. This framework consists of several projects that compile to libraries (jars). Each individual JAR can contain graphical data (resources, J4ME screens, etc.). Every project generally has a well defined entry point (module). When someone wants to build an application using the framework he must only create a Midlet project and add library dependencies and use the imported classes.
We have been using the same approach to develop the Android framework. In this case we have only used normal Java Projects inside Eclipse that compile to jar libraries. These projects have dependencies with the Android Framework (android.jar). When building a new application we create an Android Project inside Eclipse and add the dependencies.
Our next step is to build more advanced modules for Android that can also contain graphical information (Activities, Dialogs, Literals, Drawables, etc.). So far only an Android Eclipse project was needed (the end application), that contained all the graphical-related classes and resources. It seems that when using resources (literals, drawables, etc.) the only approach is to create an Android Application, since the resources are only referenceable by means of an integer handler automatically created by ADT plugin (R.XXX). So building graphical modules may not be built by means of plain Java jar projects.
Android developer information explains that modular applications are feasible, but I have not found a concise tutorial explaining the process, but some tips such as how to prevent an error to ocurr when an application invokes an intent made available by other application. This is valid when building applications that use resources from other applications. I do not need several installed applications on the system, but one built from several components.
Has anyone experience developing with similar requierements? Any good tutorial or tips to start out?
It seems that the only available way is described here as hinted by the accepted answer of this other question.
The solution however is rather new (it only works with latest Android SDK tooks R6 and SDKs 2.0.X are left out of support). It has some major caveats on which I hope Google is already working:
No binary library linking. This means that the main application needs access to sources (in Eclipse implies having all the linked library projects open).
The names of resources (layouts, drawables, etc) are treated globally. This means that if you have two "main.xml" layouts, only the most relevant (uppermost in library list) will be used.
Missing funcionalities/BUGs. The documentation states that exported activities of a library project must only be declared in AndroidManifest.xml of library project. This does not work in current version. Comments inside TicTacToe example hints that this is the desired working, but for current release of Android Tools used activities from library projects must be explicitly defined in AndroidManifest.xml of main application project.

GWT using domain models /services from external module (jar)?

Greetings,
We have been using our reusable "user-management" module which was written using Spring/Hibernate(using Doman-Model Pattern).We have used that module in several project integrating with Wicket,JSP and ZK frameworks.
Now I am going to develop an application using GWT.I am completely new to GWT and I noticed that the domain objects should go inside "xxx.xxxx.client" package.
But I want to use our reusable module (jar) and the domain model is in completely different package.
How can I use my domain-model object from this external library in GWT?
How about service interfaces? Do I have to write proxies for them too?
thanks in advance
For your domain-model object to be used on the client side (ie: to be translated in javascript), you need to provide the sources to GWT during compile time. You also need to tell GWT in a module which package name you need to compile. See how to organize projects in GWT.
About the GWT service interfaces, I'd suggest you to integrate GWT with Spring. There is a nice tutorial about how integrating GWT with Spring. The RPC calls of GWT could be then be routed to your existing services.
Hope it helps.

Categories