Android client and java server in the same git project - java

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.

Related

Stripe Integration using Kotlin in a Gradle project

Has anyone done Stripe Integration using Kotlin? It would be great to see some documentation about the project structure and HTTP requests!
Main problem: I am unable to find the gradle dependency for Kotlin. Does something like that even exist?
*Note: I am not using Kotlin for Android so please don't try to answer with this link.
Kotlin inter-op with Java is almost perfect. For most companies it doesn't make sense to build an adapter layer which somehow make it easier to work with their product using Kotlin. There is really no need for it.
There is no specific library for Kotlin, just keep using Stripe-Java, you'll be fine.
If compatibility issues arise, you'll most likely be able to handle them by yourself, maybe by writing a (very) small portion of your codebase in Java, but that's a remote possibility.
And remember the GitHub issue tracker is there for a reason.

Gradle: How to create a library from only part of a project?

I have a spring boot project with server functionality. In the same project I wrote a client that provides an abstract way of communicating with the server. The client makes it much easier to make the necessary HTTP requests without needing to be concerned with URLS, response codes, etc.
I would like for the client code to live in the server project, and export this as a library that I can add as a gradle dependency in other projects.
What is the best way to do this? Can I configure gradle to export a certain package as a separate library, e.g. com.example.client.* or do I need a second build.gradle and re-structure the project?
See the documentation for
Multi Project Builds
Publishing Artifacts

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

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.

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.

Using one Java source file in two different projects, both under development

I am developing a Spring (Java framework for server-side web-development)web application, which will respond to another client-side Java application(which uses socket communication) by a JSON object. At the same time, I'm working on both server-side and client-side Java applications.
The problem is that I have a bunch of files(say, a Json variable interfaces) that are being used at both projects. For now, I have duplicate copies of that interface, in different packages in the two projects. But this causes inconsistency, because I have to update the both files whenever I need to make a change in the interface.
Does anyone have a neat solution for this?
Thanks
You should treat your shared code at the package level and not the file level.
You should create a package of interface definitions that are used by both the client and server side of your architecture and whenever that package changes, both sides will have to change accordingly.
EDIT:
I wasn't explicit about it but zellus' suggestion about importing the common code as a jar is a good one.
You might create a separate project for your common JSON code. Using subversion, svn:externals allow a neat integration on the source level. Importing the common code as jar file is another approach.
If you're using maven, you could create a local maven project containing all the classes you might need in different projects and add this dependency to your pom.xml which requires these classes.

Categories