Android: Managing different server URL for development and release

泄露秘密 提交于 2019-11-29 21:48:44

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.

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.

I had a similar issue and I solved it using

if (BuildConfig.DEBUG) { } 

You will need to import

import com.commandsoftware.androidbookingapp.BuildConfig;
cbhaley

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!