31

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 production (live site on the internet).

And I need to switch them depending on which buildtype. Is this the right way to do this ? or is there another alternative ?

Does anyone have a small example ?

Is there a way of storing this information in a file that I do not need to commit to source control ? I am using the gradle.properties file for storing some passwords that gradle uses for the signings.. This works great as this file I do not share in version control.

I am a little confused of the correct method to use and how to implement it.

Any ideas?

Martin
  • 23,844
  • 55
  • 201
  • 327
  • possible duplicate of [Android - dynamic configuration in gradle based on build type](http://stackoverflow.com/questions/18767324/android-dynamic-configuration-in-gradle-based-on-build-type) – Kuba Spatny Jan 10 '15 at 11:50

3 Answers3

101

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
tknell
  • 9,007
  • 3
  • 24
  • 28
  • Nice that works! Just out of interest why is the value wrapped in single quotes as well as double? – Martin Jan 10 '15 at 12:12
  • 6
    The value of this String is directly pasted into a Java file and as in there, a String value needs quotes, the String inside the build.gradle must also contain these. – tknell Jan 10 '15 at 12:14
  • If you don't want to store the data for the build type in plaintext inside your gradle.properties, you can also use plugins to encrypt the contents, like https://github.com/etiennestuder/gradle-credentials-plugin - other than that, I don't see any security issue in this approach – tknell Feb 21 '18 at 15:20
  • are there any chances someone may see my debug URL during reverse engineering? – Aman Verma Nov 09 '18 at 19:26
  • I have set my URL in the build file for the debug build type, is it possible to reverse engineer the build file somehow and expose my URL which is not what I want or does my URL which is defined in debug build type be exposed if I upload the release apk to playstore. – Aman Verma Nov 10 '18 at 11:03
4
   buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        buildConfigField "String", "BASE_URL", '"url1"'
        debuggable false

    }

   debug {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        buildConfigField "String", "BASE_URL", '"url2"'
        debuggable true

 }

This you need to do in the gradle and to make run two applications(release and debug) in a same phone just add

applicationIdSuffix ".debug"

in debug portion. As the package name will be different. This worked for me.

Yogesh
  • 111
  • 1
  • 9
  • 1
    I have set my URL in the build file for the debug build type, is it possible to reverse engineer the build file somehow and expose my URL which is not what I want or does my URL which is defined in debug build type be exposed if I upload the release apk to playstore. – Aman Verma Nov 10 '18 at 11:03
0

I'm sharing the process with the image because we need to pay extra attention to java's escape sequence while adding API URL in buildTypes otherwise the URL won't generate properly in BuildConfig and produce Cannot resolve ... error like the following images - enter image description here

So the proper solution is that add URL obeying escape sequencing like the following photo-

enter image description here

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42