Getting package name of application variant in gradle plugin

android, android-gradle-plugin, gradle

Solution

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
}

Problem

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 code, which stopped working with the most recent version of the android gradle plugin: ``` private String getPackageName(ApplicationVariant variant) { // TODO: There's probably a better way to get the package name of the variant being tested. String packageName = variant.generateBuildConfig.packageName if (variant.processManifest.packageNameOverride != null) { packageName = variant.processManifest.packageNameOverride } packageName } ``` This has stopped working in the most recent version of the android plugin because of changes to the build config processing. It seemed like a hack before anyway, so I'm not surprised it stopped working. Is there a canonical way to fetch the package name?

Original source