web development
directories
/res
/assets
file organization

Difference between /res and /assets directories

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In the world of software development, particularly in web and mobile application development, the organization of assets is pivotal to streamlined code management and efficient resource access. Two commonly encountered directories in this context are `/res` and `/assets`. Although these directories are often used interchangeably by newcomers, they serve distinct purposes and embody different use-cases.

This article dives into the technical differences between the `/res` and `/assets` directories, explores their typical implementations, and provides illustrative examples to differentiate between the two effectively.

Understanding the `/res` Directory

The `/res` directory, short for "resources," is typically used to store static resources that the application may need to load dynamically. This directory is particularly prolific in Android application development, though the concept can be found in other fields, as well.

Key Functionalities

  • Structured Resources: The `/res` directory houses structured XML resources, images, and other static files. These resources are often indexed during the build process and are accessible through generated code constructs, such as references in an `R` class in Android.
  • Configuration Variants: Resources within `/res` can be defined for different device configurations. For instance, different layouts for various screen sizes can be placed in respective subdirectories.
  • Automatic Resource Management: The build system typically handles these resources. It processes, compresses (if necessary), and converts them into a suitable format for the application to use efficiently.

Examples

Consider an Android project structure:

  • `drawable/`: Contains images and image definitions.
  • `layout/`: Houses user interface layout files.
  • `values/`: Includes XML files such as `strings.xml` which defines string resources used across the application.
  • Unstructured Resource Management: Files in `/assets` are accessed programmatically. It allows developers to store any type of file (e.g., fonts, raw data files) that needs to be read and parsed at runtime.
  • Non-Inclusive in Resource Compilation: Resources inside `/assets` aren’t automatically coded into build artifacts and must be read using custom code.
  • File System-like Structure: The directory can mimic a filesystem structure with files and directories arbitrary nested.
  • `fonts/`: Contains font files which can be loaded via runtime operations.
  • `data.json`: An unstructured data file which might be parsed using the app’s logic.
  • Accessibility: If a resource is meant for rendering UI elements or is required by the layout dynamically in different device orientations or screen sizes, the `/res` directory is favorable. It allows for convenient reference in the code.
  • Complexity and Control: If an intricate control over file read/write operations is required, or if the file format is not supported by the `/res` hierarchy, `/assets` provides the necessary flexibility.

Course illustration
Course illustration

All Rights Reserved.