Android: Managing different server URL for development and release

前端 未结 4 1398
暗喜
暗喜 2020-12-04 22:32

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-

4条回答
  •  无人及你
    2020-12-04 23:11

    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.

提交回复
热议问题