Original article, no reprint.
AS is known to all, a page can be created quickly by using the activity template of the system in AS, but often the types provided by the system cannot meet our own needs, especially in today’s popular MVP, MVVM and other modes, so we began to customize templates long ago, so AS to quickly generate files according to their own needs. Specifically refer to my previous article:Studio templates, one click to generate multiple classes, MVP party benefits
AS templates are stored in the android-studio3.6\plugins\ Android \lib\templates directory. The templates directory is not found in plugins\ Android \lib AS. Android-studio/plugins/android/lib/wizard-template.jar = android-studio/plugins/android/lib/wizard-template.jar = android-studio/plugins/android/lib/wizard-template.jar = android-studio/plugins/android/lib/wizard-template.jar = android-studio/plugins/android/lib/wizard-template.jar
Students who have written custom templates are very familiar with the above two pictures, is the template resources. So the first thing THAT comes to my mind is to modify the JAR package AS and re-sign it to overwrite the original package, but his JAR package is actually written with Kotlin! And saw the template syntax and before completely different, now more is made of the code layout file, rather than generated by the template as before, considering the difficult problem decisively abandoned, then all of a sudden a great god colleagues before tang a used FreeMarker plug-in wrote a function template similar things, and I over here decisive change.
First, the effect picture:
Here are the automatically generated classes:
Plug-in source code will not explain, understand Java people a look to understand, the following is the plug-in source link:
Plug-in source
Finally, there are still some problems in plug-in mode compared with the previous template generation:
1. Failed to get android main package name, causing automatic import of databinding automatically generated classes.
2. The activity cannot be automatically registered
3. You cannot read the template file inside the plug-in package, so you must place the template file in a directory to use it
If you solve the above two problems, please leave a message
Update log for March 15, 2021: Reads the template implementation directly from the plug-in package jar, no longer need to put the template file in local directory, only need to initialize the Configuration is used when setClassForTemplateLoading (getClass (), “/ template”); Method, template is the directory under the resources directory, refer to the plugin source code, has been synchronized