Tooling cheat sheet

Revise Android tooling: Gradle modules, build variants, manifest merging, resource qualifiers, and Logcat debugging with adb commands.

Key ideas

  1. Sync reads Gradle files and tells Studio about modules and variants; it does not compile your code.
  2. A module is a Gradle project with its own build.gradle.kts; the app module produces the APK, library modules produce AARs.
  3. compileSdk is compile-time only; targetSdk changes runtime behavior, so test again after raising it.
  4. namespace sets the package for R; applicationId is the device and Play Store identity; they can differ.
  5. implementation hides a dependency from consumers; api exposes it; start with implementation.
  6. android:exported is mandatory on every component with an <intent-filter> when targeting API 31+; omitting it is a build error.
  7. Resource qualifier order is language, smallest width, width, orientation, night, density, API level; folders must follow that order.
  8. adb logcat shows main, system, and crash buffers by default; pass -b for events or radio.
  9. Commit the Gradle wrapper (gradlew, gradle/wrapper/); never commit local.properties or keystore passwords.
  10. R8 minification can silently break reflection-based code; add keep rules in proguard-rules.pro.

APIs at a glance

API or termUse it for
settings.gradle.ktsdeclare modules with include(":app") and set dependencyResolutionManagement
build.gradle.ktsper-module config: namespace, compileSdk, buildTypes, productFlavors
libs.versions.tomlversion catalog; reference as libs.androidx.core.ktx in build files
implementation / apidependency scope; implementation hides from consumers, api exposes
ksp(...)KSP annotation processor; prefer over kapt for Room, Hilt, Moshi
android:exportedrequired on every component with an <intent-filter> when targeting API 31+
tools:node / tools:replaceresolve manifest-merger conflicts; remove deletes, replace overrides an attribute
?attr/colorOnSurfacetheme attribute reference; use instead of a hardcoded hex so dark mode works
adb logcatread log buffers; use --pid, -b crash, -c to filter and clear
adb shell dumpsys activity topdump 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 / isShrinkResourcesrelease 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 V

DayNight 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 implementation over api to keep dependency graphs small and incremental builds fast.
  • Never commit local.properties, keystore passwords, or anything under build/.
  • Always set android:exported explicitly on every component that contains an <intent-filter>.
  • Prefer KSP over kapt for new annotation processing with Room, Hilt, or Moshi.
  • Never hardcode a resource ID integer; always reference R.string.foo or ?attr/....
  • Prefer a DayNight parent theme so dark mode works with no Kotlin code.

Gotchas

  • Referencing debug-only code from main. The class exists in src/debug but not in src/main, so the release build fails to compile. Move shared code to main or 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 xxhdpi and a tablet xhdpi, so density does not mean form factor. Use sw600dp or w600dp for form-factor splits.
  • Logging sensitive data with Log. Log calls stay in release builds unless R8 rules remove them, and anyone with adb access can read them. Never log tokens, passwords, or personal data.
  • Forgetting quantity="other" in a <plurals> block. Most languages use other for many counts, and getQuantityString throws Resources.NotFoundException without it. Always include other plus 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.

Go deeper