Keep flavors configs in separate .gradle files
Asked Answered
P

2

7

Being short - is there a way to keep flavor configs in separate .gradle files?

And for more details - I'd like to have per flavor .gradle files (like flavorGermany.gradle, flavorUkraine.gradle, flavorItaly.gradle etc.) that will be included with 'apply from:' directive into main gradle. Each flavor .gradle will contain signing and build configs.

Peatroy answered 14/9, 2016 at 15:42 Comment(0)
C
14

Sure you can. Just place those files in app folder and then in your app folders build.gradle import those.

Your flavorGermany.gradle would look like this:

android {
    productFlavors {
        flavorGermany {}
    }
}

And then you import those in your build.gradle (app):

apply plugin: 'com.android.application'
apply from: 'flavorGermany.gradle'
apply from: 'flavorUkraine.gradle'
Cognation answered 14/9, 2016 at 16:9 Comment(2)
Very nice, it works. I also added more info on further steps in a separate answer.Peatroy
aren't with such solution all flavourRandomCountry gradle files included in all flavours?Pase
P
1

While configuring flavor signing in a separate .gradle file I got a sticky error telling:

flavorGermany.gradle: 1: unable to resolve class com.android.ide.common.signing.KeystoreHelper

In flavorGermany.gradle the KeystoreHelper is used this way:

android {
    signingConfigs {
        germany {
            storeFile = file(KeystoreHelper.defaultDebugKeystoreLocation());
            storePassword = "some_password";
            keyAlias = "some_key";
            keyPassword = "some_other_key";
        }
    }
    // other configs ...
}

To fix this I had to add this before the 'android' definition:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        // Android gradle plugin
        classpath 'com.android.tools.build:gradle:2.1.3'
    }
}

With this change the import error has gone and the Android Studio was able to recognize the additional flavor described in a separate .gradle file.

Peatroy answered 15/9, 2016 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.