10

I have a build.gradle and a local.properties file. I want to declare a value in local.properties, which isn't checked in to version control, to use in build.gradle.

I have the buildConfigField working with:

buildTypes {
    debug {
        buildConfigField "String", "TEST", "test"
    }
}

Unfortunately though, this causes an error:

buildTypes {
    debug {
        buildConfigField "String", "TEST", local.properties.get("test")
    }
}
theblang
  • 10,215
  • 9
  • 69
  • 120
  • 2
    You can find code to steal here: http://stackoverflow.com/questions/20562189/sign-apk-without-putting-keystore-info-in-build-gradle/20573171#20573171 – Scott Barta Sep 15 '14 at 19:02

1 Answers1

12

It can be achieved like:

def getProps(String propName) {
  def propsFile = rootProject.file('local.properties')
  if (propsFile.exists()) {
    def props = new Properties()
    props.load(new FileInputStream(propsFile))
    return props[propName]
  } else {
    return "";
  }
}

in your buildTypes block:

buildTypes {
    debug {
        buildConfigField "String", "TEST", getProps("test")
    }
}
motou
  • 726
  • 4
  • 12