android - 如何为 Android Studio 3.6 或更高版本创建自定义项目模板?
问题描述
我试图创建项目模板以优化我的时间,我找到了很多关于它的答案,但不适用于最新版本的 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>
配方.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 中,无法在新建项目向导中将新的活动模板添加到“移动”外形,因为模板标题列表是硬编码在源代码中的。但是可以将模板添加到其他外形尺寸。
请注意,即使对于移动设备,新的活动模板也可以在项目创建后使用 ( File > New > Activity > "New Template Title here"
)
从 Android Studio 4.0 开始,模板 API 将(希望)以扩展点的形式打开(参见https://issuetracker.google.com/issues/154531807)
在 IntelliJ IDEA 中,可以从现有项目创建模板。
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/模板/WizardTemplateProvider.kt )
推荐阅读
- sql - 将 Access 数据库复制到 SQL Server 正常但参数输入查询失败
- java - 如何在 Java 中使用 PDFPTable 从上到下将 2 个表添加到单个表中
- python - 不能根据来源的内容写日期
- xpages - IBM Designer 客户端在 Notes 客户端中启动表单和视图!CLFRJ0010E 有时
- c# - 如何摆脱“Animator 没有播放 AnimtorController”警告?
- iis - 使用 IIS7 URL Rewrite 将虚拟目录的请求转发到内部 Web 服务器
- azure-iot-sdk - 未链接到 libcurl 的示例
- javascript - 围绕 javascript/html 数据选项的 if 语句
- angularjs - angularjs 1.7.2 - 如何从控制器调用指令函数?
- r - 如何在保留 pdf 书签文本的同时获取无衬线标题?