@ConsistentCopyVisibility in Kotlin: Making Data Class copy() Respect a Private Constructor
@ConsistentCopyVisibility makes the generated copy() function of a Kotlin data class take the same visibility as its primary constructor. If the constructor is private, copy() becomes private too, so callers can no longer use copy() to build an instance that skips your factory and its validation. Newer Kotlin versions also warn about data classes that still expose a public copy() next to a non-public constructor, and this behavior is planned to become the default.
The problem: a private constructor that copy() ignores
A common pattern is to hide the constructor of a data class and force everyone through a factory that checks the input. It looks safe, but the compiler still generates a public copy() with the same parameters as the constructor:
data class Email private constructor(val value: String) {
companion object {
fun of(raw: String): Email? =
raw.trim().takeIf { it.contains("@") }?.let { Email(it) }
}
}
val ok = Email.of("dev@example.com")!!
val broken = ok.copy(value = "not an email") // compiles, no check runsThe factory rejects bad input, but copy() builds a new Email with any string you pass. The private constructor gives a false sense of safety: the invariant you wanted to protect can be broken from anywhere in the codebase, and nothing in code review makes it obvious.
What @ConsistentCopyVisibility changes
Put the annotation on the data class and the generated copy() follows the constructor's visibility:
@ConsistentCopyVisibility
data class Email private constructor(val value: String) {
companion object {
fun of(raw: String): Email? =
raw.trim().takeIf { it.contains("@") }?.let { Email(it) }
}
// copy() is private now; this function validates first, then calls it
fun withValue(raw: String): Email? = of(raw)?.let { copy(value = it.value) }
}
val ok = Email.of("dev@example.com")!!
ok.copy(value = "x") // error: copy() is private in Email
ok.withValue("new@example.com") // fine, goes through the same checkThe same rule applies to other visibilities. An internal constructor gives you an internal copy(), so the class can be copied freely inside its module but not from other modules. The rest of the data class stays the same: equals(), hashCode(), toString() and the componentN() functions used for destructuring declarations are generated as before.
If you actually need the old behavior, there is an opposite annotation, @ExposedCopyVisibility. It keeps copy() public and silences the warning on that class. Treat it as a temporary marker for classes you have not migrated yet, or for public library APIs where removing copy() would break users.
Opt in per class or with a compiler flag for the whole module
You have two ways to turn the new behavior on. The annotation works class by class, which is the easiest way to start: add it to one data class, fix the call sites the compiler points at, then move to the next one.
For a whole module, use the compiler flag instead. In a Gradle Kotlin DSL build it looks like this:
kotlin {
compilerOptions {
freeCompilerArgs.add("-Xconsistent-data-class-copy-visibility")
}
}With the flag set, every data class in that module with a non-public constructor gets a matching copy() visibility, and you do not need the annotation anymore. In an Android project with several modules, the flag can go into a shared convention plugin so app and feature modules behave the same way.
A practical order: start with the annotation on a few classes to see how many call sites break, then switch to the flag once the module compiles cleanly with it. In Kotlin Multiplatform projects, add the flag to the shared module's own build file as well, since that module is compiled with its own settings.
Migration tips for existing data classes and factory functions
Find the affected classes. Build the project and collect the warnings about a non-public primary constructor exposed through
copy(). Each warning is a data class you need to decide on.Look at how
copy()is used for each class. If nobody outside the class calls it, add@ConsistentCopyVisibilityand you are done.If outside code needs to change one field, add a named function inside the class, like
withValue()above, that validates the input first and then calls the privatecopy(). Point callers to it.If the constructor is
internaland only code in the same module callscopy(), the annotation changes nothing for those callers. Only callers in other modules break.For a public library, do not make
copy()private in a minor release. Mark the class with@ExposedCopyVisibility, deprecate directcopy()usage in your docs, and change the visibility in a release where breaking changes are expected.Once all classes in a module are handled, replace the per-class annotations with the compiler flag so new data classes get the right behavior by default.
Making copy() private is also a binary-incompatible change. Modules or apps that were already compiled against the old public copy() can fail at runtime when they run against the new version. Once they are recompiled, any call to copy() from outside the class stops compiling, so those calls have to be changed, for example to a validating function like withValue(). Inside a single Android app this rarely hurts, since everything is compiled together. For shared libraries and SDKs it matters a lot.
If you are cleaning up data classes across a larger Android codebase and want a second pair of eyes, you can reach me through the contact page.
Frequently asked questions
Does @ConsistentCopyVisibility change equals() or destructuring?
No. It only changes the visibility of copy(). equals(), hashCode(), toString() and the componentN() functions keep working as before.
Can I still use copy() inside the class?
Yes. A private copy() is still visible inside the data class and its companion object, so a function like withValue() can validate the input and then call copy().
What is the difference between @ConsistentCopyVisibility and @ExposedCopyVisibility?
@ConsistentCopyVisibility makes copy() match the constructor's visibility. @ExposedCopyVisibility keeps the old public copy() and silences the warning, which is useful while you migrate or when a public API cannot change yet.
