Group Group Group Group Group Group Group Group Group

Clean Architecture Tutorial for Android: Getting Started | raywenderlich.com

Hi,
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.

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

@ikust
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.

Thanks

Hi,
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.

Hi,
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.

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

Hi!

Yes, it is ok to pass more different repositories to a single use case.

Thanks for the article. It might take a couple of more reads and then a practice myself. My one bit of feedback would be is that I’d have preferred it to have been done in MVP. I feel the article strayed off into MVVM territory too much in the latter half, which I know a little about, but this was quite complex MVVM, so the article stopped being about Clean and about MVVM instead.

It is a good tutorial for clean arch but i would highly recommend having the extra work to properly implement it with koin otherwise this is only partly complete.

Can i see your JSON Response to compare it with mine?

@ikust Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi,

sorry for such a late response.

Which JSON Response are you referring to? There are no network request made from the example project so I’m not sure.