Your first architecture definition
If you are not yet convinced that you need katachi, read Motivation first.
Add katachi to your project
Section titled “Add katachi to your project”-
Start the AI agent you use and give it this prompt.
Set up katachi in this project.- Follow https://tbsten.github.io/katachi/install/en/index.md to do it.- Putting the project under git first is recommended.
- The page the AI agent reads tells it to set katachi up together with you.
Additional options
- If your architecture documentation lives outside the codebase (in Notion, for example), include a link to it in the prompt.
- By default the AI agent creates an
architecture-testmodule and writes the architecture definition there.- To use a different module, add a prompt such as
Create the test in <module>.
- To use a different module, add a prompt such as
- As a matter of context hygiene, start a fresh session dedicated to setting up katachi.
- For Claude Code or Codex CLI: clear the session with
/clear, then enter the prompt.
- For Claude Code or Codex CLI: clear the session with
-
Review the changes the AI agent made to your project and adjust them as needed.
What to look at:
architecture-test/src/test/kotlin/<package>/ProjectArchitecture.ktis the entry point.- The AI agent splits the architecture definition into one file per group and per Role, and puts them in packages under
ProjectArchitecture.kt. Review those files too. - If you work in a team, have your teammates (especially whoever leads it) review this closely as well.
-
Run the architecture test and confirm that it is all green.
Terminal window ./gradlew :architecture-test:testThat completes the setup.
-
Create the
:architecture-testmoduleThis module belongs to no layer of the app. It holds nothing but the architecture definition and the test that checks it.
settings.gradle.kts pluginManagement {repositories {gradlePluginPortal()mavenCentral()}}include(":architecture-test")katachi’s Gradle plugin is published to Maven Central, not to the Gradle Plugin Portal. Without
mavenCentral()inpluginManagement { }, the plugin cannot be resolved. -
Declare the dependencies
Whether your project is Android or KMP, make this a plain
kotlin("jvm")module, because katachi itself is a JVM library.architecture-test/build.gradle.kts import org.gradle.api.tasks.testing.logging.TestExceptionFormatplugins {kotlin("jvm") version "<Kotlin version>"id("me.tbsten.katachi") version "<katachi version>"}kotlin {jvmToolchain(17)}tasks.test {useJUnitPlatform()// The files being checked are not inputs of the Test task, so run it every time (and keep its result out of the build cache)outputs.upToDateWhen { false }outputs.cacheIf { false }// Print the list of violations to the consoletestLogging { exceptionFormat = TestExceptionFormat.FULL }}dependencies {testImplementation("me.tbsten.katachi:katachi:<katachi version>")testImplementation("me.tbsten.katachi:katachi-konsist:<katachi version>")testImplementation(platform("org.junit:junit-bom:5.14.4"))testImplementation("org.junit.jupiter:junit-jupiter")testRuntimeOnly("org.junit.platform:junit-platform-launcher")}katachi {architecture = "com.example.projectArchitecture"}outputs.upToDateWhen { false }/outputs.cacheIf { false }: katachi checks the files of the whole repository, but Gradle only sees the test classpath as the Test task’s input. Without this line,:architecture-test:testis skipped asUP-TO-DATEeven after you add or change files, and stays green even when there are violations.cacheIfkeeps a previous result from being reused when the build cache is enabled.testLogging { exceptionFormat = TestExceptionFormat.FULL }: without it, a failing test prints only a single line with the exception’s class name to the console, and you have to open the test report to read the list of violations.katachi { architecture = ... }: tells document generation and templates where to find the definition. Set it to the fully qualified name of the definition you write in step 3 (package+ variable name).
Kotlin version (2.2 or later; below 2.4 a compiler option is needed)
Use Kotlin 2.2 or later. Earlier Kotlin versions cannot read the metadata of katachi’s libraries.
ktFile(),konsist { }and others are declared with context parameters. Below Kotlin 2.4 they cannot be called without the following option (do not add it on 2.4 or later; it causes a warning).architecture-test/build.gradle.kts kotlin {compilerOptions.freeCompilerArgs.add("-Xcontext-parameters")} -
Define the architecture.
architecture-test/src/test/kotlin/<package>/ProjectArchitecture.ktis the entry point.// Example@file:OptIn(ExperimentalKatachiApi::class)package com.exampleimport me.tbsten.katachi.ExperimentalKatachiApiimport me.tbsten.katachi.dsl.architectureimport me.tbsten.katachi.dsl.gradle.gradleimport me.tbsten.katachi.dsl.kotlin.ktFileval projectArchitecture = architecture {gradle() // Gradle's files: the wrapper, settings, build scripts and so on"domain".group {"UseCase" {summary = "A single app-specific behavior that occurs on a screen"layout {"domain/src/main/kotlin/usecase" / "*UseCase".ktFile()}}}} -
Write the one test that checks it
architecture-test/src/test/kotlin/<package>/ProjectArchitectureTest.kt @file:OptIn(ExperimentalKatachiApi::class)package com.exampleimport me.tbsten.katachi.ExperimentalKatachiApiimport me.tbsten.katachi.check.assertimport org.junit.jupiter.api.Testclass ProjectArchitectureTest {@Testfun `the project structure matches the definition`() {projectArchitecture.assert()}}This is the only test your project writes. Every violation in the repository lands in a single failure message, so there is no point in splitting the test up per rule.
If your definition has
konsist { }, callassert(FileConstraintCheck())instead ofassert(). Withassert()alone the constraints are not evaluated and the test fails. See Konsist integration for details. -
Run it
Terminal window ./gradlew :architecture-test:testAt first the files and directories you have not declared come out as violations. katachi is an allow-list, so this is the normal starting point. Go through the reported paths and decide, one by one, whether to add them to the definition or delete them.
Gradle’s files (the wrapper,
settings.gradle.kts, build scripts,gradle.propertiesand the version catalog) are declared by the singlegradle()line in step 3. If you havebuildSrcor convention plugins, see the Gradle recipe.If there are too many existing violations to clear up right away, the experimental baseline feature lets you tolerate the existing violations for now and fail only on new ones. See Baseline for details.
Confirm that the setup works
Section titled “Confirm that the setup works”Hand the AI agent a real task and see that it no longer produces files that break the architecture rules.
If you have implementation tasks coming up
If you have tasks that are actually coming up, running one of them as a trial is the best way to measure whether katachi does its job. Hand the task to the AI agent and let it run.
If the task is tracked in JIRA, Notion, or similar, passing those along works well too.
If you have nothing in particular
Have the AI agent invent a hypothetical task and check whether katachi works as a guardrail.
Come up with a few hypothetical tasks that are likely to come up in this project.- Think of a piece of code that breaks an architecture rule and actually create that source file.
- Run
./gradlew :architecture-test:testand confirm that the architecture test fails.
Optional
Wire it into CI
Section titled “ Wire it into CI”To keep the quality up, run the katachi architecture test regularly by wiring it into CI.
Add a workflow file like this one.
name: Architecture
on: push: branches: [main] pull_request:
permissions: contents: read
jobs: architecture: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7
- uses: actions/setup-java@v6 with: distribution: temurin java-version: '17'
- uses: gradle/actions/setup-gradle@v6
- run: ./gradlew :architecture-test:testDescribe a workflow like the following, in whatever form your CI system expects.
- Set up Java.
- Run
./gradlew :architecture-test:test. - Report the result of step 2.
Next steps
Section titled “Next steps”- Read the guides to deepen your understanding of katachi and refine your architecture definition.