Android: How to override properties files from jar dependency | ASSIST Software Romania
get in touch



Facebook Share Tweet LinkedIn Share


LinkedIn Follow Xing Follow
Sebastian Macarescu

Android Developer at ASSIST


Often times when writing a library to be used by many clients (servers, mobile apps), the programmer is faced with the following feature: making it configurable by implementing an intermediate layer which alters its
functionallity. This is a small tutorial about how to override properties files from jar dependency
Let's take for example a library for http calls which consumes an api. The programmer using this sdk may want to change the url of the api, the port, client validation tokens, or default credentials.

One way of implementing the before mentioned layer is through properties files. Create a file named and place it under resources directory.

You can put in it the options to be overridden. Example:



application.auth.useragent=App Java Client


The file must be placed in the classpath. Make sure the resources directory is present in maven file. If not add under resources tag the following:




This will include the file when building the jar which means you now have some default properties.

Reading the properties file is done through:

Properties prop = new Properties();

String propFileName = "";



 InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName);

 if (inputStream != null) {


 } else {

   throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");


 appId = prop.getProperty("application.auth.appid");

 appKey = prop.getProperty("application.auth.appkey");

 userAgent = prop.getProperty("application.auth.useragent");

 serverUrl = prop.getProperty("application.auth.serverurl");



catch(IOException e){



In android studio you can include the library as usual with gradle: compile 'groupId:artifactId-version' (the variables can be extracted from the maven build file).

If you try to create a file with the same name:, which will contain the new configuration and place it under assets, res or raw, it will be included in the apk under folders with same name. Since the apk is just an archive you can open it with a zip client to explore the contents and check where the file is placed.
What we want is to include it in the root path of the apk. To do that, create a directory named "resources" under /src/main path and place the new file there. You have now a fully configurable sdk. Congratulations.

To understand more about class loaders and how resources are loaded, read the following:


Photo source:

Do you want to get in touch with us? 

If you are interested in our software development services, you would like to join our team, or you simply want to find out more about us, we’d love to hear from you! Drop us a line and a member of the ASSIST team will get back to you as soon as possible. We are sure we can ASSIST you.