Android Studio File Templates
This section lists the Android Studio file template code written in the VTL scripting language, followed by definitions of the variables. The values that you provide in the Create New Class dialog become the variable values in the template. Note that the lines that begin with
#if (${VISIBILITY}
extend all the way to the open brace ( {
).AnnotationType file template
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK} #end #parse("File Header.java") #if (${VISIBILITY} == "PUBLIC")public #end @interface ${NAME} #if (${INTERFACES} != "")extends ${INTERFACES} #end { }
Class file template
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK} #end #parse("File Header.java") #if (${VISIBILITY} == "PUBLIC")public #end #if (${ABSTRACT} == "TRUE")abstract #end #if (${FINAL} == "TRUE")final #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end { }
Enum file template
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK} #end #parse("File Header.java") #if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end { }
Interface file template
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK} #end #parse("File Header.java") #if (${VISIBILITY} == "PUBLIC")public #end enum ${NAME} #if (${INTERFACES} != "")implements ${INTERFACES} #end { #end { }
Singleton file template
#if (${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #if (${IMPORT_BLOCK} != "")${IMPORT_BLOCK} #end #parse("File Header.java") #if (${VISIBILITY} == "PUBLIC")public #end class ${NAME} #if (${SUPERCLASS} != "")extends ${SUPERCLASS} #end #if (${INTERFACES} != "")implements ${INTERFACES} #end { private static final ${NAME} ourInstance = new ${NAME}(); #if (${VISIBILITY} == "PUBLIC")public #end static ${NAME} getInstance() { return ourInstance; } private ${NAME}() { } }
File template variables
Android Studio replaces file template variables with values in the generated Java file. You enter the values in the Create New Class dialog. The template has the following variables that you can use:
IMPORT_BLOCK
- A newline-delimited list of Javaimport
statements necessary to support any superclass or interfaces, or an empty string (""
). For example, If you only implement theRunnable
interface and extend nothing, this variable will be"import java.lang.Runnable;\n"
. If you implement theRunnable
interface and extend theActivity
class, it will be"import android.app.Activity;\nimportjava.lang.Runnable;\n"
.VISIBILITY
- Whether the class will have public access or not. It can have a value ofPUBLIC
orPACKAGE_PRIVATE
.SUPERCLASS
- A single class name, or empty. If present, there will be anextends ${SUPERCLASS}
clause after the new class name.INTERFACES
- A comma-separated list of interfaces, or empty. If present, there will be animplements ${INTERFACES}
clause after the superclass, or after the class name if there’s no superclass. For interfaces and annotation types, the interfaces have theextends
keyword.ABSTRACT
- Whether the class should be abstract or not. It can have a value ofTRUE
orFALSE
.FINAL
- Whether the class should be final or not. It can have a value ofTRUE
orFALSE
.
No comments:
Post a Comment