该文件可以定义如下常用的标签:
-
copy:复制文件到目标目录,可以用于将图标复制到项目的文件夹。
-
merge:合并,可以用于将文件和项目中现有的文件合并。
-
instantiate:通过FreeMarker将ftl文件中的变量都转换成对应的值,并且生成我们想要的文件。
-
open:在代码生成后,打开指定的文件。
FreeMarker是一个模板引擎,它可以用来生成输出文本(例如:HTML网页、电子邮件、配置文件、源代码等)的通用工具,如下图所示:
FreeMarker的工作原理,如下图所示:
代码如下所示:
<@kt.addAllKotlinDependencies />
<instantiate
from=“root/res/layout/activity_tan_jia_jun_for_dagger2.xml.ftl”
to=" e s c a p e X m l A t t r i b u t e ( r e s O u t ) / l a y o u t / {escapeXmlAttribute(resOut)}/layout/ escapeXmlAttribute(resOut)/layout/{layoutName}.xml" />
<instantiate
from=“root/src/app_package/TanJiaJunActivityForDagger2.kt.ftl”
to=" e s c a p e X m l A t t r i b u t e ( s r c O u t ) / u i / {escapeXmlAttribute(srcOut)}/ui/ escapeXmlAttribute(srcOut)/ui/{businessName}/activity/${activityName}.kt" />
<instantiate
from=“root/src/app_package/TanJiaJunViewModelForDagger2.kt.ftl”
to=" e s c a p e X m l A t t r i b u t e ( s r c O u t ) / u i / {escapeXmlAttribute(srcOut)}/ui/ escapeXmlAttribute(srcOut)/ui/{businessName}/viewmodel/${viewModelName}.kt" />
我解释下代码的逻辑:在指定的目录下,用activity_tan_jia_jun_for_dagger2.xml.ftl文件生成 l a y o u t N a m e . x m l 文 件 , 用 T a n J i a J u n A c t i v i t y F o r D a g g e r 2. k t . f t l 文 件 生 成 {layoutName}.xml文件,用TanJiaJunActivityForDagger2.kt.ftl文件生成 layoutName.xml文件,用TanJiaJunActivityForDagger2.kt.ftl文件生成{activityName}.kt文件,用TanJiaJunViewModelForDagger2.kt.ftl文件生成 v i e w M o d e l N a m e . k t 文 件 , 最 后 打 开 {viewModelName}.kt文件,最后打开 viewModelName.kt文件,最后打开{activityName}.kt文件。
存放ftl文件,也就是模板代码,代码如下所示:activity_tan_jia_jun_for_dagger2.xml.ftl
<layout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<variable
name=“viewModel”
type=" k o t l i n E s c a p e d P a c k a g e N a m e . u i . {kotlinEscapedPackageName}.ui. kotlinEscapedPackageName.ui.{businessName}.viewmodel.${viewModelName}" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background="@color/background_color">
</androidx.constraintlayout.widget.ConstraintLayout>
TanJiaJunActivityForDagger2.kt.ftl
package k o t l i n E s c a p e d P a c k a g e N a m e . u i . {kotlinEscapedPackageName}.ui. kotlinEscapedPackageName.ui.{businessName}.activity
import android.os.Bundle
import androidx.activity.viewModels
import ${applicationPackage}.R
import a p p l i c a t i o n P a c k a g e . d a t a b i n d i n g . A c t i v i t y {applicationPackage}.databinding.Activity applicationPackage.databinding.Activity{objectKind}Binding
import com.tanjiajun.androidgenericframework.ui.BaseActivity
import k o t l i n E s c a p e d P a c k a g e N a m e . u i . {kotlinEscapedPackageName}.ui. kotlinEscapedPackageName.ui.{businessName}.v
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
iewmodel.${viewModelName}
class a c t i v i t y N a m e : B a s e A c t i v i t y < A c t i v i t y {activityName} : BaseActivity<Activity activityName : BaseActivity<Activity{objectKind}Binding, ${viewModelName}>() {
override val layoutRes: Int = R.layout.${layoutName}
override val viewModel by viewModels<${viewModelName}> { viewModelFactory }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
TanJiaJunViewModelForDagger2.kt.ftl
package k o t l i n E s c a p e d P a c k a g e N a m e . u i . {kotlinEscapedPackageName}.ui. kotlinEscapedPackageName.ui.{businessName}.viewmodel
import com.tanjiajun.androidgenericframework.ui.BaseViewModel
import javax.inject.Inject
class ${viewModelName} @Inject constructor() : BaseViewModel() {
}
展示模板界面的缩略图,如下图所示:
用于定义创建面板的控件,代码如下所示:
<template
format=“8”
revision=“8”
name=“TanJiaJun Activity For Dagger2”
minApi=“9”
minBuildApi=“26”
description=“Creates a new activity of Android Generic Framework For Dagger2.”>
<parameter
id=“packageName”
name=“Package Name”
type=“string”
constraints=“package”
default=“com.tanjiajun.androidgenericframework” />
<parameter
id=“businessName”
name=“Business Name”
type=“string”
constraints=“nonempty”
default=“main” />
<parameter
id=“objectKind”
name=“Object Kind”
type=“string”
constraints=“nonempty”
default=“TanJiaJun” />
<parameter
id=“activityName”
name=“Activity Name”
type=“string”
constraints=“class|unique|nonempty”
suggest="${extractLetters(objectKind)}Activity"
default=“TanJiaJunActivity”
help=“The name of the activity class to create.” />
<parameter
id=“layoutName”
name=“Layout Name”
type=“string”
constraints=“layout|unique|nonempty”
suggest="${activityToLayout(activityName)}"
default=“activity_tan_jia_jun”
help=“The name of the layout to create for the activity.” />
<parameter
id=“viewModelName”
name=“ViewModel Name”
type=“string”
constraints=“class|unique|nonempty”
suggest="${extractLetters(objectKind)}ViewModel"
default=“TanJiaJunViewModel”
help=“The name of the viewModel class to create.” />
template_blank_activity.png
<
-
packageName:包名,默认值是com.tanjiajun.androidgenericframework。
-
businessName:业务名称,默认值是main。
-
objectKind:对象类型,默认值是TanJiaJun。
-
activityName:Activity名称,默认值是TanJiaJunActivity。
-
layoutName:布局名称,默认值是activity_tan_jia_jun。
-
viewModelName:ViewModel名称,默认值是TanJiaJunViewModel。
每一个parameter对应创建面板的一个控件,控件的id可以得到用户输入的值,用于渲染ftl文件。下面以TanJiaJunAcitivityForDagger2为例生成相应的代码。/ 生成代码 /activity_tan_jia_jun.xml,代码如下所示:
<layout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”>
<variable
name=“viewModel”
type=“com.tanjiajun.androidgenericframework.ui.ui.main.viewmodel.TanJiaJunViewModel” />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background="@color/background_color">
</androidx.constraintlayout.widget.ConstraintLayout>