liveBook · Manning

Explicit API mode

As explained in Kotlin in Action, 2E

Explicit API mode is a Kotlin compiler mode that requires explicitly declared public API types and related signatures.

Explicit API mode makes public Kotlin APIs deliberate: declarations must state their visibility and public properties or functions must state their types. This helps library authors avoid accidental exposure and later breaking changes. section 4.1.3

Declare a public API explicitly

Enable the mode with `-Xexplicit-api=strict` or `-Xexplicit-api=warning`. Then write public declarations with both parts visible: `public fun greet(name: String): String = "Hello, $name"`. Kotlin otherwise makes unspecified visibility public, unlike Java's package-private default. section 4.1.3

Explicit API mode checks:

  • Visibility for declarations exposed as public API. section 4.1.3
  • Explicit types for public properties and functions. section 4.1.3
  • Strict or warning enforcement through the compiler or build system. section 4.1.3
Table 4.2 Kotlin visibility modifiers (view table figure)

Modifier

Class member

Top-level declaration

public (default)

Visible everywhere

Visible everywhere

internal

Visible in a module

Visible in a module

protected

Visible in subclasses

—

private

Visible in a class

Visible in a file

Use the visibility table to compare public, internal, protected, and private boundaries when deciding which declarations require explicit API treatment. section 4.1.3

How Kotlin in Action, 2E covers it

How deep it goes
Defines required declarations and configuration modes but stops short of detailed build-system syntax.
Examples
Uses public API declarations and the `-Xexplicit-api` compiler option as practical examples.
What it assumes
Assumes basic Kotlin declarations and visibility modifiers while contrasting Kotlin defaults with Java conventions.
What you can do after
Configure Kotlin compilation to enforce explicit public API visibility and signature types.
How it connects
Connects public visibility, internal visibility, visibility modifiers, and Java package-private visibility.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.