Nihilism and the anti-corruption layer
Tags: architecture, gradle, android
Published: 2022-06-24 21:29:36

<a name="source-1" />Every now and then someone asks me if I intend a more technical follow-up to [Herding Elephants](https://developer.squareup.com/blog/herding-elephants/), which was a fairly high-level overview of the strategy I initiated to modernize that large Android repo I help maintain. And the answer is—not really. It is hard to appreciate how many moving parts exist in a repo at that scale without seeing it for yourself. Many of the lessons learned are irrelevant for smaller projects, which is most of them. The solutions to these medium-large problems[<sup>1</sup>](#endnotes) are expensive to build and expensive to maintain, and the cost-benefit analysis is only in their favor at a certain, well, scale. Dozens of developers and hundreds or thousands of modules. Millions of lines of code.

Of all the components that form part of our current build system, however, one stands out as _particularly cool_ and also as a metaphor for the human condition, which is what I really want to talk about.

## What to expect if you keep reading

I will elaborate on the anti-corruption layer I built to protect our build from upstream changes coming from the rapidly-evolving Android Gradle Plugin, and discuss how this has helped our build maintain stability and migrate to newer releases reliably and with increasing rapidity. I will wave my hands at some of the guard rails I built. I will discourage you from attempting this yourself. I will then pivot to how this relates to nihilism and the human condition.

## The anti-corruption layer

As any Android developer with more than a year or two of experience knows, the Android Gradle Plugin (AGP) is constantly evolving. For the very simplest projects, these changes are largely transparent, but I would guess that _most_ Android projects have had frustrations with these changes over the years.

One of the pain points of AGP is that its API is constantly changing. (Google even has a documented "[migration timeline](https://developer.android.com/studio/releases/gradle-plugin-roadmap)" for this API; it spans years.) Google also takes the idiosyncratic perspective that many of its public classes aren't really public API, but internal implementation detail, and because of this, it often pushes breaking changes without any corresponding signal via [semantic versioning](https://semver.org/).

There are two extreme solutions to this problem. At one extreme, _which I highly recommend you follow_, is being a **Conformist**. This is a design pattern whereby you essentially "give up" and admit that you will never really be able to craft a model specific to your project. Your model is AGP's model. This is fine.

![This is fine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbixqd9omkldy6rhzl5d.gif)

<a name="source-2" />At the other extreme we have the **Anti-corruption layer**. You probably shouldn't do this.[<sup>2</sup>](#endnotes)

An anti-corruption layer is used when a downstream project has a dependency on an upstream that it (1) has no control over; (2) can't avoid; and (3) doesn't trust. This precisely describes our relationship with AGP. The concept is often summarized as "wrapping" dependencies, but it is more than that. Let's consider the implementation of this pattern that we use in the "convention plugins" I wrote about in [Herding elephants](https://developer.squareup.com/blog/herding-elephants/).

> I want to be clear here that I'm speaking in a highly technical sense. I am not making normative judgments about AGP's structure nor the direction of its evolution. In point of fact, I often have fruitful discussions with members of the team that maintains AGP, whom I like and respect. Nevertheless, the only power I hold over that upstream dependency is the power of persuasion, which doesn't cut it when my code is used tens of thousands of times per day to build software that powers a multi-billion dollar business.

We start with the _facade_, or wrapper interface. A facade is the interface we wish we had, and which takes care to redirect to the actual interface provided by the dependency (AGP in this case). Here's a simplified example:

![Wrapper interface](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yoch4qcn0ymodliamaqa.png)

On the right is AGP, which has complicated methods like `majorArcanum(...)` and `minorArcanum(...)`, which each take many arguments. On the left we have our desired interface, which is just a single method that takes no arguments. We can do this because, while AGP is a _general purpose_ build tool that has to solve problems for Every Android Developer Everywhere, we have a different, smaller problem: how to build _our_ apps for _our_ developers.

We wrap our facade in an "adapter" that knows how to talk to different versions of AGP. This is how we solve the problem of AGP's rapidly changing interface. Our adapter can provide a different implementation for each version of AGP we support, which is the current version plus the next two release candidates in the AGP pipeline. Through this mechanism, we can update to each new release with ease—no more months-long migrations.

To help you visualize how this works, consider this simplified implementation:

```kotlin
class AndroidGradlePluginFactory(
  private val project: Project,
  private val agpVersion: AgpVersion,
) {

  companion object {
    fun getAdapter(project: Project): AndroidGradlePlugin {
      return AndroidGradlePluginFactory(
        project = project,
        agpVersion = AgpVersion.current(project)
      ).newAdapter()
    }
  }

  fun newAdapter(): AndroidGradlePlugin = when {
    agpVersion >= AgpVersion.version("7.3") -> AndroidGradlePlugin7_3(project)
    agpVersion >= AgpVersion.version("7.2") -> AndroidGradlePlugin7_2(project)
    agpVersion >= AgpVersion.version("7.1") -> AndroidGradlePlugin7_1(project)
    else -> throw UnknownAgpException("No adapter for AGP $agpVersion")
  }
}
```

Please be warned that `AgpVersion.current(project)` encapsulates a non-trivial implementation for retrieving the actual version of AGP that is present at runtime. Also note that each implementation is encapsulated in its own project (module) to isolate its classpath.

To use this factory, your plugins should look like this:

```kotlin
class MyPlugin : Plugin<Project> {
  override fun apply(target: Project) {
    val agp = AndroidGradlePluginFactory.getAdapter(target)
    // all accesses of AGP functionality _must_ go through `agp`
  }
}
```

You can also inject an `agp` instance into your custom extension and then require your developers to use that extension (rather than the backing `android` extension) for customizing their projects.

```kotlin
abstract class CircleExtension(agp: AndroidGradlePlugin) { 
  fun buildMyAppPlease(how: String) { … }
}
```

```groovy
// app/build.gradle
plugins {
  id 'com.circle.android.app'
}

circle {
  buildMyAppPlease 'with style'
}
```

### Guard rails

This is already pretty complicated, yet it's also not enough. This is why you shouldn't do it. But if you insist, below are some of the guards we've had to institute to keep from going off the rails.

Please note that, in each case, I'm _dramatically simplifying_ our build setup so that it fits into a blog post shorter than a master's thesis. This isn't easy and you should think long and hard before deciding to do this.

#### Comprehensive test suite

All of our plugins are thoroughly tested against all supported versions of AGP. This is very [hard to get right](https://dev.to/autonomousapps/defensive-development-gradle-plugin-development-for-busy-engineers-486c), but utterly necessary.

#### NoAgpAtRuntime

We have a task, `noAgpAtRuntime`, that is registered on all of our build-logic modules. This task will fail if it discovers a class from AGP on the build classpath. We expect the build that applies our plugins (the "main build") to be responsible for providing AGP.

#### Strict dependencies

We set a strict dependency constraint on our root build classpath to ensure it's using the version of AGP we specify for every build.

```groovy
// root build.gradle of main build
buildscript {
  dependencies {
    constraints {
      classpath('com.android.tools.build:gradle') {
        version { strictly agpVersion /* a string */ }
      }
    }
  }
}
```

#### DependencyGuard

We use [DependencyGuard](https://github.com/dropbox/dependency-guard) to fail our build if the build classpath changes unexpectedly.

```groovy
// root build.gradle
plugins {
  id 'com.dropbox.dependency-guard'
}
dependencyGuard {
  configuration('classpath')
}
```

## Nihilism gets a bad rap

<a name="source-3" />What has any of this to do with nihilism?[<sup>3</sup>](#endnotes) As a concept, it's something I've been flirting with my entire life, and it came to a head recently when I happened to catch an [episode of Radiolab](https://radiolab.org/episodes/dust-planet) from 2014, about a book called ["In the Dust of This Planet"](https://www.johnhuntpublishing.com/zer0-books/our-books/in-the-dust-of-this-planet)—a book I subsequently bought and devoured.

I think most people have weird ideas about what nihilism means; my first encounter with it was probably when I saw _The Big Lebowski_ as a teenager.

![Nihilists in The Big Lebowski](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g66wykj4964z0m15m36n.png)

This is not the kind of nihilism I'm talking about. I mean it as more of a renunciation of received truth. In the context of software development, we have lots of received truths, and simply accepting them as such leads to the _conformist_ approach (or "pattern") discussed above. Rejecting those truths and going your own way contains, for me, an element of nihilism that I find myself embracing.

### Nothing matters, so do what you want

I was already working on this post when I finally saw _Everything Everywhere All at Once_ the other day. That movie is profoundly nihilistic and I <3 it so much. The primary antagonist in the film, Jobu Tupaki, has come to realize that nothing matters and so has created the everything-bagel-of-nothing, a thing which annihilates all who gaze upon it; this self-destruction is what she herself seeks. Her mother, Evelyn, comes to the same realization, but lands on a different conclusion, which is that, because nothing matters, she can do what she wants, and what she wants is to spend her one life with her loved ones.

One of the ways I know that nothing matters is that we've radically destabilized the climate such that it is a foregone conclusion that billions will die and civilizations will fall, and the present mass extinction will continue. We're far too late to stop it, and this against a background of the majority of people _wanting to stop it_, but our public institutions being so corrupt that we can only watch in horror as they go in the opposite direction. Truly, "nothing matters."

I guess you could say I'm an _eco-nihilist_.

> On a day like today, I must also point out that the US Supreme Court, in an act of destructive nihilism, has further proven my point by going against the democratic majority to expand gun rights and constrict women's rights, and is almost certainly setting up to further curtail personal liberty with attacks on LGBTQ rights in the coming years.

### The anti-corruption layer, take 2

If you've read this far, you have the patience of a saint, and so I'm going to cut to the chase. My anti-corruption layer, that lets me continue to persist despite _knowing_, to my core, that nothing matters, is a deep well of incandescent rage. Conformity would be so much easier—it's what practically everyone around me wants me to do. But I find it literally impossible. We live on a garden world in an endlessly empty universe; it could be a paradise yet we've built a death machine instead. I am _so angry_. That anger is my shield, but increasingly my nihilism is my shield, too. Nothing matters, so I'm going to do what I want.

![An updated wrapper interface, this time around The World](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gh1ke0ehiv7a7ziv6bw.jpg)


<a name="endnotes"/>
## Endnotes
<sup>1</sup> We're not _truly_ large (cf Google). [<small>up</small>](#source-1)
<sup>2</sup> The world of software development is thick with metaphor. This post uses numerous metaphors from the world of design patterns, with a focus on two in particular: **Conformist** and **Anti-corruption layer**. I learned the names of these patterns from the book [_Domain-driven design_](https://en.wikipedia.org/wiki/Domain-driven_design) by Eric Evans, but discovered them independently through an intent focus on solving the kinds of problems I often write about on this blog. See also [Christopher Alexander](https://en.wikipedia.org/wiki/Christopher_Alexander) and [A Pattern Language](https://en.wikipedia.org/wiki/A_Pattern_Language). [<small>up</small>](#source-2)
<sup>3</sup> I am not a philosopher (IANAP). [<small>up</small>](#source-3)