How to share Java classes with Android project - java

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.

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.

Building Java Web-App with Vaadin in AWS

I have to write a (java) web-app, which fetches data from an AWS RDS Postgresql Instance, and renders the data using Vaadin Charts. So my two constraints are: (java) based, and using Vaadin to do so.
Thing is, I have never developped an form of web-app, and am complettely lost. I've read stuff about maven, spring, gradle , containers and am safe to say, have absolutely no clue where to start...
Could anyone point me to some complete tutorials about how to developp web aps from the ground up? everytime I google something I read something different and am completely overflown by information...
If you want to start with something working ASAP you can clone existing repos with vaadin examples. You will have existing code that builds, manages dependencies, starts webserver etc:
https://github.com/vaadin/dashboard-demo
https://github.com/vaadin/book-examples
https://github.com/vaadin/spreadsheet-demo
All the rest is probably opinion-based like should I you use maven or not? etc.

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.

turn api with hibernate objects into service for multiple applications

i have an API that is being written for a large group of 40 or so applications to share.
my problem is currently they plan on having the API as a simple library included in each war file for each program. the problem thats going to occur is when two apps are running on the same instance with different versions of the api library. ive had a lot of problems in the past with this.
i seem to remember a while ago something where i can wrap my library into an ear file or something and deploy it to tomcat to make it global. simply including it in the lib folder won't work because it will include hibernate systems that have to be deployed to allow the api methods to access the database. then in each application i would have an interface i can implement that allows me to call those api methods. very similar to local EJB3 but not as complex and didn't require an enterprise level server to implement.
anyone else remember something like this or was it a bad dream on my part?
You will have problems if you use a single jar shared by all the webapps, since it will then be impossible for two apps to use a different version of a library. But if each webapp has its own version of the library in its WEB-INF/lib, the container shouldn't have any problem: each webapp has its own classloader, which doesn't see the libraries of other webapps.

Can I use android.os.* libraries in a standalone project?

I'm trying to develop an external library (not sure if that's the right term) to provide prepackaged functionality in Android projects. Working in Eclipse, I've added the appropriate android.jar file to the build path, and everything is happy both while editing and upon compilation.
However, when I use Android's Handler and Message classes (android.os.Handler, android.os.Message) for inter-thread communication, I get exceptions unless I'm running within an Android app, on the emulator or a device. I can no longer test my library in a "standalone" way without having to go through a running Android target.
Is there any way for me to include these two Android classes and still be able to test my library standalone? Do I need to have the Android source available? Or would it require some sort of conditional compilation hand-waving?
Is there any way for me to include
these two Android classes and still be
able to test my library standalone?
Not readily, by any means I can think of.
Do I need to have the Android source
available?
I don't know where else you would get the implementation from. But, more importantly, those things are not designed to work in isolation outside of the OS, any more than you could just grab a Cocoa class or two and pull them into your Objective-C library and expect them to run on a Windows box.
Off the cuff, knowing nothing about what you're building, I would make whatever dependency you are introducing on Handler and Message be more pluggable. Test outside of Android using a pure-Java implementation, perhaps even just some mocks. Test inside of Android using the real implementation.
You could try the lib Robolectric, that implements the android API so you would be able to create JUnit tests for some isolated code you have:
http://robolectric.org/

Categories