Skip to content
Browse files

Fix duplicate resource error in Android gradle build (#22234) (#24518)

Issue #22234 includes a number of people (myself included) suffering with duplicate resource errors while building in Android. We have been collectively using a patch there but I don't believe any attempt has been made to PR it upstream.

I am not excellent at gradle, so I may have approached this in completely the wrong way, but it works for me in the standard init templates, as well as my current work project which has a complicated 2 buildType + 4 productFlavor configuration. If there is a better way to achieve the result I am happy to learn

The approach here is to determine the generated path the resources land in, then move them into the main build output tree. If they remain in the generated path android merging fails with duplicate resource errors, so that move (vs copy) is important.

[Android] [Fixed] - Fix duplicate resource error in Android build
Pull Request resolved: #24518

Differential Revision: D15276981

Pulled By: cpojer

fbshipit-source-id: 3fe8c8556e4dcdac5f96a2d4658ac9b5d9b67379
  • Loading branch information...
mikehardy authored and facebook-github-bot committed May 9, 2019
1 parent 6ccdf85 commit 962437fafd02c936754d1e992479056577cafd05
Showing with 26 additions and 0 deletions.
  1. +26 −0 react.gradle
@@ -48,6 +48,32 @@ afterEvaluate {

// If there are flavors, remember the current flavor for use in the resource path we move from
def flavorPathSegment = ""
android.productFlavors.all { flavor ->
if (targetName.toLowerCase().contains( {
flavorPathSegment = + "/"

// Address issue #22234 by moving generated resources into build dir so they are in one spot, not duplicated
doLast {
def moveFunc = { resSuffix ->
File originalDir = file("$buildDir/generated/res/react/${flavorPathSegment}release/drawable-${resSuffix}")
if (originalDir.exists()) {
File destDir = file("$buildDir/../src/main/res/drawable-${resSuffix}")
ant.move(file: originalDir, tofile: destDir);

// Set up inputs and outputs so gradle can cache the result
inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)

0 comments on commit 962437f

Please sign in to comment.
You can’t perform that action at this time.