Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a property from gradle.properties inside android activity?

How to use a property from gradle.properties inside android activity? Whenever I build the code it throws error. Is there a particular way that the properties can be accessed inside the activities?

like image 258
mishigun Avatar asked Jan 21 '16 13:01

mishigun


2 Answers

You can't access the gradle.properties from your Activity.

The gradle.properties is used while you are building the app, it doesn't exist in runtime or inside your apk.

However you can set some values inside the BuildConfig class from your build.gradle script (reading from the gradle.properties for example).

Just use somenthing like:

buildConfigField "boolean", "MY_FLAG", "true"
buildConfigField "String" , "MY_KEY" ,  "\"XXXXX-XXXXX-XXX\""
like image 57
Gabriele Mariotti Avatar answered Nov 09 '22 00:11

Gabriele Mariotti


in gradle.properties:

SIMPLE_STRING=ABC

in build.gradle:

android {
    buildTypes {
        debug {
            buildConfigField 'boolean', 'PREPROD', 'true'
            buildConfigField "String" , "MY_KEY" ,  SIMPLE_STRING
        }
     }
}
like image 26
Dominik Suszczewicz Avatar answered Nov 08 '22 22:11

Dominik Suszczewicz