Use the local.properties field when declaring buildConfigField

I have a build.gradle file and local.properties . I want to declare a value in local.properties that is not verified in version control, for use in build.gradle .

I have buildConfigField working with:

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

Unfortunately, this causes an error:

 buildTypes { debug { buildConfigField "String", "TEST", local.properties.get("test") } } 
+6
source share
1 answer

This can be achieved as:

 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 the buildTypes block:

 buildTypes { debug { buildConfigField "String", "TEST", getProps("test") } } 
+8
source

Source: https://habr.com/ru/post/975319/


All Articles