Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Public API management tool

License

NotificationsYou must be signed in to change notification settings

Kotlin/binary-compatibility-validator

Kotlin AlphaJetBrains official projectMaven CentralLicenseKDoc link

Important

This plugin is in maintenance mode.

Fixes for critical bugs and support for current versions of Kotlin continue to be delivered.

The implementation of new features has been stopped in favor of adding them to theexperimental binary compatibility validation in the Kotlin Gradle plugin

Binary compatibility validator

The tool allows dumping binary API of a JVM part of a Kotlin library that is public in the sense of Kotlin visibilities and ensures that the public binary API wasn't changed in a way that makes this change binary incompatible.

Contents

Requirements

Binary compatibility validator plugin requires Gradle6.1.1 or newer.

Kotlin version1.6.20 or newer.

Setup

Binary compatibility validator is a Gradle plugin that can be added to your build in the following way:

  • inbuild.gradle.kts
plugins {    id("org.jetbrains.kotlinx.binary-compatibility-validator") version"0.18.1"}
  • inbuild.gradle
plugins {    id'org.jetbrains.kotlinx.binary-compatibility-validator' version'0.18.1'}

It is enough to apply the plugin only to the root project build file; all sub-projects will be configured automatically.

Tasks

The plugin provides two tasks:

  • apiDump — builds the project and dumps its public API in projectapi subfolder.API is dumped in a human-readable format. If API dump already exists, it will be overwritten.
  • apiCheck — builds the project and checks that project's public API is the same as golden valuein projectapi subfolder. This task is automatically inserted intocheck pipeline, so bothbuild andchecktasks will start checking public API upon their execution.

For projects with multiple JVM targets, multiple subfolders will be created, e.g.api/jvm andapi/android

Optional parameters

Binary compatibility validator can be additionally configured with the following DSL:

Groovy

apiValidation {/**     * Packages that are excluded from public API dumps even if they     * contain public API.*/    ignoredPackages+= ["kotlinx.coroutines.internal"]/**     * Sub-projects that are excluded from API validation*/    ignoredProjects+= ["benchmarks","examples"]/**     * Classes (fully qualified) that are excluded from public API dumps even if they     * contain public API.*/    ignoredClasses+= ["com.company.BuildConfig"]/**     * Set of annotations that exclude API from being public.     * Typically, it is all kinds of `@InternalApi` annotations that mark     * effectively private API that cannot be actually private for technical reasons.*/    nonPublicMarkers+= ["my.package.MyInternalApiAnnotation"]/**     * Flag to programmatically disable compatibility validator*/    validationDisabled=true/**     * A path to a subdirectory inside the project root directory where dumps should be stored.*/    apiDumpDirectory="api"}

Kotlin

apiValidation {/**     * Packages that are excluded from public API dumps even if they     * contain public API.*/    ignoredPackages.add("kotlinx.coroutines.internal")/**     * Sub-projects that are excluded from API validation*/    ignoredProjects.addAll(listOf("benchmarks","examples"))/**     * Classes (fully qualified) that are excluded from public API dumps even if they     * contain public API.*/    ignoredClasses.add("com.company.BuildConfig")/**     * Set of annotations that exclude API from being public.     * Typically, it is all kinds of `@InternalApi` annotations that mark     * effectively private API that cannot be actually private for technical reasons.*/    nonPublicMarkers.add("my.package.MyInternalApiAnnotation")/**     * Flag to programmatically disable compatibility validator*/    validationDisabled=false/**     * A path to a subdirectory inside the project root directory where dumps should be stored.*/    apiDumpDirectory="aux/validation"}

Producing dump of a jar

By default, binary compatibility validator analyzes project output class files frombuild/classes directory when building an API dump.If you pack these classes into an output jar not in a regular way, for example, by excluding certain classes, applyingshadow plugin, and so on,the API dump built from the original class files may no longer reflect the resulting jar contents accurately.In that case, it makes sense to use the resulting jar as an input of theapiBuild task:

Kotlin

