CaptRespect
CaptRespect

Reputation: 2055

How can I get the current flavor while looping dependencies in android gradle?

We've got a huge project with 100s of flavors. To help keep our Google play services in line I'd like to define a default version and then override it in flavors as needed.

So to for all our various dependencies to use the same version I've got this:

 configurations.all {
    resolutionStrategy {
        eachDependency { DependencyResolveDetails details ->
            //specifying a fixed version for all libraries with 'com.google.android.gms' group
            if (details.requested.group == 'com.google.android.gms') {
                details.useVersion '8.4.0'
            }
        }
    }
}

I'd like to define:

defaultConfig {
    ext.googlePlayServicesVersion '9.6.0'
}

then over ride it in my flavor:

myFlavor {
    // XXX plugin requires this older version
    ext.googlePlayServicesVersion '8.4.0'
}

Then use the variable version in the configuration.all loop.

Is there anyway to access that variable in the that loop?

Upvotes: 2

Views: 722

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200130

You can do this directly in your dependencies block:

dependencies {
  compile "com.google.android.gms:play-services-ads:$ext.googlePlayServicesVersion"
  myFlavorCompile "com.google.android.gms:play-services-ads:$ext.googlePlayServicesVersionOld"
}

Any project flavor dependencies set with flavorNameCompile will override the default compile dependency for the same dependency and allow you to override the version for specific flavors.

(I use play-services-ads only as an example)

Upvotes: 3

Related Questions