# Binary compatibility validation in the Kotlin Gradle plugin Binary compatibility validation helps library authors ensure that users don't break their code when upgrading to newer versions. It's important not only for delivering a smooth upgrade experience, but also for building long-term trust with users and encouraging continued adoption of the library. Tip: Binary compatibility means that the compiled bytecode of two versions of a library can run interchangeably without needing recompilation. The Kotlin Gradle plugin includes support for binary compatibility validation. The plugin generates Application Binary Interface (ABI) dumps from the current code and compares them with previous dumps to highlight differences. You can review these changes to find any potentially binary-incompatible modifications and take action to address them. ## How to enable To enable binary compatibility validation, in your `build.gradle.kts` file, add an `abiValidation {}` block. If you have no custom configuration, you can use the `abiValidation()` function instead: Kotlin: ```KOTLIN kotlin { @OptIn(org.jetbrains.kotlin.gradle.dsl.abi.ExperimentalAbiValidation::class) abiValidation() } ``` Groovy: ```GROOVY kotlin { abiValidation() } ``` The KGP creates the necessary Gradle tasks. If your project has multiple modules where you want to check for binary compatibility, configure each module separately. ## Check for binary compatibility issues To check for potentially binary incompatible issues after making changes to your code, run the `checkKotlinAbi` Gradle task in IntelliJ IDEA or use the following command in your project directory: ```BASH ./gradlew checkKotlinAbi ``` The task compares ABI dumps and prints any detected differences as errors. Check the output carefully to see if you need to make changes to your code to preserve binary compatibility. By default, [when binary compatibility validation is enabled](#how-to-enable) in your project and you run the `check` task, Gradle also runs the `checkKotlinAbi` task. ## Update reference ABI dump To update the reference ABI dump that Gradle uses to check your latest changes, run the `updateKotlinAbi` task in IntelliJ IDEA or use the following command in your project directory: ```BASH ./gradlew updateKotlinAbi ``` Only update the reference dump when you're confident your changes maintain binary compatibility with the previous version. ## Configure filters You can define filters to control which classes, properties, and functions the ABI dump includes. Use the `filters {}` block to add exclusion and inclusion rules with `excluded {}` and `included {}` blocks respectively. Gradle includes a declaration in the ABI dump only if it doesn't match any exclusion rule. When inclusion rules are defined, the declaration must either match one of them or have at least one member that does. A rule can be based on: * The fully qualified name of a class, property, or function (`byNames`). * The name of an annotation that has BINARY or RUNTIME [retention](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.annotation/-retention/) (`annotatedWith`). Tip: You can use wildcards `**`, `*`, and `?` in your rules for names: * `**` matches zero or more characters, including periods. * `*` matches zero or more characters excluding periods. Use this to specify a single class name. * `?` matches exactly one character. For example: Kotlin: ```KOTLIN kotlin { @OptIn(org.jetbrains.kotlin.gradle.dsl.abi.ExperimentalAbiValidation::class) abiValidation { filters { excluded { byNames.add("**.InternalUtils") annotatedWith.add("com.example.annotations.InternalApi") } included { byNames.add("com.example.api.**") annotatedWith.add("com.example.annotations.PublicApi") } } } } ``` Groovy: ```GROOVY kotlin { abiValidation { filters { excluded { byNames.add("**.InternalUtils") annotatedWith.add("com.example.annotations.InternalApi") } included { byNames.add("com.example.api.**") annotatedWith.add("com.example.annotations.PublicApi") } } } } ``` This example: * Excludes: * The `InternalUtils` class. * Declarations annotated with `@InternalApi`. * Includes: * Everything in the `com.example.api` package. * Declarations annotated with `@PublicApi`. To learn more about filtering, see the [Kotlin Gradle plugin API reference](https://kotlinlang.org/api/kotlin-gradle-plugin/kotlin-gradle-plugin-api/org.jetbrains.kotlin.gradle.dsl.abi/-abi-filters-spec/). ## Prevent inferred changes for unsupported targets In multiplatform projects, if your host system can't compile all targets, the Kotlin Gradle plugin tries to infer ABI changes from the available targets. This helps avoid false failures when you later switch to a host that supports more targets. To disable this behavior, add the following to your `build.gradle.kts` file: Kotlin: ```KOTLIN kotlin { @OptIn(org.jetbrains.kotlin.gradle.dsl.abi.ExperimentalAbiValidation::class) abiValidation { keepLocallyUnsupportedTargets.set(false) } } ``` Groovy: ```GROOVY kotlin { abiValidation { keepLocallyUnsupportedTargets = false } } ``` If a target is unsupported and inference is disabled, the `checkKotlinAbi` task fails because it can't generate a complete ABI dump. This behavior may be useful if you'd prefer the task to fail rather than risk missing a binary-incompatible change. ## Include publications from the `maven-publish` plugin By default, binary compatibility validation uses Kotlin compilation outputs to generate ABI dumps. For this reason, the generated ABI dumps might not reflect the final published artifacts. For example, when you use the [maven-publish plugin](https://docs.gradle.org/current/userguide/publishing_maven.html), post-processing steps such as relocation can modify artifacts after compilation. To make sure the ABI dumps accurately reflect the artifacts published by the `maven-publish` plugin, add the following to your `build.gradle.kts` file: Kotlin: ```KOTLIN kotlin { @OptIn(org.jetbrains.kotlin.gradle.dsl.abi.ExperimentalAbiValidation::class) abiValidation { binariesSource.set(MAVEN_PUBLICATIONS) } } ``` Groovy: ```GROOVY kotlin { abiValidation { binariesSource = MAVEN_PUBLICATIONS } } ``` Warning: Because Kotlin/Android projects and multiplatform projects with an Android target don't publish JAR files, this feature doesn't apply to them.