Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Parcelable in Java Code

I have a multi-module project in Android Studio and define my business objects (the POJO objects I want to work with in the ui) in a pure java module (called "application"). The final product will be an Android App which connects to a server.

My app structure is the following (I have a multi-layer architecture whereas each layer represents a module):

 Project
 - presentation (android ui)
 - application (logic / mapping DTO <--> Business Objects (POJOs))
 - data (mapping JSON <--> Data Transfer Object (DTO))
 - transport (network communication)

The lower 3 modules are pure java / only the first one is an Android module.

Now my question is: How can I still use Android's Parcelable for the mentioned POJOs (in a pure java module). Is there a gradle dependency for that? I didn't find anything until now so it would appreciate some useful hints.

I wouldn't like to use Serializable and would prefer not to change the module to an Android module.

UPDATE: I tried to just implement Parcelable in the java module (which does work without any problem, but it doesn't recognize android.os.Parcelable when building. So I'm back to the very beginning of my question.

UPDATE II: Answers like try Parceler or anything like that don't work as android.os.Parcelable is not recognized in the module. I'm not looking for answers how to implement Parcelable but how to be able to use Parcelable in pure Java. Thanks.

SOLUTION: See below...

like image 223
luckyhandler Avatar asked Mar 10 '15 14:03

luckyhandler


2 Answers

I highly recommend against using Parcelable. And I've used it a lot. Avoid it if you are not going to use IPC in Android. If you're not going to pass these objects between Android processes, you don't need to implement Parcelable.

If you want to pass objects between Android processes, then Parcelable is a must. You should look into AIDL. Note that the classes still have to implement Parcelable, even if you use AIDL.

You don't need any special dependency, the Android SDK is enough.

EDIT:

I know the linked documentation says that "If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger". In my experience, I disagree with this recommendation. Using bound services and Messenger was a painful path full of hidden traps. AIDL is much easier and straightforward to implement.

like image 156
m0skit0 Avatar answered Oct 12 '22 09:10

m0skit0


O.K., it now is working. I finally realized that it is impossible to run pure java modules inside an Android Application and that you always have to apply the android library plugin instead of the java plugin:

apply plugin: 'com.android.library'

I also had to add a Manifest.xml and add the following to the build.gradle of the java modules:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
    }
}
like image 32
luckyhandler Avatar answered Oct 12 '22 08:10

luckyhandler