Android Studio project structure

What each file and folder in an Android project is for, which ones belong in Git, and how source sets decide what ends up in a build. About 15 minutes.

Why it matters

  • You open the same few files every day. If you do not know which file controls what, you edit the wrong one and lose an hour.
  • "Works on my machine" bugs often come from files that should not be shared (local.properties, build/) or files that must be shared (the Gradle wrapper).
  • Source sets and modules are the base of every modern project layout. Interviewers ask about them when they ask about architecture.
  • Knowing the layout lets you read any open-source Android project in minutes.

The simple explanation

Think of a restaurant.

  • The kitchen is where food is made: your code and resources (src/main).
  • The menu card says what the restaurant offers: the manifest.
  • The head chef's rule book says how to cook and who supplies what: the Gradle files.
  • The dining room is the output. You do not edit it by hand: the build/ folder is generated.
  • Branch kitchens (modules) cook one part of the menu each. The main restaurant (the app module) puts the plates together.

The real names:

PathWhat it isIn Git?
settings.gradle.ktsLists the modules and where plugins and libraries come fromYes
build.gradle.kts (root)Plugins shared by all modules, declared with apply falseYes
app/build.gradle.ktsBuild setup for one moduleYes
gradle/libs.versions.tomlVersion catalog: one place for dependency versionsYes
gradle/wrapper/ and gradlewDownloads the exact Gradle version the project needsYes
gradle.propertiesBuild flags and JVM settingsYes
local.propertiesYour machine's SDK path (sdk.dir)No
build/ and .gradle/Generated output and cachesNo
.idea/IDE settings, mostly personalNo (a few shared files are fine)

Inside a module:

  • src/main/java or src/main/kotlin: code. Both folder names work for Kotlin.
  • src/main/res: resources.
  • src/main/AndroidManifest.xml: the manifest.
  • src/test: local unit tests on the JVM. src/androidTest: tests on a device.

The Android view in Studio is a grouped view, not the real disk layout. Switch to Project to see the real folders.

Notes

  • Sync is not build. Sync reads the Gradle files and tells Studio about modules, dependencies and variants. It does not compile your code.
  • A module is a Gradle project with its own build file. In settings.gradle.kts you write include(":app") or include(":feature:login"). The colon path is the module's identity.
  • settings.gradle.kts also holds dependencyResolutionManagement (where libraries are downloaded from) and rootProject.name.
  • A source set is a folder of code and resources. main is always included. debug, release and flavor folders such as src/free are added when that variant is built.
  • Code in src/debug is not in release builds. If main code uses a class that exists only in src/debug, the release build fails.
  • Resources in a variant source set override the same resource name from main.
  • R is generated from res/. BuildConfig is generated only when buildFeatures { buildConfig = true } is set. Recent AGP versions turn it off by default.
  • Commit the Gradle wrapper (gradlew, gradlew.bat, gradle/wrapper/). Then every developer and CI use the same Gradle version.
  • Library modules compile to an AAR. The app module produces the APK or AAB. A library cannot be installed or run on its own: its manifest is merged into the app's manifest.
  • proguard-rules.pro in a module holds the keep rules for R8. It is only used if minification is enabled.

Code example

A typical multi-module layout and the file that wires it together.

MyApp/
  settings.gradle.kts
  build.gradle.kts
  gradle.properties
  local.properties          (not committed)
  gradle/
    libs.versions.toml
    wrapper/gradle-wrapper.properties
  gradlew
  app/
    build.gradle.kts
    proguard-rules.pro
    src/
      main/ (AndroidManifest.xml, kotlin/, res/)
      debug/ (debug-only code and resources)
      test/ (JVM unit tests)
      androidTest/ (device tests)
  core/data/build.gradle.kts
  feature/login/build.gradle.kts
// settings.gradle.kts
pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}
 
rootProject.name = "MyApp"
include(":app")
include(":core:data")
include(":feature:login")

Check your understanding: if you add a new module and forget the include line, Gradle does not know it exists. The folder is there, but nothing builds it.

In production

Release build breaks, debug build is fine
  • A developer added a FakeAnalytics class under src/debug and used it from MainActivity in src/main.
  • Debug builds passed. The release build on CI failed with an unresolved reference.
  • Cause: src/debug is only part of the debug variant. main must compile on its own for every variant.
The fix
  1. Reproduce it by running ./gradlew assembleRelease locally. Debug runs hide this error.
  2. Declare an interface in src/main and let main code use only that interface.
  3. Give every variant an implementation: the fake in src/debug and the real one in src/release (same class name in both), or choose the implementation with dependency injection. Never refer to a debug-only class from main.
  4. Run both assembleDebug and assembleRelease (or at least compile both) in CI.
New teammate cannot build: wrong Gradle version
  • A new developer cloned the repo and got errors about an incompatible Gradle or plugin version.
  • The team had added gradle/wrapper/gradle-wrapper.jar to .gitignore by mistake, so ./gradlew could not run, and the developer used a newer Gradle installed on their machine.
  • Cause: the build depended on whatever Gradle each person had.
The fix
  • Commit the wrapper files. Always run builds with ./gradlew, not a global gradle.
  • Pin the version in gradle-wrapper.properties and upgrade it on purpose, in a single reviewed commit.
  • Keep local.properties out of Git. It contains an absolute path to one machine's SDK.

Common mistakes

  • Editing generated files in build/. Your change is erased on the next build. Edit the source instead.
  • Committing local.properties. It holds a path like sdk.dir that exists on one computer only, and sometimes keys people add by habit.
  • Treating the Android view as the real folder layout. It merges folders. Use the Project view when you need the truth, for example to find a flavor folder.
  • Referencing debug-only code from main. It compiles in debug and breaks release.
  • Putting everything in app. One big module means slow builds and no clear boundaries. Split by feature or layer when the project grows.
  • Pressing Sync repeatedly to fix a compile error. Sync does not compile. Read the error from the build output.

Interview questions

Answer out loud first. Then reveal the answer and compare it at each depth.

“What is the difference between the root build.gradle.kts and the app build.gradle.kts?”

“What is a source set?”

“Why must the Gradle wrapper be committed?”

Quiz

Which file should NOT be committed to Git?

What does Gradle Sync do?

A class exists only in src/debug and is used from src/main. What happens?

You created a feature folder with a build file but the module is never built. What is missing?

0 of 4 answered

Mini task

Take any Android project (or create a new one) and do this in 15 minutes:

  1. Switch Studio from the Android view to the Project view and write down the path of five files: the manifest, one layout or string file, libs.versions.toml, gradle-wrapper.properties and settings.gradle.kts.
  2. Create app/src/debug/kotlin/.../DebugInfo.kt with a small top-level function. Call it from MainActivity. Run ./gradlew assembleDebug, then ./gradlew assembleRelease.
  3. Read the release error, then fix it by moving the code to main.
  4. Open .gitignore and confirm local.properties and build/ are listed and the wrapper files are not.

You are done when the release build passes and you can explain why it failed before without looking at this page.

Next: Gradle basics and build variants