Separate UI from Logic in Android - java

I recently read a blog post by the Gmail team on the approach they used to develop the different platform versions for Inbox for Gmail. The short story is that they write everything in Java (so basically for Android) but separate all the UI code from the shared logic code. Then for iOS they reimplement the UI in a platform specific iOS manner but use a program they developed (and open sourced) called J2ObjC to convert the shared logic from Java to Objective C.
I understand how to get this working on the iOS but how should I arrange the classes on the Android? I understand that I need to create separate classes for the different parts of the shared logic but how do I this practically so that a) all the shared logic is in its own directory so I can easily reference it on it's own and b) I can still reference it from my activities.
Based on my searches I think I need to use the MVP pattern but I'm not 100% sure.

I put the model logic in a separate project with no Android dependencies. Another project depends on the shared project and contains the user interface and builds the Android project.

Related

Is there a way to mix Gluon Mobile (javafxports) with native android code?

For example: i want to migrate the core of my javafx desktop app (using Gluon Mobile) to Android, but only core. Other parts (excluding core) of application i want to implement using native android code.
So, is there a way to keep Gluon Mobile and Android native codebase in single project ?
As a rule of thumb, you shouldn't require specific platform code in your Java project. If you just want to provide a custom Java code or custom styling based on the platform, you can use Platform.getCurrent().name() and then react based on it, for instance using different code or loading a different css file based on platform and type of device.
Nonetheless, there are several reasons to include platform code, and since you don't specify which is your requirement, I'll assume one of the following:
make use of a native service, i.e. the device's camera
make use of a native API, i.e. Android's Log
make use of a native widget/layer, i.e. Android's Toast or SurfaceView
For the first case, accessing a native service, have a look at the Charm Down library, and the native implementation for the different services.
As you'll notice, each service contains both Java and specific platform code. The main plugins (i.e. charm-down-plugin-storage-3.6.0.jar) contain neutral API that can be used later on from your Java project, and it will call the implementation for the platform you are running on (i.e. charm-down-plugin-storage-desktop-3.6.0.jar for Desktop or (i.e. charm-down-plugin-storage-android-3.6.0.jar for Android).
The service will be called from the Java main package:
Services.get(StorageService.class).ifPresent(storage -> {
storage.getPrivateStorage()
.ifPresent(file -> System.out.println("Private storage " + file.getName()));
});
In this way, you can run it in any platform.
If you require a service that is not available yet, you can create a new service. A sample on how to do it can be found here. For instance, the AndroidLogService implementation makes use of android.util.Log.
Even if you don't need a service, but just accessing the Android API, you can follow the design pattern of Charm Down as in the sample above, and provide your Android implementation.
As for Android widgets, you won't be able to use them unless you create a native layer, taking care of laying both the widget and the layer on top of the JavaFX layer. For instance, the Video service for Android does exactly this.
Finally, there is another alternative, in case your project is basically an Android project and you want to call JavaFX from it. A sample of this is the Kokos sample.

Sharing file across library project

I am writing a library project which can be installed across multiple apps.
In order for the library to function it needs to download files. My goal is to download the files once and share them across other instances of my library in other apps. (I want to save the network traffic), how do I achieve this. Public external storage is not an option for me.
If you want to share functionality between different android applications, you should implement the Inter-Process Communication (IPC) between them.
Since android applications are actually sandboxed linux processes, they do not have access to each other's private data and files.
In the domain of Android development, there are two ways to implement IPC :
Messengers and AIDL ( Android Interface Definiton Language), which both make use of the bound services in application.
I recommend you to start studying the aformentioned documents to get a big picture on the subject.

Using parts of android lib in non-android project

I am currently attempting to port an android app to desktop. I realize that the majority of the android based code cannot be reused, but what about "smaller parts" which dont depend on the android architecture?
I am mostly talking about support or math related classes like SparseArray, Matrix and so on.
Those may work. But you will have a rough time figuring out what works and what doesn't. Even once you do that you will need to create a whitelist somewhere like in your IDE that doesn't auto-complete classes you don't want or can't use and you will also want to fail your build. Maybe you should create a jar containing only the classes you want to use that you determine are usable.

Difference between Kivy and Java for android apps

I'm a python developer with little experience creating android apps in java and want to create an app that will access my university web portal, retrieve some data and show on a view.
So, after researching Kivy, I have a few questions:
1) Which one is easier and faster to develop android apps?
2) Does Kivy have any android feature limitations?
3) And finally, would an android app developed using kivy run as fast as one developed using java?
This is a rather subjective question.
1) Which one its easier and faster to develop android apps?
I think there's a strong argument for kivy, but this doesn't have an objective answer.
2) Does Kivy has limitations to access certain parts of android (like not fully integrated with its api)?
The kivy project includes pyjnius, a tool for accessing java classes through python, and in principle I think this should give arbitrary (edit: on reflection, not arbitrary, but probably not limited in immediately important ways) access to the java apis.
In practice, prebuilt python wrappers are a work in progress, though rapidly improving. The android python library already gives easy access to many things (including but not limited to intents, vibration, accelerometer etc.). Even where there isn't already a python wrapper, it can be very easy to do the necessary work.
Edit: There has recently been great work on Kivy's plyer project, intended to provide a transparent api to platform specific tools so that you can call it once and get the same behaviour on different systems without knowing about the details. It includes useful support for parts of the android api.
3) And finally, an android app developed using kivy would run as fast as one developed using java?
Ultimately the answer is probably no, but the difference is highly unlikely to be important unless you're doing something strongly cpu limited. The task you suggest would not be limited in that way.
To complete inclement's answer, pyjnius indeed allows to access a lot of the android api. But it's not perfect, calling existing classes is not always enough, and an android programmer often need to create code that will be called by android to manage events, there are two ways to do that, both used by the android api.
The first one is interfaces: you need to create a class that implement an existing java interface, pyjnius can do that, you create a python class and declare which java interface it implements, and have a decorator to declare the methods you have to declare.
The second is subclassing, you need to subclass an existing java class and override some methods, and we don't have a way to do that with pyjnius yet, so for these ones, you'd have to create a java class and use it in your program (fortunately you can mix that with kivy/pyjnius, it's just can't be 100% python in that scenario).
So it can be worth a look to the api beforehand, to see if the parts of the android api you have to access requires that.

Create Android apps in Eclipse sharing common library

is it possible to create java libraries the apps can use shared?
It is totally clear to me how to create a library project and how to use it while shared between several another projects, compile each project into different apps.
But in this case the library gets compiled into each app separated.
What I want is to create a library, compile it, install it into the device (or emulator) and several apps calling into this library.
And when it is needed to change something the apps use shard in this library, I just re-work the library, re-compile it, replace it on device (or emulator), and the apps use the new library, all apps use the new functions.
Is it possible?
I googled it, but I couldn't find the solution.
I am using Win + Eclipse, I can't use native code (since I know only Java).
Thanks
UPDATE: Thank you for your suggestions, I know about using Services and Activities started explicitly in order to share functionality.
I asked about libraries because I am investigating the possibilites. If there is no way to use common library, what is the purpose of uses-library... in the app manifest?
Thank you
It's more likely that you need a service as a library. Have you considered this possibility? You can create it as a separate application, define an interface for your service and use it in other apps. So as the interface does not change this will not influence on other apps that depends on this service.
The Android model installs each application as a separate user (UID) on the device and the users have no access to other application's/user's files. Therefore, you can't share libraries as such.
As Yury suggested, you might need a service or an activity that can be invoked from multiple applications.

Categories