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

问题描述 投票:0回答:1

我试图创建项目模板来优化自己的时间,我找到了很多答案,但是不适用于最新版本的Android Studio。我试图复制一个现有的项目模板并且可以工作,但前提是我使用的是主项目模板的相同名称。

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

Wizard screen

我在此路径{AndroidStudio Program Files文件夹} \ plugins \ android \ lib \ templates \ activities中创建了BasicActivity文件夹的副本,并更改​​了所复制模板的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 android-studio intellij-idea android-studio-3.0
1个回答
0
投票

是的。在Android Studio 3.6和4.0 beta5中,由于模板标题列表是硬编码的in the source code,因此无法在“新建项目向导”中将新的活动模板添加到“移动”形状因数中。但是可以将模板添加到其他形状因数中。

请注意,即使对于移动设备,也可以使用新的活动模板之后项目已创建(File > New > Activity > "New Template Title here"

[希望从Android Studio 4.0开始,以扩展点的形式打开Template API(请参阅https://issuetracker.google.com/issues/154531807

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

© www.soinside.com 2019 - 2024. All rights reserved.