Supercharge Your Workflow: A Step-by-Step Guide to Advanced File Templates

Crafting Custom Path Formatting in IntelliJ File Templates for Android Development

Advanced IntelliJ File Templates: Templates with multiple files and folders

Marc Picone
by MWM

--

Introduction

Creating files and folders in a project can be a repetitive and time-consuming task, especially when following a specific architecture pattern. However, IntelliJ File Templates can automate this process and save you a lot of time.

In this article, we’ll show you how to create complex templates with multiple files and folders, using IntelliJ File Templates.

Moreover, file templates promote a shared understanding of best practices by providing predefined file structures that the team can adopt together.

So let’s dive in and take your IntelliJ File Template game to the next level!

Link

Articles :

Gist :

1. Introduction to Custom Path Formatting

Let’s start by creating a simple Kotlin class file using IntelliJ’s File Templates.

To create a new template, navigate to “File” > “New” > “Edit File Template…” in the main menu. Select the “Files” tab under “File and Code Templates” and click the “+” button to create a new file template.

In the “Name” field, enter a name for your template that will identify it in the “New” menu.

You can use the ${NAME} variable in the “File name” field and select the extension for the file, such as kt.

Once you’ve created your first file template, you can click “OK”, and the IDE will propose the creation of your file template with the name you specified when creating a new file.

To create a folder and organise your file in a package, you can add the package name to the “File name” field, separated by a slash. This will create a folder with the specified package name and place your file inside it.

For example, you can enter my_package/${NAME} in the “File name” field. This will create a package called “my_package” and a file with the name of your choosing.

Now, let’s take a look at how we can make this process even more efficient by using custom path formatting with Velocity Template Language.

2. Velocity Template Language

Velocity Template Language (VTL) is a Java-based language that provides several useful methods for writing templates. Even predefined variables like ${NAME} use VTL.

Let’s assume that the ${NAME} variable no longer stands for the file name in CamelCase, but instead, it represents the package name in snake_case. We want our path to be the package name provided in snake_case followed by the file name in CamelCase.

To accomplish this, we can use the StringUtils class, which contains the removeAndHump method. Our new path would look like this:

${NAME}/${StringUtils.removeAndHump(${NAME}, "_")}

With this template, the IDE will prompt you to enter the file name, but instead of entering the file name in CamelCase, you’ll enter the package name in snake_case, and the IDE will create the package with the provided name and attach the file in CamelCase.

You can also hardcode a string after the path. For example, if you want your file to be a view, you can add it this way:

${NAME}/${StringUtils.removeAndHump(${NAME}, "")}View

Now, let’s create another package with its own file.

3. Create the layout file

To continue with our example, let’s say we need to create a custom layout for a view in our Android app. While we could hardcode the path of our app’s res folder to create the layout file, we want a more adaptable solution that works for all apps. To achieve this, we’ll need to find a way to determine the number of folders we are from the app’s root folder when creating the template.

Thankfully, IntelliJ provides a variable called ${PACKAGE_NAME}, which gives us the current file’s package name separated by dots. While this variable is typically used to fill in the package name at the top of a file, we can use it to determine the path we need to create our layout file.

To do this, we’ll create a new snippet in the “Include” tab of the template editor called “RootPath”.

This script initialises variables that we’ll use to construct the path.

The foreach loop splits the ${PACKAGE_NAME} variable on dots, and for each iteration, we add “../” to the $rootRelativePath variable to navigate up one folder in the root folder.

Once the loop completes, we check if it’s the last iteration and concatenate $endRootPath to access the “root” folder.

Finally, we call $rootRelativePath, which gives us the desired result and allows us to create a secondary package or navigate to other desired folders within the package.

Here is the updated code for the “RootPath” snippet:

Root path snippet

Note that there should be no spaces at the beginning or end of every line as this can cause issues with package creation.

To create a child template for our custom layout, select the file template and click on the “Add New Child Template” button on the right of the “+” button. In the child template file name, enter the following:

#parse("RootPath.kt")/res/layout/${NAME}

Make sure to set the extension as XML.

With this template, you can now create both the folder and file for your custom layout in a quick and efficient manner.

Conclusion

In this guide, we have learned how to use file templates to automate the creation of Android app components. By using variables such as ${PACKAGE_NAME}, we can create adaptable solutions that work for any app.

We have also seen how to use the #set and #foreach directives to create a custom path and how to create a child template for a custom view.

By automating this process, you can save time and promote best practices across your team. With these techniques, you can quickly create new files and folders that adhere to your architecture pattern without worrying about the directory structure.

If you want to take your file template skills to the next level, check out my other articles on creating a template for a custom view with MVP and a layout template, and a more complex example on templating Room database and Room entity.

--

--

Marc Picone
by MWM
Writer for

Hello! I'm Marc, Android developer at MWM. I've been creating innovative music, drawing, and creative apps with MWM since 2021. Let's connect and exchange ideas