1

I have a gradle script which needs to be imported as a dependency like this:

compile project(':subproject', { ext.app = 'myApp'; ext.serverUrl = 'https://example.com'; ext.system = 'LIVE'})

This is working fine, if I set the variables directly in the dependency statement.

As I have a different system for debug and for release I tried to move these properties to the buildTypes:

...
    debug {
        debuggable true
        serverUrl = 'https://example.com'
        system = 'TEST'
    }
    prerelease {
        debuggable true
        serverUrl = 'https://example.com'
        system = 'STAGING'
    }
    release {
        serverUrl = 'https://example.com'
        system = 'LIVE'
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
...


dependencies {
    compile project(':subproject', { ext.app = appName; ext.serverUrl = serverUrl; ext.system = system })
}

So, when I build assembleDebug it should use TEST and with assemblePrerelease it should use STAGING. However it is always using the release build type variables to compile the dependency.

The library already contains publishNonDefault true

What's wrong with this gradle script?

Denis Loh
  • 2,194
  • 2
  • 24
  • 38
  • It seems to me, that gradle always puts the last values into the extension. How do I get the definitions from the other buildTypes? Maybe with a loop? – Denis Loh Oct 14 '16 at 14:59

1 Answers1

0

I answer my own question.

Gradle does not parse the gradle file as expected. The closures will be evaluated in a single step when the tasks are being generated. This means that there is no concept of having a variable which will only be used when the specific task is being executed. The file is being read once which causes the variables to override the previous values of another flavor or buildType. This is also the reason why changing the order of the flavors results in different values.

The correct solution would be to define a custom task which is run right after the file has been generated. That task will generate a set of tasks for each variant of the app which themselves contain the configuration what to do.

This SO article helped me alot: How to get current buildType in Android Gradle configuration

Community
  • 1
  • 1
Denis Loh
  • 2,194
  • 2
  • 24
  • 38