Want your own unique code templates? A culture and education you customize Android Studio code templates!

background

We know that in the development of Android, the official offers many templates, you can create a set of code directly, it is convenient to develop

In fact, this code template is not a new technology, Google is actually the Freemarker template engine integrated into Android Studio in.

Let's look at Android Studio templates, and customize code templates two.

On the template file

We open Android Studio template files, analyze constitute such a file, the file addresses ... \ Android Studio \ plugins \ android \ lib \ templates \, the template we used in AS are here appears.

We ... \ Android Studio \ plugins \ android \ lib \ templates \ activities \ LoginActivity, for example, analyze the template file is how it works

The role of the folder and all the files in the file as follows:

In the figure above, there have been three file, ftl, xml and png

  • ftl is freemarker file, template file operations with related operations are defined in the ftl file

  • There are two xml, template.xml at the root of our definition of the interface that appears when using a template; xml file to another folder under the res no logic functions, will be copied directly to our project

  • png only the root directory of the file, in fact, is displayed in the screen displayed when using our template, use the template as a logo

Let us look at the contents of the file three files in the root directory

·template.xml

template.xml defines the interface when using templates, we look at the template interface LoginActivity

We look at how to define the interface template.xml

Custom template categories

Defined entry:

Easy to see, the default value for each input frame, tips, etc. are types defined in the xml.

Note that, when we change the Activity Name, Layout Name also change, this is done by "suggest =" a c t i v i t Y T The L a Y The u t ( a c t i v i t y C l a s s ) " {ActivityToLayout (activityClass)} "" implementation in which {activityToLayout () is a function for the Activity the class name is converted into Layout XXXActivity name activity_XXX; and the parameters of the function "activityClass" is "Activity name" corresponding to the input box id "activityClass".

Finally, define the package name and logo

More than just the role of template template interface definition, in fact, by id defined in the template, the other ftl file can get the class name and file name such as we enter at the interface, to provide input parameters for the automatic generation of documentation and code.

For example, in the LoginActivity.java.ftl, generating XXXActivity.java file name of the file, the name of the class, the value will be assigned according to the template file activityClass

· globals.xml.ftl

Define some global variables

As shown above, in the globals in the definition of the API version of a boolean value, this value will be generated when LoginActivity code to determine whether to add the code portion

·recipe.xml.ftl

recipe.xml.ftl used to define how to generate the code and documentation

· Other template code file

In addition to globals.xml.ftl, recipe.xml.ftl, template.xml three files in the root directory, and other templates can be freely configured as required.

Production MVPActivity custom template

Template file structure is as follows:

The core is that the code folder in the three documents, namely the MVP of the interface, Presenter and Activity, file contents are as follows:

Contract template file:

Presenter template file:

Activity template file:

Put a few initialization file and copy it to the project file in recipe.xml.ftl

Finally, the entire folder to ... \ Android Studio \ plugins \ android \ lib \ templates \ activities \ down, restart Studio Android , it can be applied. Activity in the template class, one more Empty MVP Activity

Open interface is as follows:

We note that there is a front Biz Name of the input parameters, the role of this parameter is not generated files, but for the following file named because of Activity Name only affect Layout Name, and I hope to set up an interface with the Presenter name, but no Freemarker and $ {activityToLayout () function to generate corresponding Presenter interface and the name of the function, so I'm at the top adds such a variable, specifically for unified naming.

In particular template.xml file, adds an id "activityName" variable, and for subsequent assignments of variables, such as the "activityClass", the value is set to suggest the "$ {activityName} Activity", it spell the name of their own XXXActivity.

Generating a file structure as follows:

Activity generated files:

Generated Presenter:

Generated Contract:

So far, we have completed the custom code template preparation and use.

At last

You take your time to invest in learning, it means that you can gain skills, have the opportunity to increase revenue.

Here to share my Android PDF Daquan learn to learn, learn this Android PDF Daquan really contain all aspects, and contains basic Java knowledge, the basis of Android, Android Advanced extension, and so on collection algorithm

My collection this study, can effectively help you grasp the knowledge points.

In short we are also here to help enhance learning advanced, but also saves you the time to learn online search data can also be shared with close friends studying together

Access: concern I see personal introduction, or direct click me free to receive

Published 168 original articles · won praise 71 · views 20000 +

Guess you like

Origin blog.csdn.net/Aerfa789/article/details/104828973