如何为 Android Studio 3.6 或更高版本创建自定义项目模板?

How do I create custom project templates to Android Studio 3.6 or above?

我试图创建项目模板来优化我的时间,我找到了很多关于它的答案,但不适用于最新版本的 Android Studio。我尝试复制现有的项目模板并成功,但前提是我使用与主项目模板相同的名称。

我只需要创建一个新的项目模板并显示在向导屏幕中,如下图所示:

Wizard screen

我创建了一个 BasicActivity 文件夹的副本,我在这个路径 {AndroidStudio Program Files 文件夹}\plugins\android\lib\templates\activities 中找到并更改了我复制的模板的 template.xml 文件,更改了名称模板和类别值。

但是重启 Android Studio 后,我的新项目没有显示,我需要做什么才能显示新项目模板?

我正在阅读 IntelliJ 存储库中 android 插件的源代码,我的模板格式正确,可以在向导中显示。

我的例子:template.xml

<template
        revision="1"
        name="My template"
        minApi="9"
        minBuildApi="14"
        description="Example template">

    <parameter
        id="className"
        name="Feature name"
        type="string"
        constraints="class|nonempty|unique"
        default="ExampleFeature"
        help="Feature name (omit 'fragment' suffix)" />

    <category value="Activity"/>

    <!-- 128x128 thumbnails relative to template.xml -->
    <thumbs>
        <!-- default thumbnail is required -->
        <thumb>template_basic_activity.png</thumb>
    </thumbs>

    <globals file="globals.xml.ftl"/>
    <execute file="recipe.xml.ftl"/>

</template>

globals.xml.ftl

<?xml version="1.0"?>
<globals>
 <global id="resOut" value="${resDir}" />
 <global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
</globals>

recipe.xml.ftl

<!--?xml version="1.0"?-->
<recipe>
 <instantiate from="root/res/layout/fragment_demo.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/fragment_${classToResource(className)}.xml"></instantiate>
 <open file="${escapeXmlAttribute(resOut)}/layout/fragment_${classToResource(className)}.xml"></open>

 <instantiate from="root/src/app_package/DemoView.java.ftl" to="${escapeXmlAttribute(srcOut)}/${className}View.java"></instantiate>
 <open file="${escapeXmlAttribute(srcOut)}/${className}View.java"></open>

 <instantiate from="root/src/app_package/DemoPresenter.java.ftl" to="${escapeXmlAttribute(srcOut)}/${className}Presenter.java"></instantiate>
 <open file="${escapeXmlAttribute(srcOut)}/${className}Presenter.java"></open>

 <instantiate from="root/src/app_package/DemoFragment.java.ftl" to="${escapeXmlAttribute(srcOut)}/${className}Fragment.java"></instantiate>
 <open file="${escapeXmlAttribute(srcOut)}/${className}Fragment.java"></open>
</recipe>

分析IntelliJ android插件,我发现这一点可能是IDE搜索用户模板目录的地方,我也把我的模板副本放在这个地方但没有成功了。

  private static List<File> getUserDefinedTemplateRootFolders() {
    List<File> folders = new ArrayList<>();

    String homeFolder = AndroidLocation.getFolderWithoutWrites();
    if (homeFolder != null) {
      // Look in $userhome/.android/templates
      File templatesFolder = new File(homeFolder, FD_TEMPLATES);
      if (templatesFolder.isDirectory()) {
        Collections.addAll(folders, templatesFolder);
      }
    }
    return folders;
  }

此时:

@GuardedBy("CATEGORY_TABLE_LOCK")
  private void addTemplateToTable(@NotNull File newTemplate, boolean userDefinedTemplate) {
    TemplateMetadata newMetadata = getTemplateMetadata(newTemplate, userDefinedTemplate);
    if (newMetadata != null) {
      String title = newMetadata.getTitle();
      if (title == null || (newMetadata.getCategory() == null &&
                            myCategoryTable.columnKeySet().contains(title) &&
                            myCategoryTable.get(CATEGORY_OTHER, title) == null)) {
        // If this template is uncategorized, and we already have a template of this name that has a category,
        // that is NOT "Other," then ignore this new template since it's undoubtedly older.
        return;
      }
      String category = newMetadata.getCategory() != null ? newMetadata.getCategory() : CATEGORY_OTHER;
      File existingTemplate = myCategoryTable.get(category, title);
      if (existingTemplate == null || compareTemplates(existingTemplate, newTemplate) > 0) {
        myCategoryTable.put(category, title, newTemplate);
      }
    }
  }

没错。在 Android Studio 3.6 和 4.0 beta5 中,无法在新建项目向导中向“移动”form-factor 添加新的 activity 模板,因为模板标题列表是 hard-coded in the source code。但是可以将模板添加到其他 form-factors.

请注意,即使对于移动设备 form-factor,也可以在 创建项目 后使用新的 activity 模板 (File > New > Activity > "New Template Title here")

从 Android Studio 4.0 开始,模板 API 将(希望)以扩展点的形式打开(参见 https://issuetracker.google.com/issues/154531807

在 IntelliJ IDEA 中,可以从现有项目create a template

更新: 模板提供程序 API 现在可以在 Android Studio 4.1 中使用。 EP 名称是 com.android.tools.idea.wizard.template.wizardTemplateProvider (https://android.googlesource.com/platform/tools/base/+/refs/heads/studio-master-dev/wizard/template-plugin/src/com/android/tools/idea/wizard/template/WizardTemplateProvider.kt)