Skip to content

Commit 962437f

Browse files
mikehardyfacebook-github-bot
authored andcommitted
Fix duplicate resource error in Android gradle build (#22234) (#24518)
Summary: 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
1 parent 6ccdf85 commit 962437f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

react.gradle

+26
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ afterEvaluate {
4848
resourcesDir.mkdirs()
4949
}
5050

51+
52+
// If there are flavors, remember the current flavor for use in the resource path we move from
53+
def flavorPathSegment = ""
54+
android.productFlavors.all { flavor ->
55+
if (targetName.toLowerCase().contains(flavor.name)) {
56+
flavorPathSegment = flavor.name + "/"
57+
}
58+
}
59+
60+
// Address issue #22234 by moving generated resources into build dir so they are in one spot, not duplicated
61+
doLast {
62+
def moveFunc = { resSuffix ->
63+
File originalDir = file("$buildDir/generated/res/react/${flavorPathSegment}release/drawable-${resSuffix}")
64+
if (originalDir.exists()) {
65+
File destDir = file("$buildDir/../src/main/res/drawable-${resSuffix}")
66+
ant.move(file: originalDir, tofile: destDir);
67+
}
68+
}
69+
moveFunc.curry("ldpi").call()
70+
moveFunc.curry("mdpi").call()
71+
moveFunc.curry("hdpi").call()
72+
moveFunc.curry("xhdpi").call()
73+
moveFunc.curry("xxhdpi").call()
74+
moveFunc.curry("xxxhdpi").call()
75+
}
76+
5177
// Set up inputs and outputs so gradle can cache the result
5278
inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
5379
outputs.dir(jsBundleDir)

0 commit comments

Comments
 (0)