简体   繁体   中英

Android Studio Template package name

I tried make android studio template like activity template bythis instruction

Current package set in 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

But my current package is 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:

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

Use it in your receipt.xml.ftl as following:

! Replace SomeClass.kt.ftl with your class in app_package;

! Replace your_needed_path with path, in which you would like to put the file

! Replace someClassName with the id of the className from template.

<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. This can be easily fixed by overriding srcOut in your globals.kt.ftl with following:

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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