Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting package name of application variant in gradle plugin

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?

like image 369
Edward Dale Avatar asked Jan 07 '14 13:01

Edward Dale


People also ask

How do I get the current flavor in Gradle?

There is no direct way to get the current flavor; the call getGradle().

What is a Buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

How do I find my Android package name?

You can find an app's package name in the URL of your app's Google Play Store listing. For example, the URL of an app page is play.google.com/store/apps/details? id=com. example.


1 Answers

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
}
like image 87
stealthcopter Avatar answered Oct 01 '22 13:10

stealthcopter