Android Studio project structure
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
appmodule) puts the plates together.
The real names:
| Path | What it is | In Git? |
|---|---|---|
settings.gradle.kts | Lists the modules and where plugins and libraries come from | Yes |
build.gradle.kts (root) | Plugins shared by all modules, declared with apply false | Yes |
app/build.gradle.kts | Build setup for one module | Yes |
gradle/libs.versions.toml | Version catalog: one place for dependency versions | Yes |
gradle/wrapper/ and gradlew | Downloads the exact Gradle version the project needs | Yes |
gradle.properties | Build flags and JVM settings | Yes |
local.properties | Your machine's SDK path (sdk.dir) | No |
build/ and .gradle/ | Generated output and caches | No |
.idea/ | IDE settings, mostly personal | No (a few shared files are fine) |
Inside a module:
src/main/javaorsrc/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.ktsyou writeinclude(":app")orinclude(":feature:login"). The colon path is the module's identity. settings.gradle.ktsalso holdsdependencyResolutionManagement(where libraries are downloaded from) androotProject.name.- A source set is a folder of code and resources.
mainis always included.debug,releaseand flavor folders such assrc/freeare added when that variant is built. - Code in
src/debugis not in release builds. Ifmaincode uses a class that exists only insrc/debug, the release build fails. - Resources in a variant source set override the same resource name from
main. Ris generated fromres/.BuildConfigis generated only whenbuildFeatures { 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.proin 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
- A developer added a
FakeAnalyticsclass undersrc/debugand used it fromMainActivityinsrc/main. - Debug builds passed. The release build on CI failed with an unresolved reference.
- Cause:
src/debugis only part of the debug variant.mainmust compile on its own for every variant.
- Reproduce it by running
./gradlew assembleReleaselocally. Debug runs hide this error. - Declare an interface in
src/mainand letmaincode use only that interface. - Give every variant an implementation: the fake in
src/debugand the real one insrc/release(same class name in both), or choose the implementation with dependency injection. Never refer to a debug-only class frommain. - Run both
assembleDebugandassembleRelease(or at least compile both) in CI.
- A new developer cloned the repo and got errors about an incompatible Gradle or plugin version.
- The team had added
gradle/wrapper/gradle-wrapper.jarto.gitignoreby mistake, so./gradlewcould not run, and the developer used a newer Gradle installed on their machine. - Cause: the build depended on whatever Gradle each person had.
- Commit the wrapper files. Always run builds with
./gradlew, not a globalgradle. - Pin the version in
gradle-wrapper.propertiesand upgrade it on purpose, in a single reviewed commit. - Keep
local.propertiesout 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 likesdk.dirthat 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:
- 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.propertiesandsettings.gradle.kts. - Create
app/src/debug/kotlin/.../DebugInfo.ktwith a small top-level function. Call it fromMainActivity. Run./gradlew assembleDebug, then./gradlew assembleRelease. - Read the release error, then fix it by moving the code to
main. - Open
.gitignoreand confirmlocal.propertiesandbuild/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.