Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle. How to combine Flavors with buildTypes

I'm working on a white brand app.

We create a different flavor per client and each client has Debug and Production APIs, so I'm trying to set them up in the Gradle.

How should I do that?

Here is what I've tried:

buildTypes {
    debug {
        // some configurations
    }
    release {
        // some configurations
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
    }
}

EDIT: I would like to be able to define a different BASE_URL per each Flavor and Buildtype.

Flavor company1, BuildType debug

https://app.company1.com/devApi/

Flavor company1, BuildType release

https://app.company1.com/prodApi/

Flavor company2, BuildType debug

https://dev.company2.com/api/

Flavor company2, BuildType release

https://prod.company2.com/api/
like image 644
kike Avatar asked Jan 15 '19 10:01

kike


People also ask

What are buildTypes and product Flavours in Gradle and what can you use them for?

Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.

What is BuildConfig flavor?

BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig. FLAVOR.

What is a Buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


3 Answers

For my specific problem, where the URLs differ a lot one to another, I couldn't make it work with Flavours and BuildTypes.

I was able to define the debug/production URLs by using specific strings.xml for each build variant (which is each combination of flavour and build type):

These are the folder structures to do so:

src/flavour1Debug/res/values/strings.xml 
src/flavour1Release/res/values/strings.xml 

and

src/flavour2Debug/res/values/strings.xml 
src/flavour2Release/res/values/strings.xml 

EXTRA: This can also be used to host different google-services.json files

like image 69
kike Avatar answered Oct 12 '22 10:10

kike


Try something like this:

buildTypes {
    debug {
        buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
    }
    release {
        buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company1/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company2/\"")
    }
}

And use it like:

String BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH

like image 34
Housefly Avatar answered Oct 12 '22 09:10

Housefly


Here is the working example

// Specifies one flavor dimension.

flavorDimensions "version"

productFlavors {
dev {
    dimension "version"
    applicationId "com.example.lk"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.dev.com/api/v1/\"")
}

prod {
    dimension "version"
    applicationId "com.example.in"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.prod.com/api/v1/\"")

}
staging {
    dimension "version"
    applicationId "com.example.other"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.staging.com/api/v1/\"")

}}




buildTypes {
    release {
      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

you can access it like this

String baseURL =BuildConfig.BASE_URL_PATH;
like image 38
Tufan Avatar answered Oct 12 '22 10:10

Tufan