M
M
mitaichik2016-10-24 13:26:13
Android
mitaichik, 2016-10-24 13:26:13

How to configure application parameters depending on build type?

Hello!
There are several application parameters that I would like to bind to the build'a type. For example, for a debug build, the API url must point to a local virtual machine and Crash Reporting is disabled, for a release build, the url points to a production server and error reporting is enabled.
Ideally, I would also like to configure classes (for example, in debug, instead of AnalyticTracker, use AnalyticTrackerStub).
Now I do all this manually, before the release, but it's not cool.
In backend frameworks, all this is done through configs and configuring the DI container, but I did not find something similar in Android (I have not figured out the dagger yet).
How to do this? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Gapchenko, 2016-10-25
@mitaichik

Hello.
Here, the so-called. build config fields. Describe it in build.gradle file and then use it as BuildConfig.API_URL. An example is here .
Just initialize your crash reporting with an if like this:

if (!BuildConfig.DEBUG) {
    // инициализируем
}

For different build types, you can set your own sets of sources. At the same level as the main source set, define a couple more sets - debug and release. Inside them you place your AnalyticTracker: debug/java/com/myapp/utils/AnalyticTracker.javaand release/java/com/myapp/utils/AnalyticTracker.java. Depending on which build variant you have currently selected (debug or release), Android Studio will use either the first or second version of the AnalyticTracker class. Yes, AnalyticTracker.java will need to be removed from the main set of sources, otherwise Android Studio will not be able to select the desired class. More detailed documentation here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question