Versions of Constants according to Buildtype

Asked

Viewed 184 times

0

I have some constants which I would like to vary according to how BuildType of Gradle. Among them are the URL’s of the API’s that I use in my app, which can be production, homologation and development. For example:

https://api.empresa.com.br and http://dev.api.empresa.com.br

I’ve read the documentation, and have tried solutions from here and from here. But none worked.

What is the correct `build.Gradle configuration so I don’t have to manually change my constants according to the environment.

2 answers

1

Both the instructions in documentation when the solutions contained in the posts I mentioned work perfectly.

The problem is that Android Studio seems to have a bug, as it recognizes only the Buildtype classes that are active giving the impression that the other packages are in error.

Use of window is required Build Variant and select the version you want to work on at the moment so that they recognize the classes and packages.

PS: I discovered this "bug" thanks to link indicated by @Netomarin, but there is no need to use Flavors for such a simple task.

0

You will need to create different files for each set of constant values you want to use. And then, create tasks that will select the file at the right time.

This is done by setting up productFlavors, and then you indicate which for production and testing, for example.
Then, in the Build Variants tab of Android Studio to switch between settings.

In that blog post you will find an explanation including images.

  • 1

    Great Neto! Thanks for the link, helped me to identify the problem, but the author was very mistaken in the example of use of Build Variants. According to official documentation, only Buildtype is enough to solve the problem of environments, the Flavors is to create alternative versions of the app as Paid and Free, where each of them will have the version dev and Prod. It doesn’t make any sense to have Flavors Productiondebug or Stagingrelease

  • Really Flavors is a little bit more than the task if it’s just for some environment variables. I’m glad you found the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.