Android Programming – Part 14: Where to Place Assets Directory and Create a Properties file

Assets and Properties file.

Along with the app-building experiences, you can learn a lot. I mean, really a lot. Here’s another tutorial on how to create an assets directory and a properties file.

During the process of building my own Java/Kotlin Android project that interacts with REST API on my backend server, I needed to create a properties file. If you’re familiar with Java, probably you’ve heard or used it yourself.

When it comes to Android projects, I’ve never done it before, so here I did some google search and implemented it. Here’s what I did.

Place properties file in assets directry:

First of all, you need to store the file in assets directly. The problem is the directory is not made by default, so you need to create it by yourself – here’s how.

Open the project (File -> open), and find your current project. then copy the path.

Paste the copied path to explorer and create the assets directory in src/main/assets.

Then, you can see it in Android Studio IDE, and create a properties file.

Here’s an example of a properties file.

#server_url=http://192.168.0.xxx/
login_php_file=login_sample.php

In a Java class, here’s an example of how to access the properties file and its contents.

private Context mContext;
...
Properties properties = new Properties();
InputStream inputStream = mContext.getAssets().open("server_config.properties");
properties.load(inputStream);
String serverUrl = properties.getProperty("server_url");
String phpInsertFile = properties.getProperty("insert_php_file");
String insert_url = serverUrl + phpInsertFile;

Leave a Reply