tasks {    apiBuild {// "jar" here is the name of the default Jar task producing the resulting jar file// in a multiplatform project it can be named "jvmJar"// if you applied the shadow plugin, it creates the "shadowJar" task that produces the transformed jar        inputJar.value(jar.flatMap { it.archiveFile })    }}

Workflow

When starting to validate your library public API, we recommend the following workflow:

  • Preparation phase (one-time action):

    • As the first step, apply the plugin, configure it and executeapiDump.
    • Validate your public API manually.
    • Commit.api files to your VCS.
    • At this moment, defaultcheck task will validate public API along with test run and will failthe build if API differs.
  • Regular workflow

    • When doing code changes that do not imply any changes in public API, no additionalactions should be performed.check task on your CI will validate everything.
    • When doing code changes that imply changes in public API, whether it is a new API oradjustments in existing one,check task will start to fail.apiDump should be executed manually,the resulting diff in.api file should be verified: only signatures you expected to change should be changed.
    • Commit the resulting.api diff along with code changes.

Experimental KLib ABI validation support

The KLib validation support is experimental and is a subject to change (applies to both an API and the ABI dump format).A project has to use Kotlin 1.9.20 or newer to use this feature.

To validate public ABI of a Kotlin library (KLib) corresponding option should be enabled explicitly:

apiValidation {    @OptIn(kotlinx.validation.ExperimentalBCVApi::class)    klib {        enabled=true    }}

When enabled, KLib support adds additional dependencies to existingapiDump andapiCheck tasks.Generate KLib ABI dumps are places alongside JVM dumps (inapi subfolder, by default)in files named<project name>.klib.api.The dump file combines all dumps generated for individual targets with declarations specific to some targets beingannotated with corresponding target names.During the validation phase, that file is compared to the dump extracted from the latest version of the library,and any differences between these two files are reported as errors.

Generated ABI dumps include a library name,so it'srecommendedto set Gradle'srootProject.name for your library.Without declaring the root project's name, Gradle defaults to using the project's directory name, which can lead tounstable validation behavior due to potential mismatches in naming.

Currently, all options described inOptional parameters section are supported for klibs too.The only caveat here is that all class names should be specified in the JVM-format,likepackage.name.ClassName$SubclassName.

Please refer to adesign document for details on the format and rationale behind thecurrent implementation.

KLib ABI dump generation and validation on Linux and Windows hosts

Currently, compilation to Apple-specific targets (likeiosArm64 orwatchosX86) supported only on Apple hosts.To ease the development on Windows and Linux hosts, binary compatibility validator does not validate ABI for targetsnot supported on the current host, even if.klib.api file contains declarations for these targets.

This behavior could be altered to force an error when klibs for some targets could not be compiled:

apiValidation {    @OptIn(kotlinx.validation.ExperimentalBCVApi::class)    klib {        enabled=true// treat a target being unsupported on a host as an error        strictValidation=true    }}

When it comes to dump generation (apiDump task) on non-Apple hosts, binary compatibility validator attemptsto infer an ABI from dumps generated for supported targets and an old dump from project'sapi folder (if any).Inferred dump may not match an actual dump,and it is recommended to update a dump on hosts supporting all required targets, if possible.

What constitutes the public API

Classes

A class is considered to be effectively public if all the following conditions are met:

  • it has public or protected JVM access (ACC_PUBLIC orACC_PROTECTED)
  • it has one of the following visibilities in Kotlin:
    • no visibility (means no Kotlin declaration corresponds to this compiled class)
    • public
    • protected
    • internal, only in case if the class is annotated withPublishedApi
  • it isn't a local class
  • it isn't a synthetic class with mappings forwhen tableswitches ($WhenMappings)
  • it contains at least one effectively public member, in case if the class correspondsto a kotlinfile with top-level members or amultifile facade
  • in case if the class is a member in another class, it is contained in theeffectively public class
  • in case if the class is a protected member in another class, it is contained in thenon-final class

Members

A member of the class (i.e. a field or a method) is considered to be effectively publicif all the following conditions are met:

  • it has public or protected JVM access (ACC_PUBLIC orACC_PROTECTED)

  • it has one of the following visibilities in Kotlin:

    • no visibility (means no Kotlin declaration corresponds to this class member)
    • public
    • protected
    • internal, only in case if the class is annotated withPublishedApi

    Note that Kotlin visibility of a field exposed bylateinit property is the visibility of its setter.

  • in case if the member is protected, it is contained innon-final class

  • it isn't a synthetic access method for a private field

What makes an incompatible change to the public binary API

Class changes

For a class a binary incompatible change is:

  • changing the full class name (including package and containing classes)
  • changing the superclass, so that the class no longer has the previous superclass inthe inheritance chain
  • changing the set of implemented interfaces so that the classno longer implements interfaces it had implemented before
  • changing one of the following access flags:
    • ACC_PUBLIC,ACC_PROTECTED,ACC_PRIVATE — lessening the class visibility
    • ACC_FINAL — making non-final class final
    • ACC_ABSTRACT — making non-abstract class abstract
    • ACC_INTERFACE — changing class to interface and vice versa
    • ACC_ANNOTATION — changing annotation to interface and vice versa

Class member changes

For a class member a binary incompatible change is:

  • changing its name
  • changing its descriptor (erased return type and parameter types for methods);this includes changing field to method and vice versa
  • changing one of the following access flags:
    • ACC_PUBLIC,ACC_PROTECTED,ACC_PRIVATE — lessening the member visibility
    • ACC_FINAL — making non-final field or method final
    • ACC_ABSTRACT — making non-abstract method abstract
    • ACC_STATIC — changing instance member to static and vice versa

Building the project locally

In order to build and run tests in the project in IDE, two prerequisites are required:

  • Java 11 or above in order to use the latest ASM
  • All build actions in the IDE should be delegated to Gradle

Contributing

Read theContributing Guidelines.

About

Public API management tool

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors32


[8]ページ先頭

©2009-2025 Movatter.jp