Chapters

Hide chapters

Advanced Android App Architecture

First Edition · Android 9 · Kotlin 1.3 · Android Studio 3.2

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

15. VIPER Sample
Written by Aldo Olivares

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last chapter, you learned the theory behind VIPER. You learned how all of its layers work and why this architecture is an excellent option for building maintainable and scalable Android apps.

In this chapter, you’ll apply that knowledge to rebuild the WeWatch app using VIPER. Specifically, you’ll learn how to:

  • Create a Presenter that acts as a bridge between all of the components in your app.
  • Create an Interactor that communicates with your app’s backend.
  • Create a Router that handles the navigation between your Views.
  • Use Cicerone to implement your Routers.
  • Use Interfaces as contracts to implement the layers of VIPER.

Getting started

Using Android Studio, open the starter project for this chapter project by going to File ▸ New ▸ Import Project and selecting build.gradle in the root of the project.

The starter project contains the basic structure you’ll use for this app, and it contains the following packages:

  • Data: All of the backend code that your app needs to work correctly, including the Entities, a Repository and the Room database components.
  • Interactor: The interactors of your app. Package should currently be empty.
  • View: The activities, fragments and adapters.
  • Presenter: The Presenters of your app. Package should currently be empty.

Note: If you don’t see the interactor or presenter packages, then go ahead and just add them to the project by right clicking on the com.raywenderlich.wewatch package and selecting New ▸ Package.

For didactic purposes, this code was organized with a structure according to VIPER layer names, but in a real-world app you often want to structure your project in packages according to the different modules in your app such as MainModule, AddModule or SearchModule.

Take some time to familiarize yourself with the rest of the starter project and the features included out-of-the-box, like the adapters and layouts.

Once the starter project finishes loading and building, run the app on a device or emulator.

Currently, it’s an empty canvas, but that’s about to change!

Note: Before moving on to the next section, remember to get an API Key from TMDB API (https://developers.themoviedb.org/3/getting-started/introduction) and to substitute the value inside RetrofitClient.kt.

Defining your app’s contract

For this project, every module is represented as an Interface contract that needs to be implemented by several associated classes. The contract represents which VIPER layers you must implement in every module and the actions the layers need to perform.

Implementing the Main Module

Now that you have the contract defined for the main module, it’s time to apply the contract to each individual component. You’ll start with the View.

The View

Open MainActivity.kt inside view/activities and make it so MainActivity implements MainContract.View:

The Router

As you learned in the previous chapter, the Router layer is in charge of the navigation across the multiple Views in your app. In a traditional VIPER implementation you would have a Presenter that receives actions from the View that are then translated as commands for the Interactor or the Router.

The Presenter

Create a new package named presenter, and then add a new file named MainPresenter.kt. Replace everything inside with the following:

The Interactor

Next, you need to implement the Interactor for the Main Module.

Implementing the AddMovie module

With the main module out the way, it’s time to apply the same architecture to the add movie module. You’ll again start with the View. Be sure to review the contract for the add movie module in the AddContract.kt file found at the root package.

The View

Open AddMovieActivity.kt inside view/activities, and make AddMovieActivity implement AddContract.View:

The Presenter

Create a new file inside the presenter package, name it AddPresenter.kt and replace everything inside with the following:

The Interactor

Create a new file inside interactor, name it AddInteractor and replace everything inside with the following:

Implementing SearchMovie

There’s only one module remaining: SearchMovie.

The View

Open SearchMovieActivity.kt inside view/activities and make SearchMovieActivity implement SearchContract.View:

The Presenter

Create a new file inside presenter, name it SearchPresenter.kt and replace everything inside with the following:

The Interactor

Create a new file inside interactor, name it SearchInteractor.kt and replace everything inside with the following:

Key points

  • View is the component that receives UI actions from the user and sends them to the Presenter.
  • Interactor is the component in charge of interacting with your backend such as your databases and web services.
  • Presenter is like the commander of your architecture. It’s in charge of coordinating your Views, Interactors and Routers.
  • Entity is the component that represents your app’s data. It’s usually represented as data classes in Kotlin.
  • Router is the component that manages the navigation between the Views in your app.

Where to go from here?

VIPER is a great architecture pattern that focuses on providing the maximum level of modularity for your Android projects. It allows you to create app’s that are maintainable, scalable and easy to test.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now