0

我正在创建一个代码模板来加快我的工作。该模板工作正常,如下所示

class SimpleActivity : AppActivity<ActivitySimpleBinding, SimpleActivityViewModel>() {
    override fun layoutID(): Int = R.layout.activity_simple
    override fun setViewModel(): SimpleActivityViewModel {
        val mViewModel: SimpleActivityViewModel by viewModels()
        return mViewModel
    }
}

唯一的问题是在使用我的模板时编写正确的App packageName 。我能摆脱它吗。是否有任何默认方法可以在模板中选择它

<?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" />

我想删除上面的参数或者在这里自动设置默认的项目包。

有没有办法实现它。

4

1 回答 1

1

您无需在此处添加包名称字段即可获取应用程序基础包名称。你可以在你的应用程序中使用 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
    }

}
于 2020-09-22T06:18:17.193 回答