我正在创建一个代码模板来加快我的工作。模板工作正常,如
class SimpleActivity : AppActivity<ActivitySimpleBinding, SimpleActivityViewModel>() {
override fun layoutID(): Int = R.layout.activity_simple
override fun setViewModel(): SimpleActivityViewModel {
val mViewModel: SimpleActivityViewModel by viewModels()
return mViewModel
}
}
唯一的问题是在使用我的模板时编写正确的应用程序包名称。我可以去掉它吗?有没有默认的方法在模板中选择它
<?xml version="1.0"?>
<template
format="5"
revision="3"
name="MVVM Activity"
description="Creates a Activity with a ViewModel."
minApi="7"
minBuildApi="21">
<category value="Activity" />
<formfactor value="Mobile" />
<parameter
id="activityClass"
name="Activity Name"
type="string"
constraints="class|nonempty|unique"
default="BlankActivity"
help="The name of the fragment class to create" />
<parameter
id="layoutName"
name="Activity Layout Name"
type="string"
constraints="layout|nonempty|unique"
default="blank_activity"
suggest="${classToResource(activityClass)}_activity"
help="The name of the layout to create" />
<parameter
id="viewModelName"
name="ViewModel Name"
type="string"
constraints="class|nonempty|unique"
default="BlankViewModel"
suggest="${underscoreToCamelCase(classToResource(activityClass))}ViewModel"
help="The name of the ViewModel class to create" />
<parameter
id="appPackageName"
name="app Package name"
type="string"
constraints="package"
default="how.to.get.default.package.here"
help="app package name" />
<thumbs>
<thumb>template_blank_fragment.png</thumb>
</thumbs>
<globals file="globals.xml.ftl" />
<execute file="recipe.xml.ftl" />
</template>
请参阅下面的appPackageName。有没有办法在这里设置默认的软件包名称,或者在AppActivity.kt.ftl中获取appPackage名称
<parameter
id="appPackageName"
name="app Package name"
type="string"
constraints="package"
default="how.to.get.default.package.here"
help="app package name" />
我想删除上面的参数,或者在这里自动设置默认的项目密码。
有办法实现吗?
您不需要在此处添加包名称字段即可获得应用程序基本包名称。你可以在你的应用中使用applicationPackage
package ${packageName}
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import ${applicationPackage}.R
import ${applicationPackage}.databinding.${fragmentClass}Binding
import com.chi.commonbase.base.BaseFragment
class ${fragmentClass} : BaseFragment<${fragmentClass}Binding,${viewModelName}>() {
override fun layoutID(): Int = R.layout.${layoutName}
override val mViewModel: ${viewModelName} by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding.mViewModel = mViewModel
}
}