Written by Yun Cheng
Do you remember when you made your first “Hello World” app on Android? From there, you likely progressed to creating complex user interfaces to display data, made web calls to APIs and managed the persistence of data. As the Android apps you built became more complex, you might have wondered if there were coding best practices available to make your apps more extensible, maintainable and testable. Perhaps you even wondered how to architect your apps so they’re best suited to your particular needs.
Given that Google (until very recently) did not provide an opinion on app architecture, Android developers were left to come up with their own. Architecture patterns like MVC, MVP, MVVM, MVI and Viper are debated passionately among Android developers. So, what are these patterns and which one is the best?
The short answer to the latter question is that it depends on your particular app and its needs. With that in mind, this book aims to guide you to an informed decision by answering the former question in detail.
What is this book?
Throughout this book, you’ll work with one sample project named WeWatch. You’ll build this project multiple times using each of these architecture patterns. During this process, you’ll get a hands-on comparison of the patterns and gain a deeper understanding of the theory behind them.
Who is this book written for?
This book is for you if:
- You’re a developer who already has a basic understanding of creating Android apps in Kotlin.
- You want to take your apps to the next level with robust architecture.
- You’re familiar with unit testing with JUnit and want to write unit tests for your app.
How to use this book
It’s not necessary to read the chapters in this book in order. Feel free to jump to the architecture pattern that interests you the most. If there are concepts that are covered in another chapter, you’ll be directed to those chapters for more information.
For instance, the sample project uses the following Android Architecture Components at various points in the book: Room, LiveData, ViewModel and data binding, so you may want to read the Android Architecture Components chapter for more information. The project also makes use of RxJava in some chapters, so be sure to check out the RxJava chapter if you need a primer on that library.
Why is app architecture important?
The idea behind the app architecture patterns presented in this book is that they all exist to help you design your app in such a way that allows the app to be maintainable as it scales. Two concepts, in particular, are useful: separation of concerns and unit testing.
Firstly, separation of concerns deals with separating the components of your app by responsibility. For example, when you update the UI of your app with a fancy new design, you want to do so without having to change any of the other code, such as the underlying data.
As you add more features to your app, you want to do so without having to change too much of your existing code.
Finally, as your app grows, you want to be able to test the app to ensure you didn’t break the logic of existing features.
Now that you know the motivation behind app architecture, it’s time to get yourself acquainted with the sample project in the book.
Introducing the sample project
WeWatch, the app you’ll build in this book, keeps a list of movies you want to watch, allowing you to easily add and delete movies within the app. To add a movie, a user can manually enter the movie or search for movies from a database of movies provided by The Movie Database (www.themoviedb.org) API.
The Movie Database API key
Any app that wants access to The Movie Database API must provide an API key in the network call to identify itself to the API. You’ll need to obtain your own API key to work with the sample code within this book.
To obtain your API key, sign up for an account at www.themoviedb.org. Then, navigate to your account settings on the website, view your settings for the API and register for a developer API key.
After receiving the API key, open the starter project for this chapter and navigate to RetrofitClient.kt. There, you can replace the existing value for
API_KEY with your new key.
WeWatch sample app walkthrough
From the project resources open the starter project for this chapter in Android Studio. Take a moment to familiarize yourself with the structure of the project. Build and run the app to see what you’re working with.
The main screen displays the list of movies to watch. You’ll find the code for this screen in MainActivity.kt.
On this screen, you can select movies you want to watch; you can also delete movies you have already watched (or ones that received a terrible rating from rottentomatoes.com) from the list.
Add movie screen
Clicking the floating action button from the main screen brings the user to the add movie screen where they can enter the title and release year of the movie. When they press the Add Movie button, the movie gets added to the to-watch list. You’ll find the code for this screen in AddMovieActivity.kt.
Alternatively, the user can enter the title of the movie and press the Search button to the right, which brings the user to the search movie screen.
Search movie screen
For this screen, corresponding to SearchActivity.kt, a network call is made to the search endpoint of The Movie Database API, using the movie title passed in as the query. The resulting screen is a list of results matching the movie title. The user can then select a result to return to the add movie screen with the movie information pre-populated.
Where to go from here?
Now that you know the motivation behind this book and had an introduction to the sample project, you’re ready to learn the theory behind the Model View Controller architecture. You’ll learn how the MVC pattern is ironically not really a pattern in Android at all. This is the default architecture that the sample project uses, at least for now!