Android

Android Studio MVP Template Google Architecture

UPDATED: Starting with Android Studio 4.1, Google ended its support for custom FreeMarker templates. 🙁 Now you can’t just write your FTL...

Written by Vortana Say · 3 min read

UPDATED:

Starting with Android Studio 4.1, Google ended its support for custom FreeMarker templates. 🙁 Now you can’t just write your FTL files and put them in a template folder of Android Studio. So I created an Android Studio plugin, MVP Generator, for this purpose. You can download it from the marketplace in plugins.

https://plugins.jetbrains.com/plugin/16254-mvp-generator

If you want to know how to develop this, here is the GitHub repository:


This is an Android Studio template for MVP; the template is inspired by google samples/ android architecture

When I follow the Android sample MVP, for each module/feature, I need to create an Activity, a Fragment, a Contract and a Presenter. Oh, and also the layout file for the activity and fragment.

Taking advantage of the template of Android studio, I created a template for Android studio so that those files can be created automatically as a starting point.

Important: The main goal of this template is to speed up the development process using android MVP template. This is just a template, so feel free to make changes according to your needs. It also shows how to create a set of files using template files in Android Studio. This template was successfully tested with Android 2.3.3.

Thanks to riggaroo/android-studio-group-templates-mvp and benoitletondor/Android-Studio-MVP-template for the resources allow me to create my own.

Installation

For Mac:

  • If you have a standard Android Studio installation:

Just run the installation script at the root of this repository:

./install.sh
  • Manual installation:

Just copy directory MVPTemplate to $ANDROID_STUDIO_FOLDER$/Contents/plugins/android/lib/templates/activities/

For Windows:

Just copy directory MVPTemplate to $ANDROID_STUDIO_FOLDER$\plugins\android\lib\templates\activities\

How to use

1. Create Base classes

The main idea of the base classes is to have common methods that shared across the activities, fragments, contracts and presenters, written down in one place.

For instance: In our MVP template, all views will need to set the presenter so wrote a method, setPresenter(), in the BasePresenter so all the presenters classes inheriting from BasePresenter need to implement the method.

Here are the needed bases classes:

  • BaseActivity
  • BaseFragment
  • BaseContract
  • BasePresenter

2. Generate MVP template

As can be seen in the demo, I create a package called test. Then I right-click on the package that I want to generate the starting MVP files and browse to New → MVP → MVP Template Google Architect. After that you can provide the desired activity and layout name.

You will see in the test package, an Activity, Fragment, Contract and Presenter are generated; and in the layout file there are two layout files generated for activity and fragment. 🙂

3. Resolve imports and add your activities to manifest

Import classes as needed. And currently this template does not add the created activity to manifest.xml, so you need to manually add your activity to manifest.xlm file.

Updated

I added support for Kotlin, so there are two templates:

  • MVP Template Google Architect (Java)
  • MVP Template Google Architect (Kotlin)

If you install manually and interested in Kotlin only, you need to copy folder MVP Template Google Architect (Kotlin) otherwise if you install via the script install.sh then both template should be available for you.

One important thing to note is that you need to select source language for each template. In other word:

  • If you choose MVP Template Google Architect (Java) template → source language need to be Java
  • If you choose MVP Template Google Architect (Kotlin) template → source language need to be Kotlin

Here is a small demo how we can generate Kotlin MVP template:

Known issue

From Riggaroo blog/ known issue for MVP template: When upgrading Android Studio, and you have custom groups of file templates in the installation area specified above, Studio will say you have conflicts in the area that need to be deleted. Unfortunately you will have to remove them, and then place them in the same folder after the upgrade. Check out this feature request here.

Visit the GitHub repository:

Recommend and share these articles to others if you find them helpful.