在 android 个工作室模板中获取包名称

Get package name in android studio templates

I am creating a code template to speed up my work. The template is working fine like below

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。我可以摆脱它吗?在模板

中是否有任何默认方法select
<?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
    }

}