1. Create a template

1.1. Configure templates extension

Once the plugin applied on your project, you can configure the template extension

                templates {
                    //define the folder where the files will be installed
                    templatesName "myCustomTemplates"
                    //create a new template named PandroidFragmentTest
                    template('PandroidFragmentTest') {
                        //template description
                        description = "Creates a new basic PandroidFragment with a presenter."
                        //template category
                        category = "CustomActivities"
                        //template formfactor
                        formfactor = "Mobile"
                        //java files to use
                        javaFiles = project.fileTree(templatesJava).include('fragment/*.java')
                        //resources files to use (layout, values, manifest...)
                        resFiles = project.files(new File(templatesFolder.path, "res/layout/fragment_base.xml"))

                        //configure your template parameters
                        parameters {
                            //variable name
                            layoutName {
                                //value to remplace in source files
                                replace = 'fragment_base'
                                //user label to display. If not set, the variable won't be visible
                                label = "Layout Name"
                                //template constraint. See existing template
                                constraints = "layout|unique|nonempty"
                                //suggest value
                                suggest = 'fragment_${classToResource(fragmentClass)}'
                                //help value to display
                                help = "The name of the layout to create for the fragment"
                            }

                            //another example
                            packageName {
                                replace = 'com.leroymerlin.templates'
                                label = "Package Name"
                                constraints = "package"
                                help = "The package name of the fragment"
                            }
                        }
                    }
                }

1.2. Build it

To build your template, you can use one of those gradle functions :

  • zipTemplates - to create a zip file with your templates

  • installTemplates - to install the zipFile in your android studio

  • uninstallTemplates - to uninstall the templates from your computer

Note
if your android studio installation is not at a standard location, you can set the installation path with environment variable ANDROID_STUDIO_HOME

1.3. Publish with delivery

If you want to share your templates with your team, you can use Delivery plugin to publish it to a directory / ftp / repository / …​

Here is a basic example

def zipTemplatesTask = project.tasks.getByName("zipTemplates")
task buildTemplate(type: DeliveryBuild, dependsOn: zipTemplatesTask) {
    variantName = templates.templatesName
    outputFiles = ["templates": zipTemplatesTask.outputs.files.singleFile]
}

For more details see Delivery plugin documentation

2. Use your template

To use a published template, you have to apply the plugin on your project and then, add the template dependency

For example with a maven template

dependencies {
    template 'com.leroymerlin.templates:mytemplates:1.0.0'
}
Note
Each time you install a new template, Android Studio needs to be restarted in order to apply it.