React-native: React Native 0.57.7 - Android build failed at task :app:mergeReleaseResources

Created on 15 Feb 2019  路  13Comments  路  Source: facebook/react-native

I'm getting an error when running the ./gradlew assembleRelease --stacktrace command in my React Native (v0.57.7) project's android folder:

> Task :app:mergeReleaseResources FAILED
/Users/steff/Github/iazzu/app/android/app/build/generated/res/react/release/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeReleaseResources'.
> /Users/steff/Github/iazzu/app/android/app/build/generated/res/react/release/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:mergeReleaseResources'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:103)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:73)
        at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
        at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:256)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:249)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:238)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:663)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:597)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: com.android.build.gradle.tasks.ResourceException: /Users/steff/Github/iazzu/app/android/app/build/generated/res/react/release/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)
        at com.android.build.gradle.tasks.MergeResources.doFullTaskAction(MergeResources.java:296)
        at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:124)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:113)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:95)
        ... 30 more
Caused by: /Users/steff/Github/iazzu/app/android/app/build/generated/res/react/release/raw/package.json: Error: package is not a valid resource name (reserved Java keyword)
        at com.android.ide.common.res2.MergingException.throwIfNonEmpty(MergingException.java:152)
        at com.android.ide.common.res2.DataSet.loadFromFiles(DataSet.java:260)
        at com.android.ide.common.res2.ResourceSet.loadFromFiles(ResourceSet.java:55)
        at com.android.build.gradle.tasks.MergeResources.doFullTaskAction(MergeResources.java:267)
        ... 42 more


* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.6/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 6s
144 actionable tasks: 4 executed, 140 up-to-date

When checking the folder android/build/generated/res/react/release/raw folder, the package.json file is being copied there along with all my other resources (mainly .json files).

What could I possibly do to prevent this weird thing from happening? Just to have said it, I've tried googling for hours now but can't find a solution.

Android Ran Commands Locked Question

Most helpful comment

Don't import package.json inside your app. This will cause production build to include package.json into your bundle resources and it will cause Java error.

Strange, I haven't seen that point in official React Native docs.

All 13 comments


We are automatically closing this issue because it does not appear to follow any of the provided issue templates.

馃憠 Click here if you want to report a reproducible bug or regression in React Native.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.

What is the solution for this?

experiencing same problem it says incompatible with gradle 6.0 i'm thinking updating gradle manually

Don't import package.json inside your app. This will cause production build to include package.json into your bundle resources and it will cause Java error.

Strange, I haven't seen that point in official React Native docs.

Am having the same error

me too :(

another one here !

Don't import package.json inside your app. This will cause production build to include package.json into your bundle resources and it will cause Java error.

Strange, I haven't seen that point in official React Native docs.

Even more strange is we were successfully doing this in 57.8 but after upgrading to 60.0 got this error.

Don't import package.json inside your app. This will cause production build to include package.json into your bundle resources and it will cause Java error.
Strange, I haven't seen that point in official React Native docs.

Even more strange is we were successfully doing this in 57.8 but after upgrading to 60.0 got this error.

Hmm, "package" is a reserved word in Java. Seems, RN Android build was including the package.json differently before the 60.0 version.

Don't import package.json inside your app.

Thanks this indeed fixed stuff for me. However I still need package.json to read version of the app and codeBundle. If you're like me then the only reason android is currently not working is that it has problem with package word. What I did is created short bash script to actually copy and rename package.json.:

// Contents of file named: createVersion.sh
#!/bin/bash

rm -r version.json
cp package.json version.json

Then inside my JS I do this: import packageJson from '../version.json'.
Before you build your production apk just run sh ./createVersion.sh.
It's definitely only a temporary solution but does the trick 馃憤 .

For those of you that need to import package.json inside your app and want a fix that doesn't require changing any of your code, you can just add this script to the root project directory and run it postinstall:

// fix_metro_android_release_bug.js
// Temporary fix for this issue: https://github.com/facebook/metro/pull/420
const fs = require('fs');

const fileLocation = './node_modules/metro/src/DeltaBundler/Serializers/getAssets.js';
const targetText = 'getJsOutput(module).type === \'js/module/asset\'';
const replacementText = 'getJsOutput(module).type === \'js/module/asset\' && path.relative(options.projectRoot, module.path) !== \'package.json\'';

const fileContent = fs.readFileSync(fileLocation, 'utf8');
if (fileContent.includes(targetText) && !fileContent.includes(replacementText)) {
  const patchedFileContent = fileContent.replace(targetText, replacementText);
  fs.writeFileSync(fileLocation, patchedFileContent, 'utf8');
}

Then, inside your inside your package.json:

  "scripts": {
    "postinstall": "babel-node ./fix_metro_android_release_bug.js",
    ...
  }

The script applies the changes from the PR which fixes the underlying issue (which hasn't been merged in yet).

Another solution is to use the app.json that in many installations is already there (Expo, etc.).

So, I put a new key there and import this json instead:

{
  "name": "mobileapp",
  "displayName": "App",
  "customFields": {
    "jsBuildNumber": "1208"
  }
}
Was this page helpful?
0 / 5 - 0 ratings