简体   繁体   English

Android Studio 模板包名称

[英]Android Studio Template package name

I tried make android studio template like activity template bythis instruction我尝试通过此指令制作类似活动模板的 android studio 模板

Current package set in globals.xml.ftl globals.xml.ftl 中设置的当前包

 <global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />

and create template file并创建模板文件

<instantiate from="src/app_package/LifecycleFragment.java.ftl"
               to="${escapeXmlAttribute(srcOut)}/${className}.java" />

It force my template file to src/main/java/myPackageName它强制我的模板文件为 src/main/java/myPackageName

But my current package is kotlin但我目前的软件包是 kotlin

How can i create template in current selected folder?如何在当前选定的文件夹中创建模板?

Sorry if I'm late with my answer, but maybe it will be useful for someone.对不起,如果我的回答迟到了,但也许它对某人有用。

To change the source package from java to kotlin try add this line to globals.xml.ftl:要将源包从 java 更改为 kotlin,请尝试将此行添加到 globals.xml.ftl:

<global id="kotlinMainSourceSet" value="${srcOut?replace('java','kotlin') />

Use it in your receipt.xml.ftl as following:在您的receipt.xml.ftl 中使用它,如下所示:

! Replace SomeClass.kt.ftl with your class in app_package;将 SomeClass.kt.ftl 替换为 app_package 中的类;

! Replace your_needed_path with path, in which you would like to put the file将 your_needed_pa​​th 替换为您想要放置文件的路径

! Replace someClassName with the id of the className from template.将 someClassName 替换为模板中 className 的 id。

<instantiate
        from="src/app_package/SomeClass.kt.ftl"
        to="${escapeXmlAttribute(kotlinMainSourceSet)}/${your_needed_path}/${someClassName}.kt" />

In addition I'll answer on the @AlexeiKorshun question with which I've faced too: applicationIdSuffix is added, while creating a directory.此外,我将回答我也遇到过的 @AlexeiKorshun 问题:在创建目录时添加了 applicationIdSuffix。 This can be easily fixed by overriding srcOut in your globals.kt.ftl with following:这可以通过使用以下内容覆盖 globals.kt.ftl 中的 srcOut 来轻松解决:

<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName?replace('.dev|.qa2|.stage', '', 'r'))}" />

.dev/.qa2/.stage are added as an example,please replace them with yours.以.dev/.qa2/.stage为例添加,请自行替换。

You can use this if you are not using default conventions. 如果不使用默认约定,则可以使用此选项。

sourceSets {
    main.kotlin.srcDirs += 'src/main/myKotlin'
    main.java.srcDirs += 'src/main/javaPackage'
}

//myKotlin must be ur current directory // myKotlin必须是您的当前目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM