0

Hey I am trying to statically define String values that change according to the configuration I am running. So if I run a test configuration, it uses the test API url, but if I run a regular build, it statically sets the real API URL.

I am using two strings files right now, one in the main folder and one in the androidTest folder in Android Studio. This works well for getting different Strings per configuration, but I'de like to do it statically rather than dealing with Resource fetches.

Is this possible?

I have seen this answer for ANT, but I am not sure how to do it with Gradle.

Community
  • 1
  • 1
grossmae
  • 331
  • 5
  • 16

1 Answers1

3

You can generate gradle constants like this:

build.gradle

android {
    buildTypes {
        debug {
            buildConfigField "String", "FOO", "\"foo\""
        }

        release {
            buildConfigField "String", "FOO", "\"bar\""
        }
    }
}

And access them in your code through BuildConfig.FOO

Note you may need to clean and/or restart your IDE for the to come in to effect.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • This looks great! Are "debug" and "release" build types analogous to "main" and "testAndroid" build configurations in Android Studio? – grossmae Sep 03 '14 at 16:28
  • @grossmae I'm using AS too. Debug and release are the default builds here. – Simas Sep 03 '14 at 16:49
  • I'm sorry if I'm being dense, but "release" and "debug" to me are determined if I click the green run button (ie ctrl + r) or the bug button (ie ctrl + d). I want to have values change depending on the configuration which I choose with the drop down menu next to the run buttons. Can I do that with the BuildTypes in Gradle? – grossmae Sep 03 '14 at 17:28
  • @grossmae Those configurations simply start your application from different activities. [Build Variants](http://developer.android.com/sdk/installing/studio-build.html#workBuildVariants) were build for the usage you are trying to achieve with run configurations. – Simas Sep 03 '14 at 17:43