Skip to main content

Modularizing your Android app, breaking the monolith (Part 2)

This is part 2 of a series of articles about modularizing Android app. If you haven't yet read the first article, you may find it here.

On our first article we just moved some classes outside the application and applied as an independent module. But what if we have dependencies pulled from the application level? This could be a small challenge. First of all, we want to clarify on how are we going to modularize the app. And depending on the previous article, I chose the by feature version of modularization. First of all, let's show some dependencies that are going to be needed in the whole app.

Note: I'm using Dagger for handling dependencies but manual DI or any dependency tool should be fine to understand this part.

So, this is my dependency schema:

Well, it's not that bad, but this isn't what we want to transform to when trying to modularize the app. If you think about it, modules that don't need a dependency, can get it quite easily. For example: A FeatureXViewModel is just making a API request and doesn't care at all about my local database.

First of all, I'm going to need some base things which I will put them in to a :core_module. These would be dependencies and components that are required all over the app. That would be my CoreApplication class, where the CoreComponent is going to stay, my MainActivity and the launching fragment of my app (personal choice, you can add this as another module too, or you can work on instant apps etc which won't be covered here). After doing so, the :core module is going to be installed in the :app_module and in every single feature which depends on the above elements:

Notes:  The Application class should stand in the CoreModule.
 Feature 6 doesn't have any dependency thus we are not installing anything over there, we just place it in the :app_module in order to run.
When modularizing don't forget all the res files and the required dependencies in each new module you create
All my so called features are {Fragments + ViewModel + Repository (if necessary)}.
Small clarification: the capitalized letter Module refers to Dagger modules while the non capitalized module refers to application modules.

And now let's literally modularize:

Now, the only thing that would immediately break is the ViewModelModule, and that's because :core_module has no idea of my other features ViewModels. So now, we need to set up dagger, for each feature. To set up dagger for multi module project, I held myself loyal to the Google's official documentation about Setting up Dagger in a Multi Module project, which was a good starting point.
TL;DR of the documentation: You need more than one scope.

So now, let's provide a ViewModel for every feature:
To know how I end up in details to this solution about my ViewModel (and why I am providing a Factory like that), please refer to this article here. The only unclear thing is how we brought that applicationComponent. Well, it's just brought from an abstract class from the :core_module which extends Fragment and holds this variable which has a reference to the CoreComponent. Therefore, every fragment will extend a BaseFragment abstract class.

We are not done yet.

Now, doesn't that seem to much work for every feature just for the ViewModel? I require from you to be patient as we are covering this in the next part. The problem is simple: We still haven't solve the feature independence problem, we just set up dagger and made a built without errors. If you notice, :core_module is installed in all features (except one), which means that a feature which doesn't need a dependency still knows everything about it. Therefore, the next step will be to fix that problem.

Stavro Xhardha

Popular posts from this blog

What I learned from Kotlin Flow API

I used to check the docs and just read a lot about flows but didn't implement anything until yesterday. However, the API tasted really cool (even though some operations are still in Experimental state).Prerequisites: If you don't know RxJava it's fine. But a RxJava recognizer would read this faster.Cold vs Hot streamsWell, I really struggled with this concept because it is a little bit tricky. The main difference between cold and hot happened to be pretty simple: Hot streams produce when you don't care while in cold streams, if you don't collect() (or RxJava-s equivalent subscribe()) the stream won't be activated at all. So, Flows are what we call cold streams. Removing the subscriber will not produce data at all, making the Flows one of the most sophisticated asynchronous stream API ever (in the JVM world). I tried to make a illustration of hot and cold streams: Since I mentioned the word asynchronous this implies that they do support coroutines also. Flows vs…

Modularizing your Android app, breaking the monolith (Part 1)

Inspired by a Martin Fowlers post about Micro Frontends, I decided to break my monolithic app into a modular app. I tried to read a little more about breaking monolithic apps in Android, and as far as I got, I felt confident to share my experience with you. This will be some series of blog posts where we actually try to break a simple app into a modularized Android app.

Note: You should know that I am no expert in this, so if there are false statements or mistakes please feel free to criticize, for the sake of a better development. 

What do you benefit from this approach:
Well, people are moving pretty fast nowadays and delivery is required faster and faster. So, in order to achieve this, modularising Android apps is really necessary.You can share features across different apps. Independent teams and less problems per each.Conditional features update.Quicker debugging and fixing.A feature delay doesn't delay the whole app. As per writing tests, there is not too much difference about…

From Gson to Moshi, what I learned

There is no doubt that people are getting away from GSON and I agree with those reasons too. The only advantage GSON has over other parsing libraries is that it takes a really short amount of time to set up. Furthermore, the most important thing is that Moshi is embracing Kotlin support.

First let's implement the dependency:
implementation("com.squareup.moshi:moshi:1.8.0") It's not a struggle to migrate to Moshi. It's really Gson look-a-like. The only thing to do is annotate the object with @field:Json instead of @SerializedName (which is Gsons way for JS representation):

data class User( //GSON way @SerializedName("name") val name: String, @SerializedName("user_name") val userName: String, @SerializedName("last_name") val lastName: String, @SerializedName("email") val email: String ) data class User( //Moshi way @field:Json(name = "name") val name: String, @field:Json(name = "user_name…