CLAUDE.md, skills and hooks

CLAUDE.md, skills, and hooks configure Claude Code for Android: always-loaded memory, on-demand skills, and hooks that run ktlint or block signing edits. About 25 minutes.

Why it matters

  • Without a CLAUDE.md at the repo root, the agent guesses your package name, build command, and architecture, producing Kotlin that does not compile against your project.
  • Skills in .claude/skills/ encode team conventions (Compose patterns, naming rules) once instead of pasting them into every prompt. Google publishes official Android skills at github.com/android/skills; you can install them with the Android CLI (android skills add <skill-name>) or copy the skill folders.
  • Hooks in .claude/settings.json run shell commands on lifecycle events such as PreToolUse and PostToolUse, so ktlint or a keystore guard fires automatically and the agent cannot skip it.
  • Interviewers at senior level ask how you govern AI-generated code in a team: what is in CLAUDE.md, which hooks block dangerous edits, and how you review the diff.

The simple explanation

  • CLAUDE.md is the onboarding wiki a new Android developer reads on day one: package names, build commands, architecture rules.
  • A skill is a runbook for one task ("add a Compose screen", "write a Room DAO") that the agent pulls only when relevant.
  • Hooks are the CI pipeline: PreToolUse is the gate that can block a tool call, PostToolUse is the lint step that runs after the edit lands.
  • A plugin bundles skills, hooks, and MCP servers so a teammate runs one install command instead of wiring everything by hand.

CLAUDE.md is a Markdown file at the repo root (or in .claude/) that Claude Code loads into every session. It holds project context: package names, build commands (./gradlew assembleDebug), architecture notes, and team conventions. Without it the agent guesses and produces code that does not match your project.

Skills are folders under .claude/skills/, each containing a SKILL.md with YAML front matter (name, description) and instructions. Only their descriptions sit in context at the start; the full skill loads when the agent judges it relevant, or when you invoke it as /skill-name. Google publishes official Android skills at github.com/android/skills. The agent in Android Studio reads skills from .agents/skills/ or .android-studio/skills/ plus AGENTS.md files. Skills follow the open Agent Skills standard, so a skill written for Claude Code can also work in other agents that support the format.

Hooks are shell commands bound to lifecycle events in .claude/settings.json. They fire at fixed points in the agent loop and can block or allow actions.

EventWhen it firesTypical Android use
SessionStartSession starts or resumesPrint branch name and module list
UserPromptSubmitUser sends a promptValidate or enrich input
PreToolUseBefore a tool call runs (edit, write, shell)Block keystore edits
PostToolUseAfter a tool call succeedsRun ./gradlew ktlintCheck
StopClaude finishes respondingRemind to review diffs

A hook gets the event details as JSON on stdin. A PreToolUse hook that exits with code 2 blocks the call and sends its stderr to Claude; other non-zero codes are non-blocking errors. A PostToolUse hook cannot undo the edit, but exit code 2 shows its stderr to Claude so it can fix the problem. Plugins bundle skills, hooks, subagents, and MCP servers into one installable unit. For connecting the agent to external tools such as a design system or a bug tracker, see the MCP servers lesson in this module.

Notes

  • CLAUDE.md lives at the repo root (or .claude/CLAUDE.md) and is always loaded into every Claude Code session. Keep it concise; move detail into skills.
  • A skill is a folder in .claude/skills/ with a SKILL.md file. The front matter holds name and description (the description is what the agent matches on); the body holds step-by-step instructions, with optional scripts and references beside it.
  • Skills load on demand (the agent decides relevance) or by explicit slash command. They do not bloat every prompt the way CLAUDE.md does.
  • Hooks are configured in .claude/settings.json under a hooks key. Each event holds a list of groups; each group has a matcher (tool name pattern) and a hooks list of { "type": "command", "command": "..." } entries.
  • PreToolUse with exit 2 blocks the call before it runs. PostToolUse runs after the edit; exit 2 shows the error to Claude but does not revert the file.
  • Google publishes official Android skills at github.com/android/skills. Run android skills list to see them and android skills add <skill-name> to install one, or copy the folder. The agent in Android Studio reads .agents/skills/ or .android-studio/skills/ and AGENTS.md.
  • Skills follow the open Agent Skills standard, so a skill is portable across agents that implement the format, not just Claude Code.
  • A plugin bundles skills, hooks, subagents, and MCP servers. A teammate installs one plugin (through the /plugin command) instead of wiring everything by hand.
  • Never put API keys, keystore passwords, or secrets in CLAUDE.md, a skill, or a hook command. Those files are committed to the repo. Secrets stay on a server.
  • Treat every AI-written diff like a junior colleague's pull request: read it, run the tests, and check that the agent did not touch signing files or BuildConfig.

