【发布时间】:2020-08-20 18:00:30
【问题描述】:
我试图创建项目模板以优化我的时间,我找到了很多关于它的答案,但不适用于最新版本的 Android Studio。我尝试复制现有的项目模板并工作,但前提是我使用与主项目模板相同的名称。
我只需要创建一个新的项目模板并在向导屏幕中显示,如下图所示:
我创建了我在此路径 {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