1

Is it possible to change the build variant Programmatically run time in Android Studio?

I have different server environments (Release, Debug, Production, Dev, Dev1, Dev2, QA, etc.). The code itself relies on some strings, SDK's initialization, and server addresses that are set when I pick my build variant. So when I pick debug for an instance, the string uses out debug server address and SDK's. The same for release. But what if I want to be able to pick the environment during run-time?

Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
  • have you tried using productFlavors in gradle files ?. Using flavors you can choose the type of app you want to run at build time. Along with that you can add specific string that will run only with that vairent – Umair Oct 17 '19 at 10:21

4 Answers4

2

Build variants are a build time process, meaning if you want to change the build variant of your apk then you will have to rebuild your project. I see you require something which is equivalent to setting.bundles which is the concept of iOS. Android doesn't have such concepts although we can implement it by using Preference or using managed-configuration in all these implementations you will have to clear app cache. I would recommend to create a generic build variant with all specified URLs and handle the configurations at the run time

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
1

As you can read in the name "build variant" it is used while building and not while running. You cannot change the build variant at runtime. You have to come up with another aproach like a hidden debug view to change your parameters at runtime.

Traendy
  • 1,423
  • 15
  • 17
1

No,You can only change it run time.

Alternatively, You can create a singleton for variant class and create a list of variants.Select the variant from a view and store it in cache and when app runs you can get that variant.Restart your application

1

Create a custom POJO class for your Variants and try to save the desired Variant in shared preferences and then simple restart your android application. Load value in the Singelton Variant object from the cache and do whatever you want to do with your app.