React-native: Package is not a valid resource name

Created on 14 Jul 2019  路  14Comments  路  Source: facebook/react-native

Having following issue when running command ./gradlew :app:assembleprodRelease with enableHermes: true. _package.json_ is generated under folder _android/app/build/generated/res/react/release/raw/_ after execute command

Execution failed for task ':app:mergeProdReleaseResources'.
> 
android/app/build/generated/res/react/prod/release/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)

There are 2 product flavors _(stag and prod)_ for the project. Using below dependencies:
package.json

"react-native": "^0.60.3",
"react-native-fbsdk": "1.0.0-rc.5"

Below is the hermes dependency added in android/app/build.gradle

dependencies {
    if (enableHermes) {
      def hermesPath = "../../node_modules/hermesvm/android/";
      debugImplementation files(hermesPath + "hermes-debug.aar")
      releaseImplementation files(hermesPath + "hermes-release.aar")
      stagImplementation files(hermesPath + "hermes-release.aar")
      prodImplementation files(hermesPath + "hermes-release.aar")
    } else {
      implementation jscFlavor
    }
}

Same error when running command react-native run-android --variant prodRelease --appId <appId> --main-activity MainActivity

Even clean _android/build_ folder using command ./gradlew :app:clean, issue still persist. Is there any other way to fix this error?

Any help would be very appreciated.

Bug Ran Commands

Most helpful comment

@crispywong
Why don't import the package.json in the app? I'm using this to show the version in the app.
Is this a bad practice?

All 14 comments


Thanks for submitting your issue. Can you take another look at your description and make sure the issue template has been filled in its entirety?

馃憠 Click here if you want to take another look at the Bug Report issue template.

Finally found this which is same as my problem. After remove _package.json_ from import, this issue is solved. I will close this issue.

Finally found this which is same as my problem. After remove _package.json_ from import, this issue is solved. I will close this issue.

Where do you remove it

Don't import package.json inside your app. Try to check if any file is having import ... from package.json

@crispywong
Why don't import the package.json in the app? I'm using this to show the version in the app.
Is this a bad practice?

@crispywong
Why don't import the package.json in the app? I'm using this to show the version in the app.
Is this a bad practice?

Same here... What should I do instead ?

@crispywong
Why don't import the package.json in the app? I'm using this to show the version in the app.
Is this a bad practice?

can someone help answer this? i'm using the same method to show app version as well

Like the mentioned thread, the answer is in not importing package.json file. Easy workaround is to make a copy of package.json and import that file instead. Just keep copying package.json to the renamed file before publishing.

Like the mentioned thread, the answer is in not importing package.json file. Easy workaround is to make a copy of package.json and import that file instead. Just keep copying package.json to the renamed file before publishing.

ok noted, thanks for the answer.
I actually turned to react-native-device-info instead since there are other features that i wanted to use from the package

@sbvorpagel you can use react-native-device-info library to get the app version and build number, e.g.

const appVersion = `${DeviceInfo.getVersion()}.${DeviceInfo.getBuildNumber()}`

@sertony, just to be clear, I believe DeviceInfo.getVersion() and getBuildNumber() will return the version and build number of the native application, not the number that is represented in the package.json.

I imported package-lock.json instead of package.json, seems to work fine, fuck the police

@honeyp0t I dont know for you, but my package-lock.json is almost 1Mb. Quite a lot just to get the version...
I prefer keeping my app version in sync with gradle / Info.plist (with react-native-version for instance), and use :

import DeviceInfo from 'react-native-device-info'
const currentVersion = DeviceInfo.getVersionSync();
Was this page helpful?
0 / 5 - 0 ratings