bigdestroyer bigdestroyer - 6 months ago 49
Android Question

Gradle: Read properties from external file

I have created a file
in my Android project that contains this data:


And now I want to use this properties in
file this way:

android {

defaultConfig {

But, obviously, it doesn't find the properties. How can I add this file to classpath so that I can use its properties?


To read values from a generic properties file you can use something like this:

def Properties props = new Properties()
def propFile = file('../')   //pay attention to the path
def versionCode;
if (propFile.canRead()){
    props.load(new FileInputStream(propFile))

    if (props!=null && props.containsKey('version.code') && props.containsKey('')) {

        versionCode = props['version.code']


Using the standard you can do:


Then in your build.gradle you can use:

versionName project.VERSION_NAME

Another way is to set these values in a .gradle file (for example in your top-level file)

ext {
  myVersionCode =  ...
  myVersionName =  ...

Then in your module/build.gradle file you can do:

versioneCode   rootProject.ext.myVersionCode