react-native: [Android] possible regression with `:app:mergeReleaseResources` in 0.60.0-rc.1
Problems
Those problems were introduced in 0.60.0-rc:
-
import packageJson from './package.json'
causes errorExecution failed for task ':app:mergeReleaseResources'. > /Users/rying/repos/resourcesRepro/android/app/src/main/res/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)
Apparently
package.json
got copied tores/raw
which makes gradle complain. This works fine in 0.59.x, however. -
All those generated resources (e.g.
package.json
,app.json
…) get moved toandroid/app/src/main/res
.- I believe those generated resources should not be moved into this directory since they are generated on the fly. The current behavior in 0.60.0-rc makes it difficult for Git version control - they should always in
build
rather thansrc
. - Because of this,
metro
will also complain:
I need to add those files into metro’s blacklist to stop this error. 😭jest-haste-map: Haste module naming collision: myapp The following files share their name; please adjust your hasteImpl: * <rootDir>/package.json * <rootDir>/android/app/src/main/res/raw/package.json
- I believe those generated resources should not be moved into this directory since they are generated on the fly. The current behavior in 0.60.0-rc makes it difficult for Git version control - they should always in
Possible relevant commits
-
https://github.com/facebook/react-native/commit/962437fafd02c936754d1e992479056577cafd05
-
https://github.com/facebook/react-native/commit/eb534bca58a89ae306010626a8bdae92c23b8784
React Native version:
System:
OS: macOS 10.14.5
CPU: (4) x64 Intel(R) Core(TM) i3-8100B CPU @ 3.60GHz
Memory: 194.87 MB / 8.00 GB
Shell: 5.7.1 - /usr/local/bin/zsh
Binaries:
Node: 12.4.0 - /usr/local/Cellar/node/12.4.0/bin/node
Yarn: 1.16.0 - /usr/local/Cellar/node/12.4.0/bin/yarn
npm: 6.9.0 - /usr/local/Cellar/node/12.4.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
IDEs:
Android Studio: 3.4 AI-183.6156.11.34.5522156
Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.6 => 16.8.6
react-native: 0.60.0-rc.1 => 0.60.0-rc.1
Steps To Reproduce
- Clone https://github.com/robertying/resources-repro
- yarn
- react-native run-android --variant release
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 48 (28 by maintainers)
Commits related to this issue
- Fix regression of improper assets copy (revert #24518 #24778) (#25363) Summary: Pull requests https://github.com/facebook/react-native/issues/24518 #24778 make Gradle copy all **generated** assets an... — committed to facebook/react-native by robertying 5 years ago
- Exclude `$projectRoot/package.json` from assets (#420) Summary: **Summary** From https://github.com/facebook/metro/commit/dcb41e39f0df6ae1e0b3dfeff9ef5a69128830d5#diff-235a3e5d21175615e1cc254dd8b17e... — committed to facebook/metro by robertying 5 years ago
- Fix regression of improper assets copy (revert #24518 #24778) (#25363) Summary: Pull requests https://github.com/facebook/react-native/issues/24518 #24778 make Gradle copy all **generated** assets an... — committed to M-i-k-e-l/react-native by robertying 5 years ago
- 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 A... — committed to facebook/react-native by mikehardy 5 years ago
also, a trick, if you have modified a package after install, by directly editing the files in node_modules, then your change won’t work if you delete your own node_modules and reinstall, and it won’t work on CI or co-workers machines etc, because the file needs to be changed every time.
Easy solution though - this is the trick - install the wonderful patch-package npm module, hook it in your postinstall in package.json, and after editing in node modules run
npx patch-package <name of module you edited>
.That diffs the original module vs the changes you made, creates a patch, puts it in the ./patches/ directory with a versioned name and everything, and now your change will be applied for everyone postinstall (so it works on CI etc) and later if the module is updated it will give you a nice warning to see if you still need your patch.
react-native development is painful (to me) without this tool…with it, no problems
@Fouppy Unfortunately, this is still an issue. I opened a PR https://github.com/facebook/metro/pull/420 to metro but they don’t seem to be responding. So I guess this problem will exist for some time.
But meanwhile, you could use the patched file to get around.
Hi @mikehardy I carefully took a look at the issue https://github.com/facebook/react-native/issues/22234 and believe your commit may not be necessary.😥
So this issue https://github.com/facebook/react-native/issues/22234 happened because people may do this:
This was done by the person who authored the issue:
Apparently
--assets-dest android/app/src/main/res/
is not correct. This meanscli
will generate assets in two places:android/app/src/main/res/
andandroid/app/build/generated/res/react/release/
, which causes the duplicate.The issue may be caused by putting generated assets in version control:
Like this one https://github.com/facebook/react-native/issues/22234#issuecomment-495052997, some team member put
res/drawable-*
in version control.Those two can be avoid by correctly using
react-native
cli or just simply usingreact-native run-android --variant=release
and removeres/drawable-*
generated by cli as some suggested in the issue https://github.com/facebook/react-native/issues/22234.What is your scenario of this problem? Can it be solved this way? If so, those generated assets may not need copying from build dir. 😉
@mailyokesh Robert’s suggestion is best, but just in case that’s not possible for folks there was a typo in my script which I’ve now fixed.
Hi. I’m still getting the
package is not a valid resource name
when importing package.json into my RN app and runningreact-native run-android
. I’m on 0.60, so it should be fixed, right?@mikehardy the pull request #25363 was made. Thanks again for clarifying the problem! Looking forward to your final solution.
IDK if this was mentioned before but building the android apk with a pre-built bundle can be done by appending
-x bundleReleaseJsAndAssets
to your gradle command. Ex:./gradlew :app:assembleRelease -x bundleReleaseJsAndAssets
If the bundle and assets are already at the locations expected by the gradle script, they will be packaged into the apk
@mikehardy I did some print on
react.gradle
and found the default bundle args:This is for
release
variant:Here it is!
assets-dest
should point to a directory underbuild
:Is this helpful? 😊
Hi @rozPierog I’m already using the latest gradle 5.4.1 and gradle plugin 3.4.1 generated from RN 0.60 template.