Android Studio
Assets Folder
Android Development
File Organization
Android Application Design

Where to place the 'assets' folder in Android Studio?

Master System Design with Codemia

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

Introduction

The assets folder in Android is used to store raw files that your application can read at runtime using the AssetManager API. Unlike the res directory, files in assets are not assigned resource IDs and can have arbitrary directory structures. Knowing where to place this folder and how to use it is essential for working with bundled files like HTML pages, JSON data, fonts, or databases.

Correct Location

In Android Studio, the assets folder belongs inside the main source set, at the same level as the java and res directories:

 
1app/
2  src/
3    main/
4      assets/          <-- Place it here
5        data.json
6        fonts/
7          custom.ttf
8        web/
9          index.html
10      java/
11      res/
12    androidTest/
13    test/

The full path is:

 
app/src/main/assets/

How to Create It

Method 1: Using Android Studio UI

  1. Right-click on the app module in the Project panel
  2. Select New > Folder > Assets Folder
  3. Keep the default location (src/main/assets) and click Finish

Method 2: Manual Creation

Create the directory manually:

bash
mkdir -p app/src/main/assets

Android Studio will recognize it automatically on the next Gradle sync.

Method 3: Custom Location in build.gradle

You can specify a custom assets directory in your build.gradle:

groovy
1android {
2    sourceSets {
3        main {
4            assets.srcDirs = ['src/main/assets', 'src/main/custom-assets']
5        }
6    }
7}

Reading Files from Assets

Use the AssetManager to read files at runtime:

Java

java
1try {
2    InputStream is = getAssets().open("data.json");
3    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
4    StringBuilder sb = new StringBuilder();
5    String line;
6    while ((line = reader.readLine()) != null) {
7        sb.append(line);
8    }
9    reader.close();
10    String jsonContent = sb.toString();
11} catch (IOException e) {
12    e.printStackTrace();
13}

Kotlin

kotlin
val jsonContent = assets.open("data.json").bufferedReader().use { it.readText() }

Listing Files in a Subdirectory

kotlin
1val fontFiles = assets.list("fonts") ?: emptyArray()
2for (file in fontFiles) {
3    Log.d("Assets", "Found font: $file")
4}

Assets vs res/raw

Both assets and res/raw can store raw files, but they serve different purposes:

Featureassets/res/raw/
Resource IDNoYes (R.raw.filename)
SubdirectoriesYesNo
File namingAny charactersLowercase, no special chars
Access methodAssetManagerResources.openRawResource()
Included in APKAs-isAs-is
Use caseComplex file structures, web contentSimple files like audio, certificates

Common Use Cases

Loading a Custom Font

kotlin
val typeface = Typeface.createFromAsset(assets, "fonts/custom.ttf")
textView.typeface = typeface

Loading a Local HTML Page in WebView

kotlin
webView.loadUrl("file:///android_asset/web/index.html")

Note the triple slash and android_asset (not assets) in the URL scheme.

Pre-populating a Room Database

kotlin
Room.databaseBuilder(context, AppDatabase::class.java, "app.db")
    .createFromAsset("databases/prepopulated.db")
    .build()

Build Variants and Flavors

You can have different assets per build variant:

 
1app/src/
2  main/assets/          # Shared assets
3  debug/assets/         # Debug-only assets
4  release/assets/       # Release-only assets
5  freeDebug/assets/     # Flavor + build type specific

Assets from more specific source sets override those from main.

Common Pitfalls

  • Case sensitivity: Asset file names are case-sensitive on Android. Data.json and data.json are different files. Be consistent with naming.
  • File size limits: While there is no hard limit on individual asset files, very large files (>100MB) may cause build issues or slow app startup. Consider downloading large files at runtime instead.
  • Compression: By default, some file types in assets are compressed in the APK. Files with extensions like .jpg, .png, .mp3, and .ogg are not compressed (already compressed formats). Use aaptOptions { noCompress } in build.gradle to control this behavior.
  • WebView URL path: When loading assets in a WebView, use file:///android_asset/ (with android_asset, singular, not assets). This is a common source of "file not found" errors.
  • ProGuard/R8: Asset files are not affected by code shrinking, but make sure your asset paths are not hardcoded in strings that might be optimized away.

Summary

  • Place the assets folder at app/src/main/assets/
  • Create it via New > Folder > Assets Folder in Android Studio or manually
  • Access files with getAssets().open("filename")
  • Use assets for files needing subdirectories or arbitrary naming; use res/raw for simple files that benefit from resource IDs
  • For WebView, use the file:///android_asset/ URL scheme

Course illustration
Course illustration

All Rights Reserved.