Search code examples
androidandroid-studiobuild.gradlesharedpreferences

how to add value to build.gradle?


Is there a way to do with shared preference? or,

Add a database column value to build.gradle?

buildConfigField "String", "secretKey", "\"here should be the value i want\""

Solution

  • In the root directory create a file, for ex: apikey.properties

    SECRET_KEY=YOUR_SECRET_KEY
    

    In your build.gradle to read the apikey.properties

    def apikeyPropertiesFile = rootProject.file("apikey.properties")
    def apikeyProperties = new Properties()
    apikeyProperties.load(new FileInputStream(apikeyPropertiesFile))
    
    android {
      defaultConfig { 
        buildConfigField("String", "SECRET_KEY", apikeyProperties['SECRET_KEY'])
      }
    }
    

    You can access the field in your with the BuildConfig object.

    String secretKey = BuildConfig.SECRET_KEY;