Invite a friend, get 1 month of Pro freeHow it works

Claude Code for Kotlin and Android: Setup and Workflow

Use Claude Code for Kotlin and Android development: set up CLAUDE.md, plan small diffs, run Gradle tests, and audit Compose and coroutine changes.

Solviera Teknoloji 5 min read

Claude Code is a terminal-based coding agent that works well with Kotlin and Android when you give it a precise CLAUDE.md and a tight review loop. This guide covers setup, prompts, common tasks, testing, and running multiple agents for Jetpack Compose, Gradle, coroutines, JUnit, and Espresso projects.

Where Claude Code earns its keep in Android codebases

Claude Code reads the whole Gradle project including build.gradle.kts, libs.versions.toml, and source set directories. It can run gradlew tasks and parse compiler, test, and lint output. Android code has many conventions: Compose state hoisting, ViewModel scopes, coroutines, Hilt, data binding, and version catalogs. Claude Code handles cross-file refactors that would take hours manually, such as renaming a repository interface across all modules, updating test fixtures, or converting a screen from XML to Compose. It is not a replacement for Android Studio, but it is a fast pair programmer in the terminal when you use plan mode and small diffs. However, it needs explicit instructions because Gradle tasks are slow, instrumented tests require an emulator, and Compose previews sometimes fail without a device context.

Handing Claude Code a useful CLAUDE.md

Create a CLAUDE.md in the project root. The file is read when you start claude. It should list build, test, lint commands, module layout, dependency versioning, and Kotlin- or Android-specific rules. Keep it short and actionable; long files dilute the agent’s attention.

# CLAUDE.md

## Build and test
- Build: ./gradlew assembleDebug
- Unit tests: ./gradlew testDebugUnitTest
- Instrumented tests: ./gradlew connectedDebugAndroidTest
- Lint: ./gradlew lintDebug

## Project layout
- Single app module `app`, feature modules in `features/`
- UI in `app/src/main/java/.../ui/` with Compose
- ViewModel in `.../viewmodel/`
- Coroutines use `viewModelScope` and `Dispatchers.IO` for data sources
- Tests: unit tests in `src/test`, instrumented Compose tests in `src/androidTest`

## Rules
- Target SDK 35, minSdk 24, Kotlin 2.0, Compose BOM 2024.09.03
- Use version catalog `libs.versions.toml` for dependencies; do not add versions in build.gradle.kts
- Follow official Kotlin style: trailing commas, named arguments for Compose previews
- Never modify generated files in `build/`
- For Compose UI tests, use `createAndroidComposeRule<MainActivity>()` and `onNodeWithTag`; avoid hardcoded sleep
- When changing coroutine code, always add or update a unit test with `runTest`

## Common tasks
- To add a feature screen, create UI, ViewModel, and tests following existing patterns in `features/home/`
- Refactor with `git diff` before and after; keep changes small

Start the agent with claude in the project root. If you have multiple modules, add a one-line note about each module’s responsibility. Avoid putting generated boilerplate in CLAUDE.md; the agent can read build files itself. Update the file when you add a module or change lint rules.

A repeatable review loop for Kotlin changes

The loop that works best is: plan, small steps, run tests, review the diff. Use plan mode so the agent proposes files and commands before editing. Then make one change at a time, run the unit tests, and check the git diff. For Android, run unit tests first because instrumented tests need a device. Then run Gradle lint.

Example prompts:

  • Add a Compose screen that shows a list of users from a repository using StateFlow and collectAsStateWithLifecycle. Include a ViewModel and unit tests.
  • Refactor this ViewModel to use viewModelScope instead of GlobalScope, and add runTest tests for the two main state transitions.
  • Fix the failing Espresso test in HomeScreenTest. Analyze the failure from the connectedDebugAndroidTest report and suggest a minimal change.
  • Migrate this module from XML layouts to Compose for the detail screen only. Keep the existing ViewModel and data layer unchanged.
  • Review the current diff for Kotlin and Android issues, run unit tests, and report before changing anything.

After the agent responds, do not accept blindly. Check for common mistakes: using Dispatchers.Main in tests without a rule, forgetting to import Compose preview annotations, or adding dependencies outside the version catalog. Use git diff to review only what changed.

Strong Kotlin tasks and the pitfalls to audit

Claude Code is strong at mechanical Kotlin refactors, updating Compose UI, writing test data builders, and converting XML to Compose. It can also diagnose Gradle sync issues if you paste the error.

