Video in progress

How do you preserve binary and source compatibility when you change the API of a library that other teams use?

Source compatibility means the client code is recompiled; binary compatibility means an old compiled JAR works with the new library. Many changes are safe for one and not the other. You need rules, automated checks (japicmp, revapi) and a deprecation policy.

📖 Answer

There are two kinds of breakage, and they blow up in different places:

Change Source Binary
Changing a method's return type from List to Collection breaks breaks (NoSuchMethodError)
Changing a parameter from int to long often works breaks
Adding an abstract method to an interface breaks (implementations) not immediately; AbstractMethodError when it's called
Adding a default method to an interface usually safe, but a name clash is possible safe
Changing the value of a public static final constant (a compile-time one: a primitive or String) safe the old value stays, because the compiler inlined it
Adding a checked exception breaks doesn't break
Adding an overload ambiguity (a null argument) can appear safe

The most dangerous case is a transitive dependency: service A calls your library's v2, while library B was compiled against v1. Maven picks one version, and the error shows up only at runtime, on a particular path.

My rules:

  1. Take semver seriously. Minor is additions only; major can break, but with a migration guide.
  2. An automated check in CI. japicmp or revapi diffs against the previous release and fails the build if a minor release has a binary break.
  3. Reduce the extension points. final classes, sealed interfaces, factory methods, a builder instead of a constructor: the less you expose, the more freely you can change.
  4. A deprecation cycle. @Deprecated(since = "2.3", forRemoval = true), plus the alternative in the Javadoc, and both live side by side for at least one minor release.
  5. Separate the internal parts. An *.internal package plus module-info or Automatic-Module-Name, or at least "don't use this" in the docs.
  6. Expose constants through a method if the value might change.

In an organization with many teams, publishing a BOM also helps: everyone gets a set of compatible versions, and "diamond" conflicts become less common.