I want to add custom fields in local.properties
like
debug.username=test123
debug.password=abcd1234
can add .properties
files in assets folder and read that easily.
Resources resources = context.getResources();
AssetManager assetManager = resources.getAssets();
Properties properties = null;
InputStream inputStream = assetManager.open(fileName);
properties = new Properties();
properties.load(inputStream);
But don't want to do this.
As i want every team member of our to use local.properties
to specify there custom attribute. which is not the part of version control system.
So how to read local.properties
placed in the root folder of gradle based android project in java files at runtime?
The local. properties file goes in the project's root level, in the same folder as the gradlew , gradlew. bat , settings. gradle and other files.
GRADLE_USER_HOME. Specifies the Gradle user home directory (which defaults to $USER_HOME/. gradle if not set). JAVA_HOME. Specifies the JDK installation directory to use for the client VM.
I know that this is an old question but I recently encountered the same thing and I thought I'd share my solution:
BuildConfig
constant.BuildConfig
constant in your Java code.local.properties
username=myUsername
build.gradle:
def getUsername() {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
return properties.getProperty("username");
}
android {
defaultConfig {
buildConfigField "String", "USERNAME", "\""+getUsername()+"\""
}
}
Example Java class:
package your.package.name;
class MyClass {
String getUsername() {
return BuildConfig.USERNAME; // Will return "myUsername"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With