Ren*_*anP 2 android intellij-idea android-studio android-studio-3.0
我试图创建项目模板来优化我的时间,我找到了很多关于它的答案,但不适用于最新版本的 Android Studio。我尝试复制现有的项目模板并工作,但前提是我使用与主项目模板相同的名称。
我需要的只是创建一个新的项目模板并显示在向导屏幕中,如下图所示:
我创建了一个 BasicActivity 文件夹的副本,我在此路径 {AndroidStudio Program Files folder}\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>
Run Code Online (Sandbox Code Playgroud)
globals.xml.ftl
<?xml version="1.0"?>
<globals>
<global id="resOut" value="${resDir}" />
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
</globals>
Run Code Online (Sandbox Code Playgroud)
配方.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>
Run Code Online (Sandbox Code Playgroud)
分析 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;
}
Run Code Online (Sandbox Code Playgroud)
此时:
@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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
没错。在 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 )
| 归档时间: |
|
| 查看次数: |
1805 次 |
| 最近记录: |