Building apps with AI agents cheat sheet
Key ideas
- CLAUDE.md (repo root or
.claude/CLAUDE.md) is loaded into every session; the docs suggest under about 200 lines of build commands, module layout, and hard rules. - A skill lives in
.claude/skills/<name>/SKILL.md; only descriptions sit in context at the start, and the full skill loads when the task matches itsdescriptionor you type/skill-name. - Hooks in
.claude/settings.jsonrun shell commands on lifecycle events and get the event as JSON on stdin;PreToolUsewithexit 2blocks a tool call before it runs. - MCP servers in
.mcp.jsonexpose external tools (Jira, Figma, Gradle) to the agent over JSON-RPC 2.0;stdiotransport spawns a child process with no open port. - Give the agent a verification command (
./gradlew test,./gradlew lint); without it the agent guesses and produces plausible-looking code that fails the build. - Treat every AI-written diff like a junior colleague's PR: read every hunk, run tests, check nullability, lifecycle handling, and Compose recomposition.
- Never put API keys, keystore passwords, or secrets in
CLAUDE.md, a skill,.mcp.json, or a prompt; secrets stay on a server or in a git-ignored local file. - A plugin bundles skills, hooks, subagents, and MCP servers into one installable unit so a team shares one setup instead of copying files.
- The open Agent Skills standard is shared across tools: Claude Code reads
.claude/skills/andCLAUDE.md; Android Studio reads.agents/skills/or.android-studio/skills/andAGENTS.md.
APIs at a glance
| API or term | Use it for |
|---|---|
CLAUDE.md | Repo-root file always loaded into every Claude Code session; hold build commands, module layout, and hard rules. |
SKILL.md | Per-task instruction file in .claude/skills/<name>/; the description front-matter triggers on-demand loading. |
PreToolUse | Hook event in .claude/settings.json that runs before a tool call; exit 2 blocks it and sends stderr to Claude (e.g. edits to keystore.jks). |
PostToolUse | Hook event that runs after a successful tool call; exit 2 shows stderr to Claude but does not revert the file. |
.mcp.json | Project-root config declaring MCP servers under mcpServers; supports ${VAR} expansion for secrets. |
stdio transport | The agent spawns the server as a child process over stdin/stdout, no network port opened; remote servers use http (sse is deprecated). |
AGENTS.md | Android Studio equivalent of CLAUDE.md; prepended to every prompt, supports @./file.md imports. |
android skills list / android skills add | List and install official Android skills from github.com/android/skills. |
matcher | Tool-name pattern in a hook group (e.g. Edit|Write); without it the hook fires on every tool call. |
mcp__<server>__<tool> | How Claude Code names MCP tools, so a PreToolUse matcher such as mcp__gradle-runner__.* can block risky calls. |
tools/list / tools/call | JSON-RPC methods an agent uses to discover an MCP server's tools and call one with JSON arguments. |
isError: true | MCP tool-result flag; return an error result instead of letting the server crash, because a dead server's tools disappear and the agent may carry on guessing. |
Snippets
CLAUDE.md skeleton for an Android project
# CLAUDE.md — MyAndroidApp
## Build & verify
- Build: `./gradlew assembleDebug`
- Unit tests: `./gradlew :app:testDebugUnitTest`
- Lint: `./gradlew :app:lintDebug`
## Hard rules
- No `var` in `ViewModel`; expose `StateFlow` / `SharedFlow` only.
- Compose: hoist state, use `rememberSaveable` for UI state.
- No `TODO()` or `println` in committed code.PreToolUse hook blocking signing-file edits
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command",
"command": "bash \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-signing.sh" }
]
}
]
}
}MCP server entry with stdio transport
{
"mcpServers": {
"gradle-runner": {
"command": "node",
"args": [".mcp-servers/gradle-server.js"],
"env": { "PROJECT_ROOT": "." },
"type": "stdio"
}
}
}Post-agent CI gate script
#!/usr/bin/env bash
set -euo pipefail
if git diff --name-only HEAD~1 | grep -qE '(keystore|\.jks|local\.properties)'; then
echo "BLOCKED: AI agent touched a signing or secret file."
exit 1
fi
./gradlew ktlintCheck --quiet
./gradlew testDebugUnitTest --quiet
./gradlew assembleRelease --quiet
echo "All checks passed."Rules of thumb
- Prefer a short
CLAUDE.md(under ~200 lines) and move detailed procedures into skills that load on demand. - Never put an API key, keystore password, or
BuildConfigsecret inCLAUDE.md, a skill,.mcp.json, or a prompt; in.mcp.jsonreference an environment variable with${VAR}. - Prefer permission
denyrules orPreToolUsewithexit 2to block forbidden paths; a rule inCLAUDE.mdis only context, andPostToolUsecannot revert a file that is already written. - Never trust the agent's summary; read every hunk of the diff and run
./gradlew testand./gradlew assembleReleaseyourself. - Prefer small, single-screen tasks over one giant migration prompt; verify with
./gradlew assembleDebugafter each step. - Prefer a
matcheron every hook group so it does not fire on reads, searches, or unrelated tool calls.
Gotchas
- Putting a 2000-line architecture doc in CLAUDE.md. It is loaded into every session's context window, burning tokens and pushing out the code the agent actually needs. Move detail into skills that load on demand; keep
CLAUDE.mdunder ~200 lines. - No verification command in CLAUDE.md or a skill. The agent cannot confirm its edit compiles and produces plausible-looking Kotlin that fails the build. Always list
./gradlew testor./gradlew lintso the agent can check its own work. - 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 2to prevent the edit from landing. - Pasting an API key or keystore password into a prompt or .mcp.json. The key lands in the session log and the agent's context, and can be written to a file in a later turn. Use
${VAR}expansion from a local environment variable or a secrets manager. - Skipping the matcher in a hook entry. Without a
matcherthe hook fires on every tool call including reads and searches, adding latency to every step. Scope it to the tools you care about, e.g.Edit|Write. - Trusting a green build without reading the diff. Code compiles and tests pass but the logic is wrong: a
ViewModelmay hold aContext, aFlowmay not be collected. Read every hunk, check nullability and lifecycle handling, and runassembleReleaseto catch R8 issues.
Quick interview answers
What is the difference between CLAUDE.md and a skill in Claude Code?
CLAUDE.md is always loaded into every session's context window, so it should hold only project-level facts like build commands and hard rules. A skill in .claude/skills/<name>/SKILL.md loads on demand when the agent matches the description field to the user's task, keeping the context window small.
How do you prevent an AI agent from editing keystore.jks or local.properties?
Add a PreToolUse hook in .claude/settings.json with a matcher of Edit|Write that runs a shell script exiting with code 2 when the target path matches *.jks, *.keystore, or local.properties. Permission deny rules work too. A rule written in CLAUDE.md is only context the agent can ignore; a hook or deny rule is a hard guardrail.
What is an MCP server and how does it differ from a skill?
An MCP server is a callable external tool (Jira, Figma, a Gradle runner) that the agent invokes like a function over JSON-RPC 2.0, configured in .mcp.json. A skill is a folder of instructions (SKILL.md) that tells the agent what to do; a skill can tell the agent when to call an MCP tool, but the two are different layers.
Why must you review every AI-generated diff even when the build is green?
A green build means the code compiles, but the logic can still be wrong: a ViewModel may hold a Context reference, a Flow may not be collected with repeatOnLifecycle, or a proguard-rules.pro change may break the release build. Read every hunk, run ./gradlew test and ./gradlew assembleRelease, and check that no signing or secret file was touched.
How do you share an AI-agent setup across a team?
Bundle skills, hooks, subagents, and MCP servers into a single plugin that a teammate installs with the /plugin command instead of copying files by hand. The plugin keeps everyone on the same guardrails, verification steps, and tool access.
What should a SKILL.md description field contain?
It should be a concrete trigger sentence that tells the model exactly when to load the skill, for example 'Use when the user asks to run ./gradlew test or debug a failing CI task.' A vague description like 'Android tips' matches almost everything or nothing, so the skill either loads too often or never.