21

I am developing an Android application that interacts with server via REST APIs. Obviously I need to use different URL for development and release builds. Commenting and un-commenting code is very tedious and error pron.

Which is the best way to handle this situation? Using different build types in gradle file is one which could automate the process, but I am not sure if this is the right way to go.

There is also a possibility of increase in number of build types viz. test, internal-release etc.

mmBs
  • 8,421
  • 6
  • 38
  • 46
Abdullah
  • 7,143
  • 6
  • 25
  • 41
  • 1
    [Refer: Managing multiple environments - flavours, build types, signing configs](http://stackoverflow.com/questions/22995057/how-do-you-manage-multiple-environments-while-developing-android-apps/39116533#39116533) – shijin May 04 '17 at 10:33

4 Answers4

79

If you are using Android Studio, use buildConfigField to add custom fields to your BuildConfig class.

buildTypes {
        debug {
          buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"'
        }

        release {
          buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
        }

        mezzanine.initWith(buildTypes.release)

        mezzanine {
            buildConfigField "String", "SERVER_URL", '"http://stage.this-is-so-fake.com"'
        }
    }

Here, I have three build types: the standard debug and release, plus a custom mezzanine one. Each defines a SERVER_URL field on BuildConfig.

Then, in Java code, you just refer to BuildConfig.SERVER_URL. That field will have a value based on what build type you used to build that particular edition of the app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks its nice. in addition i want to tell that after add SERVER_URL you have to syn gradle to update app's BuildConfig file. – Khizar Hayat Dec 28 '16 at 07:23
  • One more thing if we want to use mezzanine for staging server then we have to use mezzanine.initWith(buildTypes.mezzanine) instead of mezzanine.initWith(buildTypes.release) – Khizar Hayat Dec 28 '16 at 08:06
  • @KhizarHayat: `mezzanine.initWith(buildTypes.mezzanine)` would be a no-op, with `mezzanine` being unchanged. – CommonsWare Dec 28 '16 at 12:17
  • In my case it will not work to use staging url if i use buildTypes.release – Khizar Hayat Dec 29 '16 at 04:54
  • @CommonsWare What if you want to connect to your production server with a debug binary? What will change in the configuration in the above answer? – Pawan Jan 27 '17 at 08:22
  • @Pawan: You would not be able to use the approach outlined in the answer. – CommonsWare Jan 27 '17 at 12:50
4

It can be managed by using ProductFlavours in app build.gradle. ProductFlavours will manage different URL ie. development and release.

Please have a look it on medium. It involves detailed explanation.

Juhi Matta
  • 481
  • 1
  • 5
  • 15
0

I had a similar issue and I solved it using

if (BuildConfig.DEBUG) { } 

You will need to import

import com.commandsoftware.androidbookingapp.BuildConfig;
apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • Good point, but I think this will not scale if there are more configurations than debug and release for example test. I will edit my question to include this point. – Abdullah Jun 10 '15 at 13:43
0

I had a similar problem with writing to logcat. I wanted to write all the messages if the app was signed with the debug key, otherwise write almost none of them. I solved the problem with this line of code:

boolean showAllMessages = ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);

then using that boolean in my log writer. You should be able to do something similar when you initialize the URIs.

I am using Eclipse. I can't say with certainty that this will work in other IDE environments. This answer implies that it might be an Eclipse-only feature

Community
  • 1
  • 1
cbhaley
  • 41
  • 2