Getting package name of application variant in gradle plugin

后端 未结 3 847
感情败类
感情败类 2021-02-19 08:53

I\'m building a gradle plugin that adds a new task for every application variant. This new task needs the package name of the application variant.

This is my current cod

相关标签:
3条回答
  • 2021-02-19 09:07

    I use this:

    // Return the packageName for the given buildVariant
    def getPackageName(variant) {
        def suffix = variant.buildType.packageNameSuffix
        def packageName = variant.productFlavors.get(0).packageName
        if (suffix != null && !suffix.isEmpty() && suffix != "null") {
            packageName += suffix
        }
        return packageName
    }
    
    0 讨论(0)
  • 2021-02-19 09:20

    This will handle nullable applicationIdSuffix for you:

    android.applicationVariants.all { variant ->
        def applicationId = [variant.mergedFlavor.applicationId, variant.buildType.applicationIdSuffix].findAll().join()
        println applicationId 
    }
    
    0 讨论(0)
  • 2021-02-19 09:34

    The only way I found was to define packageName in the project's build.gradle and then from the plugin do project.android.defaultConfig.packageName.

    If the project has flavors that define their own package then the solution stated by stealthcopter would work.

    However a plugin is meant to work with any project regarless whether packageName is defined or not in build.gradle so these solutions are far from ideal.

    Let me know if you find something better.

    0 讨论(0)
提交回复
热议问题