Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Managing different server URL for development and release

Tags:

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.

like image 309
Abdullah Avatar asked Jun 10 '15 13:06

Abdullah


Video Answer


2 Answers

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.

like image 119
CommonsWare Avatar answered Oct 02 '22 13:10

CommonsWare


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.

like image 34
Juhi Matta Avatar answered Oct 02 '22 12:10

Juhi Matta