as per the best practices it is recommended to keep all the credentials in gradle.properties
like this:
KEYSTORE_PASSWORD=password123
KEY_PASSWORD=password789
GOOGLE_MAP_KEY=API_KEY_HERE
This file is automatically imported by Gradle, so you can use it like:
signingConfigs {
release {
try {
storeFile file("myapp.keystore")
storePassword KEYSTORE_PASSWORD
keyAlias "thekey"
keyPassword KEY_PASSWORD
}
catch (ex) {
throw new InvalidUserDataException("You should define KEYSTORE_PASSWORD and KEY_PASSWORD in gradle.properties.")
}
}
}
To use gradle.properties
inside a class you can refer this:
android {
...
defaultConfig {
...
// defining the google map key
buildConfigField "String", "GOOGLE_MAP_KEY", MAP_KEY
}
}
This will be generated in <package_name>.BuildConfig.java
and would contain these fields:
public class BuildConfig {
// ... other generated fields ...
public static final String GOOGLE_MAP_KEY = "API_KEY";
}
You can now directly use GOOGLE_MAP_KEY by calling BuildConfig.GOOGLE_MAP_KEY
in any class.
In order to keep all your credentials safe do not commit gradle.properties in your version control repositories like Github etc.