10

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?

Boken
  • 4,825
  • 10
  • 32
  • 42
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72

3 Answers3

17

I know that this is an old question but I recently encountered the same thing and I thought I'd share my solution:

  1. Set the value in your local.properties file.
  2. Read the value in your Gradle build script and set it to a BuildConfig constant.
  3. Access the 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"
    }
}
Jakub
  • 603
  • 7
  • 18
  • @Kat can you be more specific? You should be able to move the `buildConfigField` declaration from `defaultConfig` to one of your `buildTypes` or `productFlavors`. – Jakub Nov 27 '17 at 11:49
2
Properties properties = new Properties();
InputStream inputStream = 
this.getClass().getClassLoader().getResourceAsStream("fileName.properties");
properties.load(inputStream);
Username=properties.getProperty("username");      
password=properties.getProperty("password");

More detailed answer is here http://pillsfromtheweb.blogspot.in/2014/09/properties-file-in-android.html

and for asset use this code

        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open("fileName.properties");
            properties.load(inputStream);
            String Username = properties.getProperty("username");
            String password = properties.getProperty("password");
        }
        catch (IOException e){
            Log.e("message: ",e.toString);
        }
  • 2
    Didnt work inputstream comes as null. Did you tried it yourself? – Rohit Sharma Aug 20 '15 at 13:11
  • Its because your getResourceAsStream is returning null, check this http://stackoverflow.com/questions/18053059/getresourceasstream-is-returning-null-properties-file-is-not-loading –  Aug 21 '15 at 04:28
  • they are saying to put file under some directory. As i specified in the question i would like to avoid that and use local.properties of root folder. – Rohit Sharma Aug 21 '15 at 06:44
  • For this to work i need to put the file in assets folder of root directory . But i needed to read file local.properties from root folder. I mentioned that in the question – Rohit Sharma Aug 21 '15 at 10:53
  • InputStream input = new FileInputStream("/path/of/file/fileName.properties"); put the path of your properties file inside inverted comma's, –  Aug 21 '15 at 11:55
  • i can read asset folder files. Learning is to know how to read files from root directory. And standard is to use local.properties file for doing local build setttings stuff. – Rohit Sharma Aug 21 '15 at 13:10
  • how can i know the path of android root directory of my app.? – Rohit Sharma Aug 21 '15 at 13:52
  • "/system/app/" + localApplicationInfo.packageName –  Aug 24 '15 at 04:29
0

Here's a variant of the other solutions, stripped down to the essentials:

local.properties:

username=test123
password=abcd1234

build.gradle

android {
    ...
    defaultConfig {
        ...
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newDataInputStream())
        buildConfigField "String", "USERNAME", "\"" + properties.getProperty('username') + "\""
        buildConfigField "String", "PASSWORD", "\"" + properties.getProperty('password') + "\""
    }
}

Java:

String userName = BuildConfig.USERNAME;
j3App
  • 1,510
  • 1
  • 17
  • 26