Sometimes you have to deliver application for multiple environments, demo/full features and other configurations. Configuring this manually before build is not a good idea and here appears problem with Continuous Delivery and Continuous Deployment.
Let’s imagine that we have an application which works with some API endpoint and we should also delivered in Demo/Full mode.
The Android build system compiles app resources and source code and packages them into APKs that you can test, deploy, sign, and distribute. Android Studio uses Gradle as a build system.
Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven of declaring the project configuration. Gradle uses a directed acyclic graph (“DAG”) to determine the order in which tasks can be run.
Gradle Project Structure
Although we won’t go deep into this topic, we will need some basic knowledge about project structure.
- MyProject — Project
- app — Android application module
- androidTest — build type source set. Resources declared here are available only in Android Test Mode
- debug — build type source set. Resources declared here are available only in Debug mode
- main — Default source set. Every flavor/build type will contain everything from this set.
Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.
Although you can find some documentation of this in references, I will make a brief information of flavors definition and configuration.
Gradle build configuration file. You can see such one in project and inside modules. Also, here you can also find settings.gradle which defines what modules to include. In my case, MyProject has content :
This means that our project includes app module.
Let’s create a sample application which will be compound of 2 versions, Full and Demo.
Add following lines of code in builld.gradle of App Module. These Flavors are added in default dimension(will cover this topic later).
After adding this, in Android Studio Gradle Tasks you can find under :app section following tasks.
You can use terminal to check build tasks, to run
$ ./gradlew tasks // using gradle wrapper
$ gradle tasks // using gradle
and get following output under Build Tasks
Configuration is done. What’s next ?
One we had made have configured successfully, it’s time to define something specifics for this flavors. Into Android Studio project explorer, switch to “Project” mode and create following hierarchy
This means we had defined some specific strings for Demo mode and some for Full Mode.
Content of strings.xml in demo
<string name="app_name">Demo App</string>
Content of strings.xml in full
<string name="app_name">Full App</string>
Great, now you can switch to “Android” mode in Project Explorer. And now open “Build variants” panel in AS and you should have following variants:
You see here combination of flavors with build types, that’s OK. Select one of them and try to run application in your device. For Demo variant you’ll have application with name Demo App and for Full you’ll have Full App.
Try to install both flavors in your phone, you’ll see that it’s not overriden, but we have 2 different apps called “Demo App” and “Full App”. That’s because we had specified applicationId property for both variants and they had different ID-s.
In same way you can define flavor specific resources,activities, manifest and … everything you want. Note that you Gradle will Flavor Specifics with defaults. For checking this, try to build an apk and analyze it with Android Studio.
Furthermore, you can specify resources for Demo variant and Debug Build Type. For this, you should create directory in
In previous step we had defined 2 flavors “demo”, “full” but sometimes you need more flexibility. What if we will need more flexibility and to deliver following applications :
- Demo App pointing to Live Environment
- Full App pointing to Test Environment
- or other configuration.
Here we can observe that we need another layer of flavors. Here comes in help flavor dimensions. You can think this in terms of “category”. Let’s update our app/gradle.config to be
flavorDimensions "type", "environment"
We defined added 2 dimensions “type” and “environment”.
Now, you’ll have following variants(also you can check gradle tasks, you’ll see same result). You can find here that Gradle mixed our dimensions with build types and we obtained following combinations
If this still doesn’t tell you nothing, then you can think that Gradle gives you following combinations
Android plugin allows you to combine different flavors, but you still need to take care of naming conventions, here I denoted some of them. You can have more than 2 flavors, you can use in your own purpose.
We all know how great is Gradle and how much flexibility it gives to us. Additionally Android Plugin offers us Flavors for creating combinations of configurations. This can be useful when you have to deliver multiple types of application like in our example. This also gives you possibility automate everything for Continuous Delivery and Continuous Deployment.