In the article
“Moxy - MVP implementation for Android with a pinch of magic” we told how to overcome the problem of the life cycle and divide the code of your Android project into layers. However, MVP (even with smart code generation of the view state in Moxy) makes it necessary to write a huge amount of redundant code (to create presenter and view classes and interfaces connected to each other). In fact, the robot should do this work for you! :) It is time to reduce boilerplate even more with customization of Android Studio templates for Moxy.
Project structure
In order to start code generation, you need to fix the structure of the project.
The contents of the presenter, view, activity and fragment packages, in turn, are divided into logical modules. Often, such modules are sections of the application (for example, intro, offers, feed). Below is an example of a project structure with two Activities (CarActivity and HomeActivity) and one fragment (CarDetailsFragment)

')
Our goal is to learn how to generate these classes.
Customize templates
How to create templates in Android Studio can be read in the article from
Fi5t about
Total TemplatingWe add templates for Moxy to the project:
- Download templates from Github or by reference.
- Copy the contents of the archive in ANDROID_STUDIO_DIR / plugins / android / lib / templates / activities
- Restart Android Studio for changes to take effect.
Customize hot keys for quick access to templates:
- Open Settings-> Keymap
- In the search box, enter Moxy
- Add keyboard shortcuts (I use Alt + A to activate and Alt + F for a fragment)

Use of templates
- Select the root package and press Alt + A.
- In the Activity Name field we write “MyFirstMoxyActivity”
Remaining fields will be typed.

Next we change the word blank in the Package Name field to the name of the subpackage and click Finish. Your class pack is ready! It is worth noting that when updating Android Studio can demolish all custom templates. In this case, you will have to import them again.
What is the result
The concept of MVP implies the division of application logic into layers and, as a result, an increase in the code base. The use of patterns and code generation frees you from the boilerplate as much as possible, protects against random errors and allows you to focus on business logic. Let the code be written by a robot for you!
Materials on the topic