CLAUDE.md, skills and hooks
Why it matters
- Without a
CLAUDE.mdat 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 atgithub.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.jsonrun shell commands on lifecycle events such asPreToolUseandPostToolUse, soktlintor 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.mdis 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:
PreToolUseis the gate that can block a tool call,PostToolUseis 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.
| Event | When it fires | Typical Android use |
|---|---|---|
SessionStart | Session starts or resumes | Print branch name and module list |
UserPromptSubmit | User sends a prompt | Validate or enrich input |
PreToolUse | Before a tool call runs (edit, write, shell) | Block keystore edits |
PostToolUse | After a tool call succeeds | Run ./gradlew ktlintCheck |
Stop | Claude finishes responding | Remind 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.mdlives 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 aSKILL.mdfile. The front matter holdsnameanddescription(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.mddoes. - Hooks are configured in
.claude/settings.jsonunder ahookskey. Each event holds a list of groups; each group has amatcher(tool name pattern) and ahookslist of{ "type": "command", "command": "..." }entries. PreToolUsewithexit 2blocks the call before it runs.PostToolUseruns after the edit;exit 2shows the error to Claude but does not revert the file.- Google publishes official Android skills at
github.com/android/skills. Runandroid skills listto see them andandroid skills add <skill-name>to install one, or copy the folder. The agent in Android Studio reads.agents/skills/or.android-studio/skills/andAGENTS.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
/plugincommand) 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 0matchermatches the tool name;Edit|Writecatches both file-edit and file-create operations.SessionStartandStophave no matcher here, so they run every time.PreToolUseruns before the edit lands, soexit 2inblock-signing.shstops the agent from touching*.jks,*.keystore, orlocal.properties.PostToolUseruns after the edit;run-ktlint.shcalls./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 thesettings.jsonreadable 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
- A team shipping a Compose-based app had no
CLAUDE.md. The agent generated Kotlin files undercom.example.appinstead of the real packagecom.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
ClassNotFoundExceptionin staging.
- The team added a
CLAUDE.mdat the repo root with the real package name, the Gradle module layout, and the build command./gradlew :app:assembleDebug. - They added a
PostToolUsehook 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.
- A developer asked the agent to refactor the
build.gradle.ktssigning block. With noPreToolUsehook, the agent rewrotesigningConfigsand 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 team added a
PreToolUsehook matchingEdit|Writethat runs a script checking whether the target path containskeystore,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.mdbloats 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 thematcherto the tools you care about, e.g.Edit|Write. - Using PostToolUse to block a bad edit.
PostToolUseruns after the file is already written. A non-zero exit surfaces an error but does not revert the change. UsePreToolUsewithexit 2if 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
BuildConfigsecret inCLAUDE.mdis 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:assembleDebugor./gradlew testleaves 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)
-
Create
CLAUDE.mdat 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. -
Create a skill. Make a folder
.claude/skills/add-compose-screen/. Inside it, writeSKILL.mdwith 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:assembleDebugand confirm zero errors." -
Add a
PreToolUsehook. Open.claude/settings.json(create it if missing). Add ahookskey with aPreToolUsegroup:matcherset to"Edit|Write"and ahookslist holding one entry withtype"command"andcommand"bash .claude/hooks/block-signing.sh". Create the script from the example above: it readstool_input.file_pathfrom stdin and exits with code 2 for keystore orlocal.propertiespaths. -
Add a
PostToolUsehook in the same file, using the same shape:matcher"Edit|Write",command"./gradlew ktlintCheck --quiet || true". -
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 thatktlintruns after the edit. Then ask it to editapp/release.jksand confirm thePreToolUsehook 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.