Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How To Get Flavor's ApplicationId

I'm building an app wit different build variant flavors. The flavors are "Free" and "Paid". I want to create some logic on my java classes which should only get triggered if the app is "Paid". Therefore I need a way to get the "applicationId" set during the gradle build process as shown below:

gradle.build

    productFlavors {
    free {
        applicationId "com.example.free"
        resValue "string", "app_name", "Free App"
        versionName "1.0-free"
    }
    paid {
        applicationId "com.example.paid"
        resValue "string", "app_name", "Paid App"
        versionName "1.0-paid"
    }

Once I have the application ID I could do something like this:

    if(whateverpackageid.equals("paid")) {
      // Do something or trigger some premium functionality.
    } 

Am I correct to say that during the gradle build process the "applicationId" eventually becomes the "package name" once the app has been compiled? If so what is the best way to get either the "application ID" or "package name" so that I can implement some flavor dependent logic in my java files?

like image 344
CBA110 Avatar asked Sep 27 '15 23:09

CBA110


People also ask

How do I find my app ID Android?

Android. We use the Application ID (package name) to identify your app inside our system. You can find this in the app's Play Store URL after 'id'. For example, in https://play.google.com/store/apps/details?id=com.company.appname the identifier would be com.

Where is build variants in Android Studio?

Change the build variant By default, Android Studio builds the debug version of your app, which is intended for use only during development, when you click Run. To change the build variant Android Studio uses, select Build > Select Build Variant in the menu bar.

What are build variants in Android?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.


1 Answers

I would use build configuration variables in your product flavors. Something along the lines of:

productFlavors {
    free {
        applicationId "com.example.free"
        resValue "string", "app_name", "Free App"
        versionName "1.0-free"
        buildConfigField "boolean", "PAID_VERSION", "false"
    }
    paid {
        applicationId "com.example.paid"
        resValue "string", "app_name", "Paid App"
        versionName "1.0-paid"
        buildConfigField "boolean", "PAID_VERSION", "true"
    }
}

Then after a build you can use:

if (BuildConfig.PAID_VERSION) {
    // do paid version only stuff
}

You may have to do a sync/build on gradle after you add the attribute before you can compile and import the BuildConfig class that Gradle generates on your behalf.

like image 99
zerobasedindex Avatar answered Sep 16 '22 12:09

zerobasedindex