Code example

This settings.json snippet wires four hook events for an Android project: a PreToolUse guard that blocks edits to keystore or signing files, a PostToolUse step that runs ktlint, and SessionStart / Stop messages. The actual shell logic lives in small scripts under .claude/hooks/.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "bash \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-signing.sh" }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "bash \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/run-ktlint.sh" }
        ]
      }
    ],
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "echo 'Android project. Build with ./gradlew assembleDebug.'" }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "echo 'Review all diffs before committing.'" }
        ]
      }
    ]
  }
}

A minimal block-signing.sh reads the tool input from stdin:

#!/usr/bin/env bash
path=$(jq -r '.tool_input.file_path // empty')
if echo "$path" | grep -qE '\.jks$|\.keystore$|local\.properties$'; then
  echo "Blocked: signing and secret files are off limits." >&2
  exit 2
fi
exit 0
  • matcher matches the tool name; Edit|Write catches both file-edit and file-create operations. SessionStart and Stop have no matcher here, so they run every time.
  • PreToolUse runs before the edit lands, so exit 2 in block-signing.sh stops the agent from touching *.jks, *.keystore, or local.properties.
  • PostToolUse runs after the edit; run-ktlint.sh calls ./gradlew ktlintCheck, and exiting with code 2 sends the lint errors back to Claude. The file is not reverted.
  • Keeping shell logic in separate scripts (.claude/hooks/*.sh) makes the settings.json readable and lets you version-control the scripts independently.

Run the idea in plain Kotlin

// Same logic as block-signing.sh: exit code 2 blocks the tool call, 0 allows it.
val blockedEndings = listOf(".jks", ".keystore", "local.properties")

fun preToolUse(tool: String, filePath: String): Int {
  if (tool != "Edit" && tool != "Write") return 0 // matcher: Edit|Write
  return if (blockedEndings.any { filePath.endsWith(it) }) 2 else 0
}

fun main() {
  val calls = listOf(
      "Edit" to "app/src/main/java/com/acme/LoginViewModel.kt",
      "Write" to "app/release.jks",
      "Edit" to "local.properties",
      "Read" to "app/release.jks"
  )
  for ((tool, path) in calls) {
      val code = preToolUse(tool, path)
      val result = if (code == 2) "BLOCKED" else "allowed"
      println(tool + " " + path + " -> exit " + code + " (" + result + ")")
  }
}

In production

Agent kept using the wrong package name
  • A team shipping a Compose-based app had no CLAUDE.md. The agent generated Kotlin files under com.example.app instead of the real package com.acme.wallet.
  • Every generated file needed a manual find-and-replace before it compiled, and two PRs slipped through with the wrong package, causing a ClassNotFoundException in staging.
The fix
  • The team added a CLAUDE.md at the repo root with the real package name, the Gradle module layout, and the build command ./gradlew :app:assembleDebug.
  • They added a PostToolUse hook that runs ./gradlew :app:compileDebugKotlin, so unresolved references from a wrong package fail fast and the agent sees the error immediately.
  • After the change, the agent produced compilable code on the first pass for subsequent sessions.
Agent edited a keystore file
  • A developer asked the agent to refactor the build.gradle.kts signing block. With no PreToolUse hook, the agent rewrote signingConfigs and overwrote the keystore path with a placeholder string.
  • The next release build failed because the keystore file could not be found, and the team lost time diagnosing it.
The fix
  • The team added a PreToolUse hook matching Edit|Write that runs a script checking whether the target path contains keystore, signing, or .jks; if so, the script exits with code 2 and the edit is blocked.
  • They also added a note in CLAUDE.md: "Never modify signing configuration or keystore files. Ask the user to do it manually."
  • The hook and the instruction together made the agent stop and ask instead of editing.

Common mistakes

  • Putting the entire architecture doc in CLAUDE.md. A very long CLAUDE.md bloats every prompt and slows the agent. Keep it to project-level facts (package name, build command, module layout) and move detailed conventions into skills that load on demand.
  • Forgetting the matcher in a hook entry. Without a matcher, the hook fires on every tool call, including reads and searches, adding latency to every step. Always scope the matcher to the tools you care about, e.g. Edit|Write.
  • Using PostToolUse to block a bad edit. PostToolUse runs after the file is already written. A non-zero exit surfaces an error but does not revert the change. Use PreToolUse with exit 2 if you need to prevent the edit from landing.
  • Committing secrets into CLAUDE.md or a skill. These files live in the repo. An API key, keystore password, or BuildConfig secret in CLAUDE.md is visible to every clone. Secrets must stay on a server; reference an environment variable or a secrets manager instead.
  • Skipping the diff review after an agent session. The agent can produce plausible-looking Kotlin that compiles but violates your architecture or introduces a subtle null-safety bug. Treat every diff like a junior colleague's PR: read it, run ./gradlew test, and check that no signing files were touched.
  • Writing a skill with no verification step. A skill that says "write a Compose screen" without telling the agent to run ./gradlew :app:assembleDebug or ./gradlew test leaves the agent with no way to check its own work. Always include a verification command in the skill body.

Interview questions

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

“What is CLAUDE.md and when does it load?”

“How do skills differ from CLAUDE.md, and where do they live?”

“What are hooks and when would you use them in an Android project?”

“How do you govern AI-generated code in a team of five Android developers?”

Quiz

Where does CLAUDE.md live and when is it loaded?

What happens when a PreToolUse hook exits with code 2?

Where do project-level skills live for Claude Code?

Which file configures hooks for Claude Code?

0 of 4 answered

Mini task

Wire CLAUDE.md, a skill, and a hook for your Android project (15 min)

  1. Create CLAUDE.md at the repo root. Add three lines: the application package name, the Gradle build command (./gradlew :app:assembleDebug), and one architecture rule (for example "UI layer uses Jetpack Compose; no XML layouts"). Keep it under 30 lines.

  2. Create a skill. Make a folder .claude/skills/add-compose-screen/. Inside it, write SKILL.md with YAML front matter (name: add-compose-screen, description: Scaffold a new Compose screen with a ViewModel and a test) and a body that lists the steps: create the composable, create the ViewModel, add a unit test, run ./gradlew :app:testDebugUnitTest. End the body with a verification line: "Run ./gradlew :app:assembleDebug and confirm zero errors."

  3. Add a PreToolUse hook. Open .claude/settings.json (create it if missing). Add a hooks key with a PreToolUse group: matcher set to "Edit|Write" and a hooks list holding one entry with type "command" and command "bash .claude/hooks/block-signing.sh". Create the script from the example above: it reads tool_input.file_path from stdin and exits with code 2 for keystore or local.properties paths.

  4. Add a PostToolUse hook in the same file, using the same shape: matcher "Edit|Write", command "./gradlew ktlintCheck --quiet || true".

  5. Test. Start a Claude Code session. Ask it to add a new Compose screen. Confirm it uses the package name from CLAUDE.md, follows the skill steps, and that ktlint runs after the edit. Then ask it to edit app/release.jks and confirm the PreToolUse hook blocks the edit.

Done when: CLAUDE.md is under 30 lines, the skill folder exists with a valid SKILL.md, both hooks are in settings.json, the agent follows the skill for a new screen, and the keystore edit is blocked.

Next: MCP servers for Android development