androidandroid-studiokotlinintellij-ideacode-templates

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
    }
}

The only issue is to write your correct App packageName while using my template. Can I get rid of it. Is there any default way to select it in template

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

See appPackageName below . Is there a way to set default packagename here or a way to get appPackage name in AppActivity.kt.ftl

<parameter
        id="appPackageName"
        name="app Package name"
        type="string"
        constraints="package"
        default="how.to.get.default.package.here"
        help="app package name" />

I want to remove above parameter or to set default Project pacakge here automatically .

Is there a way to achieve it.


Solution

  • You don't need to add package name field here to get application base package name. you can use applicationPackage in your app

    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
        }
    
    }