Skip to content

Commit 2d9e2f3

Browse files
jpshelleyfacebook-github-bot
authored andcommitted
Android - Fix Drawable v4 paths in Android Artifact Resources (Libraries)
Summary: _Quick apologies for the lengthiness of this description. Want to make sure I'm clear and it is understood what is being altered._ Thanks for submitting a PR! Please read these instructions carefully: - [x] Explain the **motivation** for making this change. - [x] Provide a **test plan** demonstrating that the code is solid. - [x] Match the **code formatting** of the rest of the codebase. - [x] Target the `master` branch, NOT a "stable" branch. #5787 ``` Unknown source file : /home/tom/projects/blueprint-native/android/app/build/intermediates/res/merged/release/drawable-mdpi-v4/images_google.png: error: Duplicate file. Unknown source file : /home/tom/projects/blueprint-native/android/app/build/intermediates/res/merged/release/drawable-mdpi/images_google.png: Original is here. The version qualifier may be implied. ``` At Hudl, we've been attempting to package our React Native code into Library Dependencies _(Cocoapods / Android Artifact Resource (aar))_. Recently in React Native 0.42.0, there was an upgrade to the Android Project's gradle plugin from 1.3.1 to 2.2.3. This update drastically effected the outcome of drawable resources in Android without anyone noticing. **There are 4 outcomes to consider with this change:** 1. You are developing in an Android Application using Gradle 1.3.1 or lower 2. You are developing in an Android Application using Gradle 2.2.3 or higher 3. You are developing in an Android Library Module using Gradle 1.3.1 or lower 4. You are developing in an Android Library Module using Gradle 2.2.3 or higher With the upgrade to 2.2.3, Android changed the way aapt builds its resources. Any Library created with 2.2.3, has its resources ending with a `v4` suffix. The reasoning behind this I'm not sure of but assume it deals with Vector support that was added around that time. The change I've added checks if React Native is being ran in an Android Library vs an Application, and appends the v4 suffix to the merged asset folders. Multiple test were performed. 1. I first started out validating my assumption about the asset merger by creating a new Android Project to verify my assumptions above. 1. [Application + >= 2.2.3](https://github.com/jpshelley/TestAndroidLibraryDrawables/tree/master/app/build/intermediates/res/merged/debug) -- `hdpi` contains my drawable. `hdpi-v4` contains dependency's drawables. 2. [Application + <= 1.3.1](https://github.com/jpshelley/TestAndroidLibraryDrawables/tree/Android-LegacyVersion/app/build/intermediates/res/merged/debug) -- Same as above (I expect because deps are compiled against gradle 2.2.3+ themselves. 3. [Library + >= 2.2.3](https://github.com/jpshelley/TestAndroidLibraryDrawables/tree/Android-UsingAndroidLibrary/library/build/intermediates/res/merged/debug) -- Only `-v4` folders found! Resources from the library are packages in the app's build output in similar `-v4` folder too. 4. [Library + <= 1.3.1](https://github.com/jpshelley/TestAndroidLibraryDrawables/tree/Android-UsingAndroidLibraryLegacyVersion/library/build/intermediates/res/merged/debug) -- Same as ii. & iii. `-v4` contains other resources, but my resources are located in non -v4 folder. 2. I then wanted to validate against React Native itself. So I updated my react native code using this PR/Branch, and tested against my project locally. Unfortunately I cannot share that code as it is private, but before this change I was getting the same error as mentioned in #5787 and now my build runs as intended with the assets being placed where they should be. Additional resources: * #5787 * http://stackoverflow.com/questions/35700272/android-build-tool-adds-v4-qualifier-to-drawable-folders-by-default-in-generated * #12710 Please let me know if more information is needed, further test plans, etc. With this change we should be able to upgrade to Gradle 2.3.0 as well to support the latest version of Android Studio. Closes #13128 Differential Revision: D7828618 Pulled By: hramos fbshipit-source-id: a7ad7b63b1b51cbfd2ea7656e4d77321306ce33a
1 parent f99ca3c commit 2d9e2f3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

react.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void runBefore(String dependentTaskName, Task task) {
2424
}
2525

2626
afterEvaluate {
27+
def isAndroidLibrary = plugins.hasPlugin("com.android.library")
2728
// Grab all build types and product flavors
2829
def buildTypes = android.buildTypes.collect { type -> type.name }
2930
def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
@@ -101,6 +102,24 @@ afterEvaluate {
101102
enabled config."bundleIn${targetName}" ||
102103
config."bundleIn${buildTypeName.capitalize()}" ?:
103104
targetName.toLowerCase().contains("release")
105+
106+
if (isAndroidLibrary) {
107+
doLast {
108+
def moveFunc = { resSuffix ->
109+
File originalDir = file("${resourcesDir}/drawable-${resSuffix}")
110+
if (originalDir.exists()) {
111+
File destDir = file("${resourcesDir}/drawable-${resSuffix}-v4")
112+
ant.move(file: originalDir, tofile: destDir)
113+
}
114+
}
115+
moveFunc.curry("ldpi").call()
116+
moveFunc.curry("mdpi").call()
117+
moveFunc.curry("hdpi").call()
118+
moveFunc.curry("xhdpi").call()
119+
moveFunc.curry("xxhdpi").call()
120+
moveFunc.curry("xxxhdpi").call()
121+
}
122+
}
104123
}
105124

106125
// Hook bundle${productFlavor}${buildType}JsAndAssets into the android build process

0 commit comments

Comments
 (0)