Java – How to read android in a java file

How to read android in a java file… here is a solution to the problem.

How to read android in a java file

I want to add a custom field in, eg


You can add a .properties file in the assets folder and read it easily.

Resources resources = context.getResources();
AssetManager assetManager = resources.getAssets();
Properties properties = null;        
InputStream inputStream =;
properties = new Properties();

But don’t want to.
Because I want each of our team members to use to specify custom properties. This is not part of a version control system.

So how do you read in the root directory of the Gradle based Android project in a Java file at runtime?



know this is an old issue, but I’ve had the same thing recently and I thought I’d share my solution :

  1. Set the values in your file.
  2. Read the value in the Gradle build script and set it to the BuildConfig constant.
  3. Access the BuildConfig constants in Java code.



def getUsername() {
    Properties properties = new Properties()
    return properties.getProperty("username");

android {
    defaultConfig {
        buildConfigField "String", "USERNAME", "\""+getUsername()+"\""

Sample Java class:


class MyClass {
    String getUsername() {
        return BuildConfig.USERNAME;  Will return "myUsername"

Related Problems and Solutions