TaskWhat to askWatch for
Refactor ViewModel statePartition into a UiState data class and expose a single StateFlowAvoid introducing unnecessary flows or losing one-shot events
Compose screen from specCreate composables with previews and stable parametersThe agent may use unstable params, causing recomposition; check with layout inspector
Test migrationConvert JUnit4 tests to Kotlin and runTest with a MainDispatcherRuleDo not let it use GlobalScope or real dispatchers in tests
Gradle dependency updateBump a version in libs.versions.toml and run lintIt may update multiple dependencies at once and break transitive compatibility
Fix lint errorsApply suggestions for UnusedMaterial3ScaffoldPaddingParameter or similarAvoid adding global suppressions; fix the actual issue

Watch closely when the agent edits Gradle version catalogs, because it may bump multiple dependencies at once and break transitive compatibility. Compose code needs structure: stable values, immutable models, and state hoisting; the agent may generate code that compiles but recomposes excessively. For coroutines, ensure it does not use GlobalScope or blocking calls in composables. Android-specific generated files like R class or BuildConfig are off-limits; tell the agent never to edit them. Instrumented tests can be flaky; do not let the agent add sleeps to fix races.

Running JUnit, Espresso, and lint through the agent

Use the agent to run tests after each change. Unit tests run with ./gradlew testDebugUnitTest; for coroutine code, use runTest and a MainDispatcherRule. Instrumented tests run with ./gradlew connectedDebugAndroidTest and require a running emulator or device; check adb devices. For Compose UI tests, use createAndroidComposeRule and onNodeWithTag or semantics matchers, not sleeps. Have the agent inspect reports under app/build/reports/ and fix failures. Lint: ./gradlew lintDebug before commit. The agent can also add CI-like verification by running the same commands. But be explicit: do not run connected tests if no device. Use unit tests for ViewModel and repository logic; reserve Espresso for critical navigation and UI flows. If the agent claims a test passed, ask it to paste the command output or the report path.

Parallel agents with AgentVera for Android repos

AgentVera runs Claude Code and other CLI agents side by side in real terminals. For Kotlin and Android work, the most useful features are git worktrees so one agent can refactor Compose while another migrates tests without overwriting each other; automatic code review after each turn, which catches coroutine and recomposition mistakes with file:line links; and token budgets that stop runaway Gradle iterations before they burn quota. Checkpoints also allow undoing a bad ViewModel change with one click. For details on running multiple Claude Code agents, see How to run multiple Claude Code agents in parallel.

Bottom line for Kotlin and Android teams

Claude Code becomes a reliable Kotlin and Android pair programmer when you invest in a good CLAUDE.md, keep changes small, and verify with Gradle tests and lint. Use plan mode for risky refactors, and consider AgentVera for parallel worktrees and automatic review. The terminal agent cannot replace Android Studio for visual previews or device debug, but it saves time on repetitive code and test writing.

Questions

Can Claude Code work on Kotlin and Android projects without a CLAUDE.md?

Yes, it can often infer basic Gradle commands, but a CLAUDE.md with build, test, lint commands and module layout greatly reduces wrong assumptions and wasted runs. For Compose, coroutines and multi-module projects, explicit instructions are the difference between a useful agent and a confused one.

How do I get Claude Code to run Android Gradle tests and lint correctly?

Put the exact commands in CLAUDE.md, such as ./gradlew testDebugUnitTest, connectedDebugAndroidTest and lintDebug. Tell the agent to check for a running emulator before connected tests, and to inspect reports under app/build/reports. For unit tests, use runTest and a Main dispatcher rule.

Does Claude Code understand Jetpack Compose and Kotlin coroutines?

It reads and writes Kotlin and Compose fluently, including composables, StateFlow, viewModelScope and previews. However, it can still make mistakes like using Dispatchers.Main in tests without a rule or creating unnecessary recompositions; always review the diff and run tests.

How can I prevent Claude Code from breaking Gradle builds or generated files?

In CLAUDE.md, state that generated files in build/ must never be edited, dependencies go only through libs.versions.toml, and each change must be followed by ./gradlew assembleDebug or testDebugUnitTest. Keep changes small and use git diff to review before accepting.

Bring your agents to one desk.

Download AgentVera for free; your installed CLIs are ready to go.

More Claude Code guides

Other agents for Kotlin and Android