Project Structure & the android {} Block
An Android project is an ordinary multi-project Gradle build with one special plugin. Here is the smallest useful layout:
hello/
├── settings.gradle.kts which repositories, which modules
├── build.gradle.kts root: declares plugin versions, applies nothing
├── gradle.properties build-wide flags (JVM args, android.* options)
├── gradle/wrapper/… the pinned Gradle version (commit it)
└── app/
├── build.gradle.kts applies com.android.application, the android {} block
└── src/
├── main/
│ ├── AndroidManifest.xml
│ ├── java/com/example/hello/MainActivity.java
│ └── res/values/strings.xml
└── test/java/… local unit tests (JVM)settings.gradle.kts
pluginManagement {
repositories { google(); mavenCentral(); gradlePluginPortal() } // where plugins (AGP) come from
}
dependencyResolutionManagement {
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS // modules may not add their own
repositories { google(); mavenCentral() } // where libraries come from
}
rootProject.name = "hello"
include(":app")google() is Google's Maven repository (AGP, AndroidX, Play libraries). FAIL_ON_PROJECT_REPOS keeps every repository in one place, which matters for supply-chain reviews and for CI mirrors.
The root build file and the version catalog
The root build.gradle.kts declares plugin versions once with apply false; modules then apply them without a version. Most teams keep versions in gradle/libs.versions.toml:
[versions]
agp = "9.4.1"
[libraries]
junit4 = "junit:junit:4.13.2"
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }// build.gradle.kts (root)
plugins { alias(libs.plugins.android.application) apply false }The module and its android {} block
plugins { id("com.android.application") } // or alias(libs.plugins.android.application)
android {
namespace = "com.example.hello" // package of the generated R and BuildConfig classes
compileSdk = 36 // the android.jar you compile against
defaultConfig {
applicationId = "com.example.hello" // the app's identity on devices and in Play
minSdk = 24 // oldest Android version that can install it
targetSdk = 36 // the behaviour version the app opts into
versionCode = 1 // integer, must grow with every upload
versionName = "1.0" // what users see
}
}| Property | Meaning | Changing it later |
|---|---|---|
namespace |
Java package for R/BuildConfig; resolves .MainActivity in the manifest |
a refactor, invisible to users |
applicationId |
the install identity and Play listing | never for a published app: a new id is a new app |
compileSdk |
which APIs the compiler knows about | raise freely; doesn't change runtime behaviour |
minSdk |
devices below it can't install the app | raising it drops users |
targetSdk |
which platform behaviour changes the app has adapted to | raise yearly (Play requires it) and test |
Since AGP 7.3 the package lives in namespace, not in the manifest's package= attribute; AGP rejects a manifest package that disagrees.
Building it
The session writes the files above, generates the wrapper and builds. Look at the task names: each one is a step from the pipeline, for the debug variant:
mkdir -p hello/app/src/main/java/com/example/hello hello/app/src/main/res/values && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement {
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
repositories { google(); mavenCentral() }
}
rootProject.name = "hello"
include(":app")
KTS
mkdir -p gradle
cat > gradle/libs.versions.toml <<'TOML'
[versions]
agp = "9.4.1"
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
TOML
echo 'plugins { alias(libs.plugins.android.application) apply false }' > build.gradle.kts
cat > app/build.gradle.kts <<'KTS'
plugins { alias(libs.plugins.android.application) }
android {
namespace = "com.example.hello"
compileSdk = 36
defaultConfig {
applicationId = "com.example.hello"
minSdk = 24
targetSdk = 36
versionCode = 1
versionName = "1.0"
}
}
KTS
cat > app/src/main/AndroidManifest.xml <<'XML'
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:label="@string/app_name">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML
echo '<resources><string name="app_name">Hello</string></resources>' > app/src/main/res/values/strings.xml
cat > app/src/main/java/com/example/hello/MainActivity.java <<'JAVA'
package com.example.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override protected void onCreate(Bundle state) {
super.onCreate(state);
TextView view = new TextView(this);
view.setText(R.string.app_name);
setContentView(view);
}
}
JAVA
gradle wrapper > /dev/null
ls gradle/wrapper
./gradlew assembleDebug | grep -E '^> Task|^BUILD'gradle-wrapper.jar
gradle-wrapper.properties
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:mergeDebugNativeDebugMetadata NO-SOURCE
> Task :app:generateDebugResources
> Task :app:packageDebugResources
> Task :app:processDebugNavigationResources
> Task :app:parseDebugLocalResources
> Task :app:generateDebugRFile
> Task :app:compileDebugKotlin NO-SOURCE
> Task :app:javaPreCompileDebug
> Task :app:compileDebugJavaWithJavac
> Task :app:generateDebugAssets UP-TO-DATE
> Task :app:mergeDebugAssets
> Task :app:compressDebugAssets
> Task :app:generateDebugGlobalSynthetics
> Task :app:processDebugJavaRes NO-SOURCE
> Task :app:mergeDebugJavaResource
> Task :app:checkDebugDuplicateClasses
> Task :app:desugarDebugFileDependencies
> Task :app:mergeExtDexDebug
> Task :app:mergeLibDexDebug
> Task :app:checkDebugAarMetadata
> Task :app:mapDebugSourceSetPaths
> Task :app:compileDebugNavigationResources
> Task :app:mergeDebugResources
> Task :app:createDebugCompatibleScreenManifests
> Task :app:extractDeepLinksDebug
> Task :app:processDebugMainManifest
> Task :app:processDebugManifest
> Task :app:processDebugManifestForPackage
> Task :app:processDebugResources
> Task :app:dexBuilderDebug
> Task :app:mergeProjectDexDebug
> Task :app:mergeDebugJniLibFolders
> Task :app:mergeDebugNativeLibs NO-SOURCE
> Task :app:stripDebugDebugSymbols NO-SOURCE
> Task :app:validateSigningDebug
> Task :app:writeDebugAppMetadata
> Task :app:writeDebugSigningConfigVersions
> Task :app:packageDebug
> Task :app:createDebugApkListingFileRedirect
> Task :app:assembleDebug
BUILD SUCCESSFUL in 13s
Reading the list top to bottom: resources are merged and compiled (mergeDebugResources, processDebugResources runs aapt2 and generates R), Java is compiled, D8 dexes the classes (dexBuilderDebug, mergeProjectDexDebug), the manifest merger runs (processDebugMainManifest), and packageDebug zips and signs the APK with the debug key. Note compileDebugKotlin NO-SOURCE: AGP 9 has Kotlin support built in, so the task exists even in a Java-only module and is skipped when there are no .kt files.
The full output, once
You'll usually filter Gradle's output in this course. Here is what the end of a build really prints, warts and all:
mkdir -p hello/app/src/main && cd hello
cat > settings.gradle.kts <<'KTS'
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
rootProject.name = "hello"
include(":app")
KTS
echo 'plugins { id("com.android.application") version "9.4.1" apply false }' > build.gradle.kts
cat > app/build.gradle.kts <<'KTS'
plugins { id("com.android.application") }
android {
namespace = "com.example.hello"
compileSdk = 36
defaultConfig { applicationId = "com.example.hello"; minSdk = 24; targetSdk = 36 }
}
KTS
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:label="Hello" /></manifest>' > app/src/main/AndroidManifest.xml
gradle wrapper > /dev/null
./gradlew assembleDebug --warning-mode all | grep -v '^> Task'
aapt2 dump badging app/build/outputs/apk/debug/app-debug.apk | head -1
> Configure project :app
The Configuration.setVisible(boolean) method has been deprecated. This is scheduled to be removed in Gradle 11. Consult the upgrading guide for further information: https://docs.gradle.org/9.8.2/userguide/upgrading_version_9.html#deprecate-visible-property
[Incubating] Problems report is available at: file:///home/learner/hello/build/reports/problems/problems-report.html
BUILD SUCCESSFUL in 10s
32 actionable tasks: 32 executed
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.8.2/userguide/configuration_cache_enabling.html
package: name='com.example.hello' versionCode='' versionName='' platformBuildVersionName='16' platformBuildVersionCode='36' compileSdkVersion='36' compileSdkVersionCodename='16'
Two things to notice. --warning-mode all shows which deprecation triggered Gradle's generic "Deprecated Gradle features were used" footer: here it comes from AGP 9.4.1 itself, not from our scripts, so there's nothing to fix but an AGP upgrade later. And a module with no code and no version fields still builds, but aapt2 dump badging shows the APK has no versionCode or versionName at all (both empty). That's fine for an experiment and wrong for anything you upload (see versionCode & versionName Strategy).
Test yourself
Which property must never change once an app is published on Google Play?
- namespace
- compileSdk
- applicationId
- versionName
What does compileSdk = 36 control?
- The oldest Android version that can install the app
- The android.jar the code is compiled against
- Which platform behaviour changes the app opts into
- The build-tools version
Why put RepositoriesMode.FAIL_ON_PROJECT_REPOS in settings.gradle.kts?
- It makes builds faster
- It forces every module to use the repositories declared centrally, so there is one list to review and mirror
- It disables google()
- It is required by AGP