Before we discuss how to setup multiple environments for an app, it’s important to understand why and when you might need multiple environments. As an app developer, you would like to separate the things like; Base URL, configuration IDs and paid/unpaid functionality for an app that you can achieve the same by multiple environments.
During development, you might face challenges like:
- How the mobile app work with multiple environments?
- How to switch between them?
These challenges can be overcome by product variants and Build Types
Android Build Types :-
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. The release builds are meant to be uploaded to the play store.Following image shows the default build types.
In the build.gradle by default, only the release build type block is written:
We can add properties on the other build types too. Let’s add new build types and more properties to the buildConfig.
applicationId suffix appends the string to the applicationId of the application.
versionName does the same on the version name present in the defaultConfig.
Now we have 3 build variants:
Android Product Flavors :-
Android Product Flavors are used to create different app versions.App versions can be free or paid.
They can use different environments or APIs. to successfully create the flavors we need to set flavor dimensions. Now syncing the gradle would give you the following product flavors:
Now in our project, the main folder consists of the common logic across all app versions. To write flavor specific code, create a folder with the same name as the flavor.
- The java classes with the same name in the flavor folders won’t override the main folder.
- The res folder in the main should only have those directories that are common to all flavors.
Project Structure
Hope this speed up a little on your initial project setup. Cheers!
I hope you appreciate this post and it’s helpful for you.