Variant is a way to create multiple .apks with different set of configurations within maintaining a single project. If you are a new to build variant, you can checkout my introduction tutorial below
What can I learn in this tutorial?
You will be able to do some basic configurations to up and running with variants.
- Configure general settings for AndroidManifest.xml
- Variables (String, Boolean, Int…)
- Gradle settings
- App variants (Free, Paid or Pro versions etc…)
Let’s get into the practical with Build Variants
We will be discussing some basic configurations that can be done very easily. If you had a look into my beginner tutorial, you will see a basic setup of variant
Let’s configure some more stuff here. Add the following code snippet inside local and production
manifestPlaceholders = [appLabel: "Build Variant Demo - Dev"]
You can now use appLabel
inside <application />
tag in AndroidManifest.xml to display app name. When you switch the variant, your app name will be replaced with the associated variant.
<application
...
android:label="${appLabel}" />
Just press Sync Now
or Sync Project with Gradle Files
and build apk to see changes.
Let’s add some more stuff here…
resValue "string", "google_api_client_key", "YOUR GOOGLE API CLIENT
KEY FOR DEVELOPMENT"//configuration Datatype Key value
buildConfigField 'String', "BASE_URL", '"YOUR BASE URL FOR DEVELOPMENT"'buildConfigField 'boolean', 'IS_DEV_ENABLED', 'false'
resValue
will be generated and can be accessible using R.string.google_api_client_key
and buildConfigField
will be generated and can be accessible using BuildConfig.BASE_URL
Press Double Shift in Android Studio and type BuildConfig open that file, you will see the generated variable
// Fields from product flavor: dev
public static final String BASE_URL = “YOUR BASE URL FOR DEVELOPMENT”;
Sometimes when the project is in under development and you want to maintain the app versions for QA iterations etc… or you want to have different application id to distinguish the app, you can add the following stuff to your desired variant. For instance
dev {
...
versionCode 2000 + android.defaultConfig.versionCode
versionNameSuffix "-dev"
applicationIdSuffix ".dev"
}
Cool! Now come to the last guideline for this tutorial i.e. adding variants like staging or qa or free or paid or pro or anything with your requirements…
We have already looked into a basic setup of how to add variants, let’s add more
flavorDimensions "env", "version"
productFlavors {
free {
dimension "version"
... //Add your variant configurations
}
paid {
dimension "version"
... //Add your variant configurations
}
dev {
dimension "env"
... //Add your variant configurations
}
production {
dimension "env"
... //Add your variant configurations
}
}
Now, look at the complete snapshot for the above configurations.
free or paid belongs to the version of app and dev or production to development environment. Just try to add these flavors and press Sync Now, then you will see the variants as below
Here is the code for variant configurations in gradle.
Congratulations! You are done.
What’s next?
Configurations and settings in package level.
Don’t forget to Clap. 😊 Happy coding!