【问题标题】:How do I create custom project templates to Android Studio 3.6 or above?如何为 Android Studio 3.6 或更高版本创建自定义项目模板?
【发布时间】:2020-08-20 18:00:30
【问题描述】:

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

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

Wizard screen

我创建了我在此路径 {AndroidStudio Program Files folder}\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】:

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

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

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

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

    UPD:模板提供程序 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)

    【讨论】:

    • 谢谢@Kuzneц,我的想法是创建项目模板,但我认为暂时不可能,我会尝试使用带有android插件的IntelliJ IDEA来解决这个问题,谢谢你回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多