Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio custom project template with two or more Modules together

I am trying to create an Android Studio custom project template with Domain and data module together using Free Marker. But i can't able to create new module at time of new project creation. Please help me on this

Edit : Am using this blog for creating custom template https://robusttechhouse.com/tutorial-how-to-create-custom-android-code-templates/ But it will generate only one app Module

like image 246
OMAK Avatar asked Apr 28 '17 09:04

OMAK


1 Answers

This is no trivial task.

To understand the complexity behind the task you want to accomplish you would have to read through the source code of Android Studio and/or IntelliJ Community Edition.

So I'll outline a couple of possible alternative solutions to your problem, and then I'll explain some of the complexity related to creating your own Android project template.

  • 1st approach: Create a template app project that already has two modules.
    This is the easiest way. You can create this project that will serve as base for your other projects. You can put it under version control and the re-use a specifc version of it. From Android Studio you can import this by using:
    File-> New-> Import Project... File-> New-> Project From Version Control

  • 2nd approach: Modifty the NewAndroidProject template and / or templates of modules you want to use.
    Navigate to the following path: <your_androidstudio_installation_directory>/plugins/android/lib/templates/gradle-projects/NewAndroidProject

    This is the template that is used for most (or all) of the Android projects.

    This one instead is the template that is used for each module you use in your application, such as the "app" moudle, a library module, and few other. You can customize the files within, but it will affect ALL the projects that you create in Android Studio. <your_androidstudio_installation_directory>/plugins/android/lib/templates/gradle-projects/NewAndroidModule

    Modifying these two templates may or may not solve your problem, and I am not going to describe in depth how you could do that, because you may want to keep your standard way of creating new projects intact.

    This second approach of course does not answer your question, and this is why I present to you the 3rd approach.

  • 3rd approach: Build your own plugin for Android Studio The reason it's difficult to add your own project template to Android Studio is that until now Android Studio is programmed (and hard coded) to pick the project template files from a specific directory and to process them with FreeMarker template engine, and to finally process that result with Gradle importer to get to the state it shows you when you start a new project from scratch.

    To witness this behavior try to change the /plugins/android/lib/templates/gradle-projects/NewAndroidModule/root/build.gradle.ftl file even by adding a comment to it, restart Android Studio, and start a new project. You'll notice your change in the build.gradle file in the root directory of your new project.

    For now Android Studio is hard-coded to use that specific template and it is the reason you won't be able to change it's behavior unless you change templates or build your own plugin for Anroid Studio.

    By developing your own plugin for AS you'll be able to reuse specific components and wizards that AS is using to create a new project from scratch. In your case you'll have to create a new wizard which is going implement some more steps than the defult one, with the benefit of leaving the last one intact.

    You can start by downloading the source code and going through the following files: ModelWizard ModelWizardStep ActivityGalleryStep AndroidNewProjectAction

    But before you begin let me tell you that unless you're working within a team, then creating a plugin that solves your problem is going to be a huge effort. Android Studio / IntelliJ IDEA CE is an ecosystem on its own. It may take you a couple of weeks just to grasp the basics of developing a plugin, so it's up to you. Developing a plugin and learning how this IDE works is a great insight to have as you'll be able to automate your workflow even more once you understand how the parts connect together, but in this case I'd solve the problem by using the 1st approach for its simplicity and effectiveness.

TL;DR; Use 1st approach.

In case you work within a medium/large team, then develop a plugin. (3rd approach)



EDIT: 3rd approach implementation of a plugin (Getting started with plugin development):

IntelliJ Platform SDK Documentation: http://www.jetbrains.org/intellij/sdk/docs/index.html

Here is a github repo for the example code for my Droidcon Italy 2017 talk that I did with a colleage who I work with: https://github.com/synesthesia-it/droidcon_as_plugin

This is a basic example that you can download, compile, install and experiment with. It does few simple things to demonstrate basic concepts.

There are some videos on youtube about this topic:

  • Live Coding an IntelliJ IDEA Plugin from Scratch [ https://www.youtube.com/watch?v=-ZmQD6Fr6KE ]
  • Live Coding an IntelliJ IDEA Plugin from Scratch (Part 2)
    [ https://www.youtube.com/watch?v=s_cCB2R5_1k ]

The IntelliJ SDK documentation is great, but the best documentation available is source code from other plugins that are available for download. Many of the projects provide a link to a repo where you can download the code and experiment with it.
https://plugins.jetbrains.com/idea

@OMAK
Now let me tell you that you are probably not going to find what you're looking for in the documentation or plugins available.
The content I provided is targeted mainly at IDEA IDE (Android Studio is based on it), and the documentation to do what you're trying to do simply is not there. There is no guide that explains how to modify a wizard in android studio by adding steps to it or importing templates from non-standard directories.
The solution you're working on is very similar to what I developed in the company I work for. It is hard to get started, but being able to develop a plugin offers endless possibilities to automate multiple parts of your development workflow.
I may write a short guide on how to accomplish this in the non so distant future to document how to accomplish it. For the time being it's best to get started on your own by watching the tutorials on youtube.

like image 68
codeWhisperer Avatar answered Sep 20 '22 07:09

codeWhisperer