Android – Clean Architecture – Kotlin
The purpose of this repo is to follow up Clean Architecture principles by bringing them to Android. The repo contains a Sample News Application which shows current news from https://newsapi.org/ API.
Clean architecture promotes separation of concerns, making the code loosely coupled. This results in a more testable and flexible code. This approach divides the project into 3 modules: presentation, data, and domain.
- Presentation: Layer with the Android Framework, the MVVM pattern and the DI module. Depends on the domain to access the use cases and on di, to inject dependencies.
- Domain: Layer with the business logic. Contains the use cases, in charge of calling the correct repository or data member.
- Data: Layer with the responsibility of selecting the proper data source for the domain layer. It contains the implementations of the repositories declared in the domain layer. It may, for example, check if the data in a database is up to date, and retrieve it from service if it’s not.
The app’s functionality includes:
- Fetch Current News data from https://newsapi.org/ & show them in
RecylerViewwith smooth pagination.
- When an item is selected from
RecyclerViewit will load the news article in a
- From Details view , a news article can be added to Favorite news – which will store the News article in the Room database.
- From Today’s news section users can search for specific news topic & return the search results with pagination.
- From favorite news section users can view all their saved news articles, they can also swipe left/right to delete the article from local database.
The app uses clean architecture with
MVVM(Model View View Model) design pattern.
MVVM provides better separation of concern, easier testing, Live data & lifecycle awareness, etc.
The UI consists of two parts
View– Activity screen, Host the navigation component fragments.
Fragment– Contains two fragments:
FeedFragement– Show & search for today’s news. Listen for
FavoriteFragment– Show saved news articles.
DetailsFragment– Show webview with floating action button for loading & saving news article.
Model is generated from
JSON data into a Kotlin data class.
In addition entity class has been added for room database along with
Type converter for saving/retrieving custom object data.
Used for fetching today’s news, searching news & update livedata. Also send out the status of the network call like Loading, Success, Error using
ViewModel also responsible for pagination of data using page count.
The app uses
Dagger-hilt as a dependency injection library.
ApplicationModule.kt class provides
Singleton reference for
The network layer is composed of Repository, ApiService.
NewsApi – Is an interface containing the suspend functions for retrofit API call.
NewsRepository – Holds the definition of the remote/local repository call.
In-order to successfully run & test the application you will need an
Go to – https://newsapi.org/ and click
Get Api Key
Now Go to –
const val API_KEY = "YOUR_API_KEY"
You can open the project in Android studio and press run.
Android Studio version used to build the project: Arctic fox 2020.3.1
Gradle plugin used in the project will require
Java 11.0 to run.
you can set the gradle jdk in
Preferences->Build Tools->Gradle->Gradle JDK
The project uses
Kotin-Dsl build scripts to maintain dependencies & plugins.
- Android appcompat, core, constraint layout, Material Support.
- Android View Binding
Hiltfor dependency injection
Retrofitfor REST API communication
Coroutinefor Network call
Roomfor local database.
Navigation Componentfor supporting navigation through the app.
GLidefor image loading.
Swipe Refresh Layoutfor pull-to-refresh
EndlessRecyclerOnScrollListenerfor Recylerview Infinite Scroll.
Junitfor Unit testing.
Robolectricfor Instrumentation testing
Truthfor Assertion in testing.
Espressofor UI testing.
Unit testing has been added for
Test the viewmodel of the app using
The test cases comprise of testing different states like Loading, Success, Error with fake data for testing News Response & Search Response.
Test the Repository of the app using
The test comprises of testing the functionality of Favorite News Room Database like Insertion, Remove, Get saved news etc.
Mock Webserver is used to test the Network api response in case of successful data, empty, failed case.
Copyright (c) 2020 Rafsan Ahmad Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.