Search code examples
androidauto-valueclean-architecture

Android CleanArchitecture Domain module and AutoValue


i want to use clean architecuture in a new app and so far it works great. I structured the app into 3 modules (presentation, data and domain) like in the following example: Android-CleanArchitecture

There are some entities in my domain module. One of them is User.

    public class User {

        private String name;

        public String name () {
            return name;
        }

        public static class Builder {
            ...
        }
    }

I want to use AutoValue with some extensions to get rid of boilerplate code. One of the extensions is AutoValue Parcel. Now i need to implement the android.os.Parceable interface which is a part of android and can not be used in my domain module because it is a Android dependency.

What is the correct way to implement this?


Solution

  • Fulfilling the ideal of Clear Architecture is a bit hard with Android, as Android is a entire framework that has its own specification, even at basic language level.

    I think, as the ideal of specific tools free level is mostly for testing purposes, thus - this should be the main goal in mind when dealing with Android.

    Regarding the Parcelable itself, consider that it's merely adds additional methods for model object in order to make Android OS be able to serialize/deserialize data between different system components. So as your tests in the domain level doesn't run by Android OS, they should not touch this methods and it would not affect your tests.

    Consider, several other APIs in Android that are not available in unit test, like Uri, DateUtils, SparseArray and more...
    That's where I think that Roboelectriccan come handy, with mocking these Android primitives.

    Having said all that... with Clear Architecture, your Domain level should be separated from Presentation level, so basically you would have model objects in both layer (that can share interface) and both of them can use AutoValue, but, only in Presentation level you can implement the Parcelable interface (which are the ones that needs it, as in this level you'll interact with the System and use Parcel). No Android dependencies in the Domain level are not the only consideration for this separation, bear in mind, that Presentation level might add information that is not relevant to Domain level.