Clean Architecture Tutorial for Android: Getting Started |

In this tutorial, you’ll learn how to use Clean Architecture on Android to build robust, flexible and maintainable applications.

This is a companion discussion topic for the original entry at
1 Like

Great tutorial!! Thanks :slight_smile:

GlobalScope not work, can you update the tutorial please? Thank you for share your knowledges.

Correction* I dont know why but GlobalScope bugs import. Download again and sync works!

@cryptoseven Glad you like it! Cheers! :]

@nicolaugalves Glad you sorted it out! Cheers! :]

I think the tutorial its really nice
I just have a concern:
why are you using Serializable instead of Parcelabe for the models is there any reason in specific?

thanks, I’m glad you like the tutorial! :]

I’m using Serializable only because it’s easier to implement - wanted to keep the code in sample project as simple as possible.

Awesome article! One thing I would have liked to have seen though is at least a little bit more of a reference to how the data provider is abstracted. Specifically, maybe a diagram making it clear that you might have both network and local DB acting as data providers, and that it’s the responsibility of the Repository to figure out which to use and how to sync between them.

kind of like this. I realize the implementation for this is probably out of scope for a tutorial article like this, though but some reference on how to approach this common pattern or where to go for examples would be great.

1 Like

I realize this probably happened during course of writing the article - you started out with a project structure with a certain name but in packaging the tutorial for starter / final projects things may have changed. In the point of the tutorial where you first have the reader do stuff in Android Studio, you say:

Right click on MajesticReader in Project explorer and select New ▸ Module or select File ▸ New ▸ New Module

This is incorrect in relation to the project the reader actually downloads, and the screenshots are also incorrect.
The only folder named majestic reader is too far down on the hierarchy. Comparing to the final project, the place they should be creating the new module is at the project root.

Actually, I can’t get the starter or final projects to run. Opening them in Android Studio, it says “module not specified” and there is no module to choose from. Something seems to be missing from the project files to get the projects to work out of the box?

Also, the final project code does not match the code as per the tutorial. For example, there is a step which says:

Open MajesticReaderApplication and replace onCreate() with the following, making sure you add all the necessary imports:

The code from this step is nowhere to be found in the final project. It seems perhaps the wrong code was submitted for the final project?

Hi @abunur thanks for the question!

I uploaded a new version of the tutorial materials that should have addressed this problem. Let us know if you’re still unable to build the project in Android Studio 3.5 or later.

Also, the MajesticReaderApplication class is in the framework package of the app module.

Thanks again!

1 Like

@macsimus @ikust
Great article! But I am curious to know why did you use GlobalScope instead of viewModelScope?

Hi @sagarsuri,
thanks for the question! I’m glad that you like the article :slight_smile:

viewModelScope would be the appropriate scope to use but it was added in version 2.1.0 of androidx.lifecycle:lifecycle-viewmodel-ktx library which wasn’t available at the time of writing. To keep things as simple as possible we used GlobalScope.

1 Like

Where is the right place for di folder to reside?

@sagarsuri If you are referring to a folder that would hold Dependency Injection related code, the right place for it is framework package in the app module.

1 Like

Nice tutorial!

I have some questions related to the graph that shows communication between the layers:

  1. Why did you add arrows in both directions between layers? For example between 'Presentation layer ’ and ‘Use cases layer’ or ‘Data Layer’ and ‘Domain layer’?
  2. Why there is no arrow between ‘Use cases layer’ and ‘Domain Layer’ because, for example, ‘AddBookmark’ class (from ‘interactors’ package) has direct access to ‘Bookmark’ and ‘Document’ classes from domain layer.


thank you, I’m glad that you like the tutorial!

  1. I’ve added arrows in both directions to show that a lower layer requests information from a higher layer and gets a result. For example ViewModel from Presentation layer calls function from Interactor and gets a result.
  2. That is correct. I wanted to note that ‘Bookmark’ and ‘Document’ classes from Domain layer are accessed through Data layer - for example AddBookmark class uses BookmarkRepository to add a new Bookmark.

Great tutorial, but I don’t viewModel should hold the application context,.
If i am not wrong the viewModel should not know anything about Android framework.

thank you, glad that you like the tutorial!

Looking at the official documentation, using Application context in ViewModel is permitted. It is important to not reference anything that may hold a reference to the activity context since ViewModel can outlive Activity.

A quote from the documentation:

A ViewModel must never reference a view, Lifecycle , or any class that may hold a reference to the activity context.

Android Jetpack even provides AndroidViewModel which can be extended and has a reference to Application.

1 Like

i’m just wondering, is it allow to pass 2 or more different repositories into one use case?