Android Studio: Switching a URL depending on buildtype? (used for testing in debug /release)

后端 未结 2 1829
一向
一向 2021-01-31 18:01

I have been reading something about variants and buildtypes and I don\'t know if I am understanding it right but I would like to store a URL for locahost (testing) and one for p

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 18:27

    You can use the BuildConfig for supplying different URLs for each BuildType

    buildTypes {
        debug {
            buildConfigField "String", "SERVER_URL", '"http://someurl/"'
        }
        release{
            buildConfigField "String", "SERVER_URL", '"http://someotherurl/"'
        }
    }   
    

    The BuildConfig will be autogenerated each time you sync your project with the gradle file. In your code, you can access the URL like this:

    BuildConfig.SERVER_URL
    

    If you don't want to commit these URLs, you can store them in your gradle.properties just like your password and such and reference them in the build.gradle.

    buildConfigField "String", "SERVER_URL", serverurl.debug
    

提交回复
热议问题