# android-code-template **Repository Path**: duraemon/android-code-template ## Basic Information - **Project Name**: android-code-template - **Description**: 代码模版,生成一套样例代码,提示开发效率 - **Primary Language**: Kotlin - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-12 - **Last Updated**: 2025-03-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Android Studio 代码模板插件 ## 背景 >可以跳过背景和简述,从模板插件实现开始看. 开发新页面时,原先需要写一堆模板代码。比如用Databinding写列表结构的页面,需要手写以下文件: * `XxActivity.kt` * `XxFragment.kt` * `XxViewModel.kt` * `XxListAdapter.kt` * `XxListItemModel.kt(UI数据结构)` * `XxBean.kt(接口数据结构)` * `XxBeanModelConvert.kt` * `XxRetrofitApi.kt` * `XxRetrofitRepository.kt` * `yymoudle_xx_layout_activity.xml` * `yymoudle_xx_layout_fragment.xml` * `yymoudle_xx_layout_list_item.xml(列表item)` 并且类文件间还有相互的引用关系。 >如果能有一套代码模板,可以一键生成最小单元功能代码,确实能提高新开发页面的效率。(加快1~2个小时不为过吧?) ## 简述 ### Android Studio Editor Android Studio 自带了两种代码模板(入口为Settings -> Edit): * `File and Code Templates` : 倾向生成单个文件 * `Live Templates` :在单个文件中快捷生成代码,例如logd生成`Log.d(TAG, String)` File and Code Templates 和Live Templates ![截屏2023-07-26 16.46.37.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/817c88c377eb4a68a585605c4a360b50~tplv-k3u1fbpfcp-watermark.image?) ### Android Studio Plugin 于是把目光投向了代码模板插件,搜索`template`,可以看到很多,比如这个`Jetpack Compose UI Architecture Plugin`。 ![截屏2023-07-26 16.57.03.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e7d9f63652a84bca9b9b8f40e3ad0643~tplv-k3u1fbpfcp-watermark.image?) ![image.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2bce943dfbf44d08b4de6eb84213f212~tplv-k3u1fbpfcp-watermark.image?) 如何我实现自己的代码模板插件? ## 模板插件实现 Android Studio 是基于 IntelliJ IDEA开发的,Android Studio可以使用IntelliJ上丰富的插件。 IntelliJ提供了一个用于创建模板代码插件的模板项目,基于这个模板项目改造 。 ### 最终效果 > 选择模板 -> 模板配置 -> 生成代码 选择模板 ![截屏2023-07-26 17.16.29.png](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/fe263f5563014c71ae1ceae90cbc5981~tplv-k3u1fbpfcp-watermark.image?) 模板配置 ![截屏2023-07-26 17.18.19.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e74fedc6da3d420a923d556dd164fe8d~tplv-k3u1fbpfcp-watermark.image?) 生成代码 ![企业微信截图_86bca621-e6b9-4b7f-bd09-500cdf024a60.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a7271ef140e54ca8832a63b669a1f342~tplv-k3u1fbpfcp-watermark.image?) ### 插件工程的创建与配置 #### 工程创建 模板项目仓库地址: [https://github.com/JetBrains/intellij-platform-plugin-template](https://github.com/JetBrains/intellij-platform-plugin-template) 按照步骤 Use this template-> Create a new repository 在自己的github下生成仓库。 这是我生成的项目仓库地址:https://github.com/AlvinScrp/android-code-template ![截屏2023-07-26 17.06.30.png](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/fa3053ea3d5d4b789b92ad5645384f78~tplv-k3u1fbpfcp-watermark.image?) #### 修改配置 用Android Studio打开这个项目,先修改一波基础配置,修改内容的commit: https://github.com/AlvinScrp/android-code-template/commit/c0eaaa9c7a5451f29efb1b5f91eec131568d5f89 注意:要导入Android代码模板需要的 **wizard-template.jar**, 其实是从**Android Studio目录/plugins/android/lib**中复制过来的。 ### 模板插件代码编写 generator包下都是我们新写的代码,代码调用顺序为 **PluginGeneratorProvider.kt** -> **Generator.kt** -> **Recipe.kt** ![企业微信截图_e0135067-0241-4a7e-ab7f-35643ec9d7ae.png](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/becac53c6a8f4b05aabbf8c5c2a14af4~tplv-k3u1fbpfcp-watermark.image?) #### PluginGeneratorProvider 对应【选择模板】界面 ```kt package com.github.alvinscrp.androidcodetemplate.generator import com.android.tools.idea.wizard.template.Template import com.android.tools.idea.wizard.template.WizardTemplateProvider import com.github.alvinscrp.androidcodetemplate.generator.mvvm.jlMvvmGenerator import com.github.alvinscrp.androidcodetemplate.generator.util.AppType class PluginGeneratorProvider : WizardTemplateProvider() { override fun getTemplates(): List