Cli: A project with custom root fails at: cli-platform-android/native_modules.gradle' line: 191

Created on 26 Jul 2019  路  11Comments  路  Source: react-native-community/cli

I am trying to upgrade to 60.4, and introduced the 'autolinking' capability by modifying my settings.gradle and app/build.gradle.
Platform is Linux, RN platform is android only, cli is 2.8.0.

With that change, however I cannot run any gradle commands (not even gradlew clean):

bash gradlew clean


FAILURE: Build failed with an exception.

* Where:
Script '/home/v/myproj/mob/rn.common/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 191

* What went wrong:
A problem occurred evaluating settings 'myapp1b'.
> Cannot get property 'packageName' on null object

The cli version is latest, 2.8.0
The line where it is failing, trying to parse json output from running react-native-config
Line 191:
this.packageName = json["project"]["android"]["packageName"]

When I run
PATH=$(npm bin):$PATH react-native config

I can see that in its output ["project"]["android" is indeed null.

...
"platforms": {
    "ios": {},
    "android": {}
  },
  "haste": {
    "providesModuleNodeModules": [
      "react-native"
    ],
    "platforms": [
      "ios",
      "android"
    ]
  },
  "project": {
    "ios": null,
    "android": null
  }

_But why, and what do I need to do to fix it?_
It seems that applyNativeModulesSettingsGradle relies on it, and that's why my build completely stopped at the moment.

Additional info:

cat react-native.config.js <-- this is root of my RN code. this dir also has index.android.js

module.exports = {
  project: {
    ios: {},
    android: {},
  },
  assets: ['./src/js.app/img'],
};

cat settings.gradle

//do  npm install @react-native-community/[email protected] --save-dev
//in case this component is not installed directly by RN

rootProject.name = 'myapp1b' //this rootProjectName used by community cli. so I am setting to be what my gradle defined as project.name (which is directory name of my proj, or equal to rootProject.name that can only be set in this file) https://discuss.gradle.org/t/rootproject-name-in-settings-gradle-vs-projectname-in-build-gradle/5704

//this whole machinery (plus a change in app/build.gradle) is used to auto generate rn-community module names so that they can, indirectly be included in to app/build.gradle so that those modules can be used

apply from:file("${rootDir}/../../rn.common/node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings,"../../rn.common/") //note second arg, this is a must because I do not have a default location of node_modules (see custom root section at: https://github.com/react-native-community/cli/blob/master/docs/autolinking.md )

include ':app', ':lint'
include ':react-native-vector-icons'

project(':react-native-vector-icons').projectDir = file("${rootDir}/../../../mob/rn.common/node_modules/react-native-vector-icons/android")

I did not even have, initially, the react-native.config.js (because it was not noted as needed in rn-diff utility) , but even after adding it -- it did not help.

appreciate in advance any guidance.

question

Most helpful comment

I deleted my build folder and it worked.

All 11 comments

I fixed the problem.
Because of custom paths (I have essentially a form of mono-repo with many sub projects)
PATH=$(npm bin):$PATH react-native config
could not find where my root android part of the project was (it was not in standard directory relative to the JS part of the project)

I am not sure if this nuance is covered in
https://github.com/react-native-community/cli/blob/master/docs/autolinking.md
It talks about how to tell cli, in 'custom root' configuration , about the location of JS part of the project, but does not tell how to tell the cli tools about the the location of android side of the project

So all I had to do, to fix this specific problem, is to specify sourceDir and packageName in the react-native.config.js


module.exports = {
  project: {
    ios: {},
    android: {
    "sourceDir": "../a/myapp1b/app/src",
    "packageName":"com.myapp1b.myapp"

    },
  },
  assets: ['./src/js.app/img'],
};


can anyone assist on how this would relate to a build in Azure DevOps, this is where I get the error:

FAILURE: Build failed with an exception.

* Where:
Script 'D:\a\1\s\node_modules\@react-native-community\cli-platform-android\native_modules.gradle' line: 191

* What went wrong:
A problem occurred evaluating settings 'NFIBEngage'.
> Text must not be null or empty

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

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

BUILD FAILED in 1m 42s
Error: The process 'D:\a\1\s\android\gradlew.bat' failed with exit code 1
 at ExecState._setResult (D:\a\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.155.0\node_modules\azure-pipelines-task-lib\toolrunner.js:816:25)
 at ExecState.CheckComplete (D:\a\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.155.0\node_modules\azure-pipelines-task-lib\toolrunner.js:799:18)
 at ChildProcess.<anonymous> (D:\a\_tasks\Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4\2.155.0\node_modules\azure-pipelines-task-lib\toolrunner.js:721:19)
 at emitTwo (events.js:106:13)
 at ChildProcess.emit (events.js:191:7)
 at maybeClose (internal/child_process.js:886:16)
 at Socket.<anonymous> (internal/child_process.js:342:11)
 at emitOne (events.js:96:13)
 at Socket.emit (events.js:188:7)

@ldco2016 I am receiving the same error on App Center (should be same as Azure). Were you able to find a solution?

If anyone still facing this problem when using the monorepo approach please pay attention to android/settings.gradle:

apply from: file("../../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings, "../../..")
include ':app'

and android/app/build.gradle:

apply from: file("../../../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project, "../../..")
include ':app'

applyNativeModulesSettingsGradle and applyNativeModulesAppBuildGradle has second parameter root (relative path to folder with settings) which is passed to gradle to find react-native.config.js

Cleaning gradle cache works for me

Stop gradlew on Windows:
gradlew --stop

Stop gradlew on Mac or Linux:
./gradlew --stop

Clean cache on Windows:
gradle cleanBuildCache

Clean cache on Mac or Linux:
./gradlew cleanBuildCache

In my case bash was not installed in the Docker (build) container.

I fixed the problem.
Because of custom paths (I have essentially a form of mono-repo with many sub projects)
PATH=$(npm bin):$PATH react-native config
could not find where my root android part of the project was (it was not in standard directory relative to the JS part of the project)

I am not sure if this nuance is covered in
https://github.com/react-native-community/cli/blob/master/docs/autolinking.md
It talks about how to tell cli, in 'custom root' configuration , about the location of JS part of the project, but does not tell how to tell the cli tools about the the location of android side of the project

So all I had to do, to fix this specific problem, is to specify sourceDir and packageName in the react-native.config.js


module.exports = {
  project: {
    ios: {},
    android: {
    "sourceDir": "../a/myapp1b/app/src",
    "packageName":"com.myapp1b.myapp"

    },
  },
  assets: ['./src/js.app/img'],
};

@vladp How do i perform PATH=$(npm bin):$PATH react-native config in windows, and where can i keep react-native.config.js in my project.

and which library can add a react-native.config.js

@cmcaboy I'm also seeing this same error in AppCenter. Did you find a solution that worked?

Please note, that
react-native-community/cli in RN 0.61 had remove the functionality where we can specify the custom RN root using app/build.gradle root: directive in project.ext.react

project.ext.react = [

        entryFile: "index.android.js",
        // whether to bundle JS and assets in debug mode
        bundleInDebug: false,
        // whether to bundle JS and assets in release mode
        bundleInRelease: true,
        // https://facebook.github.io/react-native/docs/performance#enable-the-ram-format
        bundleCommand: "ram-bundle",
        enableHermes: false,  // clean and rebuild if changing

        // the root of your RN project, i.e. where "package.json" lives
        root: "${myRnAppRootDir_abs}"
]

So starting from RN 0.61 if you have RN root folder located as a sibling or underneath of a sibling of your Android app directory, the CLI will not find it and Autolinking will fail

See my bug report at
https://github.com/react-native-community/cli/issues/804


@tarunvella

how do I perform PATH=$(npm bin):$PATH react-native config

on windows, just
cd < your react native root, where node_modules in installed>

and do
node_modules/.bin/react-native config

You do not need to this manually, as CLI's autolinking code does that.
The code knows where to go, because you had to specify your RN root in
in app/buid.gradle project.ext.react
variable.

The best guide for this, unfortutely, is just to carefull follow diff report between your current version
and RN 0.60
https://github.com/react-native-community/rn-diff-purge

I deleted my build folder and it worked.

Cleaning gradle cache works for me

Stop gradlew on Windows:
gradlew --stop

Stop gradlew on Mac or Linux:
./gradlew --stop

Clean cache on Windows:
gradle cleanBuildCache

Clean cache on Mac or Linux:
./gradlew cleanBuildCache

FAILURE: Build failed with an exception.

BUILD FAILED in 7s

This was the error that appear to me when I wrote this commands.

Was this page helpful?
0 / 5 - 0 ratings