androidandroid-studiotemplatesintellij-pluginpackage-name

Get the Application Package name in template Wizard recipe class Intellij


Get the Application Package name in template recipe class

For example:

I have four directories A, B, C, D.

A(parent) , B ( Child of A) , C (child of B) , D (Child of C).

let say I am standing in directory D and I want to create common directory (F) inside A along with B ( F sibling to B).


Solution

  • You can get the parent directory form srcOut like

    fun getApplicationPackageFile(srcOut: File, applicationPackage: String): File {
        var applicationPackageFile = srcOut.path.toString()
        var pk = applicationPackage.replace(".", "\\")
    
        val status: Boolean = applicationPackageFile.contains(pk)
        return if (status) {
            var file =
                applicationPackageFile.substring(0, applicationPackageFile.indexOf(pk)) + pk + "\\"
            File(file)
        } else {
            srcOut
        }
    }
    

    And call it like

    val pkFile = getApplicationPackageFile(srcOut, moduleData.projectTemplateData.applicationPackage)
    

    So If you want to save some file to the core application package..

    pkFile.resolve("AppViewModel.$ktOrJavaExt")
    

    For Linux and Mac Change above line to

    var pk = applicationPackage.replace(".", "/")