Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read value from gradle.properties in java module?

I have a project in Android Studio with a couple of modules.

  • The app module. With apply plugin: 'com.android.application'.
  • The androidLibrary module. With apply plugin: 'com.android.library'.
  • The javaLibrary module. With apply plugin: 'java'.

I want to declare some variables in the project gradle.properties file and be able to read them from the javaLibrary module.

I have declared the properties in the following ways according this documentation...

  • mysuperhost=superhost
  • systemProp.mysuperhost=superhost
  • ORG_GRADLE_PROJECT_mysuperhost=superhost
  • org.gradle.project.mysuperhost=superhost

... and tried to read them this way with no success:

  • System.getenv("mysuperhost");
  • System.getProperty("mysuperhost");

I know how to read properties from the BuildConfig class, but this is a generateed class in the app module (with apply plugin: 'com.android.application'), so this does not work for this particular case.

like image 222
Jorge E. Hernández Avatar asked Mar 26 '26 04:03

Jorge E. Hernández


1 Answers

If you have some value inside of the your gradle.properties file like mysuperhost=superhost then write the following lines in the your build.gradle file (to grab property from gradle.properties file and add it into BuildConfig.java class):

// ...
// ...

android {
    // Just for example
    compileSdkVersion 23
    // Just for example
    buildToolsVersion "23.0.2"

    // ...
    // ...

    defaultConfig {
        // Just for example
        minSdkVersion 14
        // Just for example
        targetSdkVersion 23

        // This is the main idea
        buildConfigField('String', 'MY_SUPER_HOST', "\"${mysuperhost}\"")

        // ...
        // ...
    }

    // ...
    // ...
}

// ...
// ...

After that build your project and you are able to use your value via BuildConfig.MY_SUPER_HOST

like image 194
akirakaze Avatar answered Mar 28 '26 17:03

akirakaze