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

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/cli@2.8.0 --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.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 15

Most upvoted comments

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

I deleted my build folder and it worked.

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'],
};


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

@Cuadros99 i got this error too but in linux when i init new react-native project in react-native-cli i’ve faced with this error every time

Could not load compiled classes for script ‘…/node_modules/@react-native-community/cli-platform-android/native_modules.gradle’ from cache.