Tooling cheat sheet
Key ideas
- Sync reads Gradle files and tells Studio about modules and variants; it does not compile your code.
- A module is a Gradle project with its own
build.gradle.kts; the app module produces the APK, library modules produce AARs. compileSdkis compile-time only;targetSdkchanges runtime behavior, so test again after raising it.namespacesets the package forR;applicationIdis the device and Play Store identity; they can differ.implementationhides a dependency from consumers;apiexposes it; start withimplementation.android:exportedis mandatory on every component with an<intent-filter>when targeting API 31+; omitting it is a build error.- Resource qualifier order is language, smallest width, width, orientation, night, density, API level; folders must follow that order.
adb logcatshowsmain,system, andcrashbuffers by default; pass-bforeventsorradio.- Commit the Gradle wrapper (
gradlew,gradle/wrapper/); never commitlocal.propertiesor keystore passwords. - R8 minification can silently break reflection-based code; add keep rules in
proguard-rules.pro.
APIs at a glance
| API or term | Use it for |
|---|---|
settings.gradle.kts | declare modules with include(":app") and set dependencyResolutionManagement |
build.gradle.kts | per-module config: namespace, compileSdk, buildTypes, productFlavors |
libs.versions.toml | version catalog; reference as libs.androidx.core.ktx in build files |
implementation / api | dependency scope; implementation hides from consumers, api exposes |
ksp(...) | KSP annotation processor; prefer over kapt for Room, Hilt, Moshi |
android:exported | required on every component with an <intent-filter> when targeting API 31+ |
tools:node / tools:replace | resolve manifest-merger conflicts; remove deletes, replace overrides an attribute |
?attr/colorOnSurface | theme attribute reference; use instead of a hardcoded hex so dark mode works |
adb logcat | read log buffers; use --pid, -b crash, -c to filter and clear |
adb shell dumpsys activity top | dump the top activity's state and view hierarchy without a debugger |
buildFeatures { buildConfig = true } | opt in to BuildConfig generation; off by default in recent AGP |
isMinifyEnabled / isShrinkResources | release build flags that enable R8 shrinking and resource removal |
Snippets
Per-module Gradle config with version catalog
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.ksp)
}
android {
namespace = "com.example.shop"
compileSdk = 36
defaultConfig {
applicationId = "com.example.shop"
minSdk = 24
targetSdk = 36
}
buildFeatures { buildConfig = true }
}
dependencies {
implementation(libs.androidx.core.ktx)
}Manifest with an exported launcher and a private service
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application android:name=".App" android:allowBackup="true">
<activity android:name=".ui.MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".sync.SyncService" android:exported="false" />
</application>
</manifest>Common adb logcat and dumpsys commands
adb logcat -c
adb logcat --pid=$(adb shell pidof -s com.example.app)
adb logcat NetworkClient:V DbHelper:W *:S
adb logcat -b crash -d
adb shell dumpsys activity top | head -60
adb shell setprop log.tag.NetworkClient VDayNight theme pair for dark mode
<resources>
<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
<item name="colorPrimary">#6750A4</item>
<item name="colorOnPrimary">#FFFFFF</item>
<item name="colorSurface">#FFFBFE</item>
<item name="colorOnSurface">#1C1B1F</item>
</style>
</resources>Rules of thumb
- Prefer
implementationoverapito keep dependency graphs small and incremental builds fast. - Never commit
local.properties, keystore passwords, or anything underbuild/. - Always set
android:exportedexplicitly on every component that contains an<intent-filter>. - Prefer KSP over
kaptfor new annotation processing with Room, Hilt, or Moshi. - Never hardcode a resource ID integer; always reference
R.string.fooor?attr/.... - Prefer a
DayNightparent theme so dark mode works with no Kotlin code.
Gotchas
- Referencing debug-only code from main. The class exists in
src/debugbut not insrc/main, so the release build fails to compile. Move shared code tomainor guard the reference with a variant check. - Omitting android:exported on a component with an <intent-filter>. On API 31+ this is a build error, not a warning. Add
android:exported="true"or"false"explicitly to every such component. - Using density qualifiers to target tablets. A phone can be
xxhdpiand a tabletxhdpi, so density does not mean form factor. Usesw600dporw600dpfor form-factor splits. - Logging sensitive data with Log.
Logcalls stay in release builds unless R8 rules remove them, and anyone withadbaccess can read them. Never log tokens, passwords, or personal data. - Forgetting quantity="other" in a <plurals> block. Most languages use
otherfor many counts, andgetQuantityStringthrowsResources.NotFoundExceptionwithout it. Always includeotherplus the forms each language needs. - Pressing Sync repeatedly to fix a compile error. Sync does not compile; it only reads Gradle files, so the error persists. Read the build output from the Build window instead.
Quick interview answers
What is the difference between compileSdk and targetSdk?
compileSdk is a compile-time choice that lets you call new APIs; raising it does not change runtime behavior. targetSdk opts the app into new platform behavior rules, so you must test again after raising it.
Why must android:exported be declared on components with an intent filter?
When targeting API 31+, the build fails if android:exported is missing on any activity, service, or receiver that has an <intent-filter>. Set it to true only when another app needs to start or bind to the component; otherwise use false.
What is the difference between implementation and api in Gradle?
implementation makes a dependency available to the current module but hides it from modules that depend on it. api also exposes the dependency to consumers, which is needed when a library module re-exports types.
How does the manifest merger resolve conflicting elements?
Priority goes from build variant manifests, to the app manifest, to libraries in dependency order. A conflicting attribute does not silently win; the build fails until you add tools:replace or tools:node to resolve it.
What does adb logcat -b crash -d do?
It reads the crash buffer, which holds fatal exception stack traces, and exits after dumping instead of tailing. The crash buffer is separate from main and system, so you need -b to see it.
Why should you commit the Gradle wrapper?
The wrapper pins the Gradle version so every developer and CI machine uses the same build tool. Without it, different machines may use different Gradle versions and produce different build results.