This topic assumes you have prior knowledge of building Android applications using Android Studio and you want to know how we can configure the apk for different flavors.
Suppose if we need to build apk with slight variations in the configuration, for example, in our project we have INDIA, UK and USA flavors (different server URLs for each country) and we need to hard code the URL for each flavors, we need to have build configuration (to avoid changing the code each time we build apk).
To achieve this, we need to create an entry in build.gradle file of android application.
Step 1. Open module level build.gradle file (Highlighted in the image):

Step 2. Add an entry of "productFlavors{}" block inside "android{}" block:

Step 3. Now add entries "INDIA", "UK" etc in productFlavors{} and add flavor specific configurations:

This will create build variant entry.
Step 4. Select a particular entry and build (e.g INDIA flavor):

BuildConfig.java file will be updated with the entries made in productFlavor when we build the variant.

Step 5. You can use the build config file entry in your java code:

That's it, we are ready to go. We can also add resource folders which will compile based on build config. For example, if you need different images or strings for each flavor (e.g. INDIA and UK), we can add that as well. I will cover this as a different topic.
If you have any questions and suggestions, feel free to share it here.
- Vinayak
Suppose if we need to build apk with slight variations in the configuration, for example, in our project we have INDIA, UK and USA flavors (different server URLs for each country) and we need to hard code the URL for each flavors, we need to have build configuration (to avoid changing the code each time we build apk).
To achieve this, we need to create an entry in build.gradle file of android application.
Step 1. Open module level build.gradle file (Highlighted in the image):
Step 2. Add an entry of "productFlavors{}" block inside "android{}" block:
This will create build variant entry.
Step 4. Select a particular entry and build (e.g INDIA flavor):

BuildConfig.java file will be updated with the entries made in productFlavor when we build the variant.
Step 5. You can use the build config file entry in your java code:
That's it, we are ready to go. We can also add resource folders which will compile based on build config. For example, if you need different images or strings for each flavor (e.g. INDIA and UK), we can add that as well. I will cover this as a different topic.
If you have any questions and suggestions, feel free to share it here.
- Vinayak
Comments
Post a Comment