Sphere
Log in Join

Fix AAPT2 duplicate resource error in Android Gradle Plugin 8.5.2

Our release build died about twenty minutes before I was supposed to hand a test APK to QA. Nothing fancy changed, someone merged a PR that "cleaned up" the strings file for our new demo flavor. Locally on my MacBook the normal debug build was fine. On the CI box (Ubuntu 22.04, JDK 17, Gradle 8.7, AGP 8.5.2) assembleDemoDebug just stopped.

## The actual error

The terminal spat this out after about a minute of kapt work:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDemoDebugResources'.
> AAPT2 aapt2-8.5.2-11315950-linux Daemon #0: Unexpected error during link, attempting to stop daemon.
This should not happen under normal circumstances, please file an issue if it does.
AGPBI: {"kind":"error","text":"duplicate value for resource 'string/app_name' with config ''","sources":[{"file":"/home/runner/work/app/app/build/intermediates/incremental/mergeDemoDebugResources/merged.dir/values/values.xml","position":{"startLine":12}}],"tool":"Resource and asset merger"}

The "unexpected error" line is a red herring. AAPT2 says that whenever linking fails for any reason. The useful bit is buried in the AGPBI line: duplicate value for resource 'string/app_name'.

## Finding the second definition

I looked at merged.dir/values/values.xml line 12 and saw the app name, but that file is generated so it doesn't tell you who put it there. What actually found it was:

grep -R "name=\"app_name\"" app/src -n

Two hits. One in app/src/main/res/values/strings.xml, one in app/src/demo/res/values/strings.xml. The PR author had copied the whole main strings file into the flavor folder and then changed three entries. Nine other strings were defined twice.

Turns out resource merging does not "override by flavor" the way Gradle's dependency resolution does. Two definitions of the same resource name in the same merged source set is just an error, full stop. The flavor folder is meant to hold overrides, not a full copy.

## The fix, and the thing I added after

Deleted everything from the flavor's strings file except the three strings that genuinely differ. Build went green. Then I added a guardrail in app/build.gradle:

android {
namespace 'com.example.app'
compileSdk 35

resourcePrefix 'demo_'

defaultConfig {
applicationId "com.example.app.demo"
minSdk 24
targetSdk 35
versionCode 42
versionName "2.4.0"
}
}

resourcePrefix makes AGP warn when a new resource in that module doesn't start with demo_. It would not have caught the duplicate app_name (that's defined in main), but it did catch the next copy-paste two weeks later.

I also got in the habit of running ./gradlew :app:dependencies --configuration demoDebugRuntimeClasspath when a flavor build starts acting strange, though that only helps with dependency conflicts, not resource ones. The real lesson was simpler: if a PR adds files to a flavor source set, read the diff before you trust the build. A one-line change to a string usually means someone did something more interesting than they described.

## Why the local build never saw it

Because I was building :app:assembleDebug, which doesn't include any flavor source sets. The flavor only gets merged when you actually ask for it. I've since changed my habit to always build the exact variant CI builds, even when I'm lazy:

./gradlew :app:assembleDemoDebug --stacktrace

The --stacktrace is mostly habit at this point, but it's saved me a couple of times when a plugin threw something opaque. If you know a cleaner way to catch duplicate resources across all flavors at once, I'm all ears, right now I just grep.