1

I am reading config fields from an environment.properties file that I would like to reuse in my Android application. I have tried various ways to get values from gradle's build without success (Gradle 2.7):

buildTypes {

    debug {

        buildConfigField "String", "SERVER_SCHEME", getBuildConfigField('SERVER_SCHEME')
        buildConfigField "String", "SERVER_BASE_URL", getBuildConfigField('SERVER_BASE_URL')
        buildConfigField "int", "SERVER_PORT", getBuildConfigField('SERVER_PORT')
        buildConfigField "String", "SERVER_CONTEXT",  getBuildConfigField('SERVER_CONTEXT')

    }
}

where getBuildConfigFields(key) is a function defined in my config.gradlereading a file and returning the value corresponding to the specified key. I have tried :

  • buildConfigField "TYPE" "KEY" "VALUE",
  • buildConfigField("TYPE" "KEY" "VALUE")
  • resValue "TYPE" "KEY" "VALUE"

But none of the are working with gradle errors like :

Error:(71, 0) Gradle DSL method not found: 'buildConfigField()' Possible causes:
The project 'app' may be using a version of Gradle that does not contain the method. Gradle settings
The build file may be missing a Gradle plugin. Apply Gradle plugin

I am using gradle 2.7 to build my android app and found these ways to set BuildConfigFields in different answers like this one but they seem to be outdated for newer versions. What functions can I use to get values from gradle in my application ?

Thank you !

Community
  • 1
  • 1
Jerec TheSith
  • 1,932
  • 4
  • 32
  • 41
  • 1
    the question linked is still valid. – Gabriele Mariotti Sep 18 '15 at 15:06
  • Agreed. I just pasted the code from the "Plugin version greater than 0.7.x" section of the accepted answer into an Android project, and it worked just fine, with both version 1.2.3 and 1.3.0 of the `com.android.tools.build:gradle` plugin. – CommonsWare Sep 18 '15 at 15:35
  • I use external gradle with 2.7 version as I need to deploy to a Jenkins server on which only gradle 2.7 is available. I think there is still a way to do the same with newer versions of gradle but I could not find how – Jerec TheSith Sep 18 '15 at 15:39

0 Answers0