0

I have gradle modules: app and library module libApp. Each one of them has own gradle file. app.gradle:

productFlavors {
     prod {somevariable = false}
     debug {somevariable = true}
}

dependencies {
    implementation project(':libApp')
}

And libApp.gradle:

defaultConfig {
    buildConfigField "boolean", "SOME_VARIABLE_FROM_APP_MODULE", somevariable
}

How to force gradle to compile project libApp in app module with somevariable dependent on product flavor ?

taulus
  • 69
  • 8

2 Answers2

1

You can do this with flavors.

In the library module's build.gradle, define a flavor dimension that corresponds to the variable's value:

android {
    /* ... */

    flavorDimensions "someVar"

    productFlavors {
        someVarTrue {
            dimension "someVar"
            buildConfigField "boolean", "SOME_VARIABLE_FROM_APP_MODULE", 'true'
        }

        someVarFalse {
            dimension "someVar"
            buildConfigField "boolean", "SOME_VARIABLE_FROM_APP_MODULE", 'false'
        }
    }
}

In the app module's build.gradle, define which app flavor should use which library flavor:

android {
    /* ... */

    flavorDimensions "environment"

    productFlavors {
        prod {
            dimension "environment"
        }

        dev {
            dimension "environment"
        } // note: naming a flavor "debug" might create a conflict with the default "debug" configuration
    }
}

dependencies {
    /* ... */
    prodImplementation project(path: ':libApp', configuration: 'someVarTrue')
    devImplementation project(path: ':libApp', configuration: 'someVarFalse')
}

You might want to see this article for some more details.

SpaceBison
  • 2,525
  • 1
  • 21
  • 38
  • This will not work, because you have mistaken configuration with flavor name. someVarTrue must be declared in `configurations {}` block in library, but inside it we can't declare buildConfigFields. Are you sure that code above is correct ? – taulus Sep 25 '18 at 11:42
  • @taulus I've updated my answer, it should now work. Sorry, I was posting this from memory on a smartphone and messed up the syntax. ;) – SpaceBison Sep 26 '18 at 10:35
0

You can create ext block in your project level build.gradle which encapsulates custom properties and makes them available to all modules in the project.

ext {
    somevariable = true
}

While accessing it, you can simply

defaultConfig {
    buildConfigField "boolean", "SOME_VARIABLE_FROM_APP_MODULE", ext.somevariable
}
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • It will work BUT I want to change value of somevariable dependent on currently builded flavour. – taulus Sep 24 '18 at 09:40
  • 1
    You can set the variable in each flavour as shown [here](https://stackoverflow.com/questions/17697154/gradle-android-plugin-add-custom-flavor-attribute) – Jan Stoltman Sep 24 '18 at 11:13
  • @taulus just update the value per flavour and it should do the trick – Sagar Sep 24 '18 at 12:33
  • @Sagar but it is not :/ It is behave like libApp is compiled once with somevariable value like in debug flavor - even if I am building prod. – taulus Sep 24 '18 at 13:21