How to define and use a constant in Gradle build script (Android)?
Asked Answered
M

2

27

I'm working on an Android application with Gradle as its build system.

My objective is to use a value (a package name) as an applicationId:

productFlavors {
   orange {
      applicationId "com.fruits.android.orange"
      // ...

But also to expose it via BuildConfig so that Java code has access to it.

This access has to be from outside the flavor (namely, free version of the app needs to know the package name of the paid version so that it can prompt user for an upgrade in Play store).

So I'd like to do something like that:

productFlavors {
   orange {
      applicationId orangeProPackage
      // ...


buildConfigField 'String', 'ORANGE_PRO_PACKAGE', "$orangeProPackage" // ?

Only I'm not sure how to define orangeProPackage so that it's visible in the entire build.gradle and doesn't break the script.

Since there's a few different flavors, it would be best if I could somehow group all these constants like that (I guess?):

def proPackages = [
        orange: "..."
        apple: "..."
        banana: "..."
]

and then refer to them in a clean and descriptive manner like proPackages.orange etc.

The question is, how to accomplish that?

This is not a duplicate of Is it possible to declare a variable in Gradle usable in Java?

I've seen that question (and a few others). I know how to declare buildConfigFields, I already have plenty. My question is about reusing the same value as a buildConfigField and applicationId.

Matta answered 26/10, 2015 at 13:24 Comment(2)
Possible duplicate of Is it possible to declare a variable in Gradle usable in Java?Luftwaffe
@Maloubobola it's not a duplicate. I've seen that question (and a few others). I know how to declare buildConfigFields, I already have plenty. My question is about reusing the same value as a buildConfigField and applicationId.Matta
F
38

Only I'm not sure how to define orangeProPackage so that it's visible in the entire build.gradle and doesn't break the script.

You could put it in gradle.properties in your project root. Like other .properties files, it's just a key-value store:

ORANGE_PRO_PACKAGE=com.morawski.awesomeapp

You then refer to it as a simple global string variable (ORANGE_PRO_PACKAGE) in your build.gradle:

buildConfigField 'String', 'ORANGE_PRO_PACKAGE', '"' + ORANGE_PRO_PACKAGE + '"'

it would be best if I could somehow group all these constants

Anything involving .properties files won't handle that. There, you may be looking at defining globals in the top-level build.gradle file just in plain Groovy code or something.

Footsie answered 26/10, 2015 at 13:35 Comment(3)
Actually it should be buildConfigField 'String', 'ORANGE_PRO_PACKAGE', '"' + ORANGE_PRO_PACKAGE + '"' or else the value isn't generated as a string literal in BuildConfig and the build breaks. But other than that - it works, thanks for the tip.Matta
Is it possible to use a different .properties file for each flavour? One advantage will be that this way one can retain the same key (in gradle.properties) across the flavours. Based on the flavour since the file would change, the values of those keys would be different. If using the same gradle.properties file for a string whose value changes based on flavour, we would need to have different keys in same .properties file, which are harder to maintain over time.Loser
@ShobhitPuri: "Is it possible to use a different .properties file for each flavour?" -- there's nothing automatic for that AFAIK. But Gradle is Groovy, so you can read whatever files from your development machine that you want in your build scripts.Footsie
H
2

you could use extentions like this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    ext.kotlin_version = '1.6.10'

    ext.app_version_code = 1010

    ext.app_version_name = '1.0.1.0'

}
Hag answered 31/5, 2022 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.