Android modularization, part 1
The first part of this Android modularization series walks through project setup and the creation of the domain module.

Editorial translation by Impulso.
MVVM, Koin, Rx, Room, Databinding, and a little more…
You’ve probably heard of modularization at some point in Android development, right? If you haven’t, no problem. This series is also for you if you want to learn from scratch.
My main motivation for writing this series of articles is to help more developers get up to speed with this “new” approach, which has become widely used in Android development.
We’ll start with three articles, in this order:
- Domain
- Data
- Presentation
What do you need to follow along? A willingness to learn!
Domain Module
We’ll start with the Domain module, which handles communication with the Presentation module. It’s also a pure Kotlin/Java module, meaning it has no Android dependencies.
What’s in the domain module:
- Entities: These contain only the data we want to send to the ViewModel/Presenter, meaning the mapped data from the backend.
- UseCases: This is where we put our business rules, based on the data requested from the repository. Think of it as “shielded”: changes here shouldn’t affect other modules in the project, and changes in other modules shouldn’t affect the UseCase.
- Repository: This is the interface for requesting data from the backend or the cache.
Flow diagram for the Domain module.

The presentation layer requests data from the UseCase, which asks the repository to retrieve it through its implementation. The data comes back to the UseCase, which can apply a business rule before returning it to the presentation layer.
Now let’s build this module step by step:
- Start a new project
- Select Empty Activity
- Give the project any name you like (I used MyModuleExample)
- Select Android X
- And set minSdk to 20
Now that we’ve created our project, let’s create our domain module:




The first thing we’ll do may not be specific to the domain module, but it needs to be done right at the start of the project.
Let’s create a dependency file where we’ll centralize our project’s Gradle dependencies, including libraries, their versions, and a few other things. As the project evolves, we’ll update the file as needed. For now, we’ll create the file, configure the project’s build.gradle to use it, and set up the dependencies for our domain module. Let’s get to work:
- Create a file at the root of the project and name it dependencies.gradle.
- In this file, list the versions of the libraries and add an array of dependencies using the corresponding versions you defined earlier.
ext {
kotlinVersion = '1.3.21'
buildTools = '3.4.0-beta04'
//Rx
rxJavaVersion = '2.2.7'
rxKotlinVersion = '2.2.0'
rxAndroidVersion = '2.1.1'
//Koin
koinVersion = '1.0.2'
Now we can use it in our project. First, let’s configure the project’s build.gradle file:
- Configure: apply from: ‘dependencies.gradle’ inside buildScript
- Set versions: The interesting thing about this approach is that we need to use double quotes around the dependency so we can set the version with $kotlinVersion.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
apply from: 'dependencies.gradle'
repositories {
google()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "com.android.tools.build:gradle:$buildTools"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
Now we can configure the dependencies for our domain module (FINALLY!)
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
def dependencies = rootProject.ext.dependencies
implementation dependencies.kotlin
implementation dependencies.rxJava
implementation dependencies.koin
}
Let’s open build.gradle in the domain module and create a variable called dependencies that pulls in all the dependencies from the file we created earlier. Then we can use the ones we want to add, as shown in the code above.
But why set all this up for a simple project?
Simple or not, the project will have all its dependencies in one place. For example, if other modules use RxJava, this approach ensures they all use the same version of the library. That way, we avoid having different versions across modules, and when we upgrade to a newer version, all the modules are updated.
So even if the project is simple, we should always think about how to avoid rework down the road and keep our structure as clean as possible.
NOW LET’S DIVE INTO THE DOMAIN MODULE!
Note: Our project will display a list of Android jobs, and we’ll use the following API: http://demo8470178.mockable.io/android-jobs
We’ll have 4 packages in the domain module:
- Entities: This is where we’ll create our data classes. The first one we’ll create is AndroidJob:
data class AndroidJob(
val title: String,
val experienceTimeRequired: Int,
val native: Boolean,
val country: String
)
- Repository: This is where we keep the interfaces for communicating with the data module (next article). Here’s our first repository, which will return an observable list of AndroidJobs:
interface AndroidJobsRepository {
fun getJobs(forceUpdate: Boolean): Observable<List<AndroidJob>>
}
- UseCases: These are the use cases the presentation layer will call. Our first UseCase will fetch the list of AndroidJobs:
class GetJobsUseCases(
private val repository: AndroidJobsRepository,
private val scheduler: Scheduler
) {
The constructor shows that we need two dependencies to use the UseCase: the repository we’ll request the list of data from and a scheduler to specify the thread we’ll subscribe on. Both dependencies will be supplied through dependency injection using the Koin framework.
- Di: This package simply keeps the dependencies for this module in one place:
val useCaseModule = module {
factory {
GetJobsUseCases(
repository = get(),
scheduler = Schedulers.io()
)
}
}
We create a variable called useCaseModule and assign it a module from Koin that contains the dependencies we’ll provide. I used factory because we want a new instance to be created every time this dependency is requested.
We have repository = get(), where get means this dependency has already been created somewhere in the project, and we just retrieve it to use in the UseCase. For scheduler = Schedulers.io(), we pass in the Scheduler we want to use. In this case, it was IO, but it could be computation, etc. (https://stackoverflow.com/questions/33370339/what-is-the-difference-between-schedulers-io-and-schedulers-computation)
And finally, we end up with something like this:

Our project doesn’t do anything yet, but we’re building it step by step. So for now, our reward is simply the satisfaction of having created the domain module.
Ideally, we’d write tests at this stage, but first let’s understand the concepts behind modularization. Once we’re done, we’ll start writing the tests.
You can check out the full code in this GitHub repository.


