question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android: latest release broke support custom react-native Root

See original GitHub issue

Environment

System: OS: Windows 10 CPU: (8) x64 Intel® Core™ i Memory: 15.44 GB Binaries: Node: 12.10.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.19.1 - C:\Users\v\AppData\Roaming\npm\yarn.CMD npm: 6.12.0 - C:\Program Files\nodejs\npm.CMD npmPackages: react: 16.9.0 => 16.9.0 react-native: ^0.61 => 0.61.2

Description

my directory structure is andrapp\ <-- has android ./app , gradlew and so on rn.common\ <-- has package.json index.android.js and ./node_modules

in react-native 60.6 I was able to specify the React native root (in app/build.gradle)


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,
        bundleCommand: "ram-bundle",
        enableHermes: false,  // clean and rebuild if changing
        **root:** "${myRnAppRootDir_abs}"
]

And cli was able to correctly invoke
npx.cmd --quiet react-native config
from within rn.common directory

However since this change https://github.com/react-native-community/cli/commit/5724d29dc979a4b645c620109348a1e5c8c74d03#diff-375026a0607eb034a6fc70cca7d74689

native_modules.gradle Now invokes npx in ‘default’ directly, not in React’s project root. Which causes npx.cmd --quiet react-native config to complain with

error Unrecognized command "config".
info Run "react-native --help" to see a list of all available commands.

Which, then, causes JSON parsing error in native_modules.js

Here is code change in native_modules.js that causes the error on Android

// old code, that was working cmdProcess = Runtime.getRuntime().exec(command, null, root)

// new code that does not work cmdProcess = Runtime.getRuntime().exec(command)

From the design/documentation perspective, it seems that the whole infrastructure for getReactNativeProjectRoot() have been removed, and no alternative/migration documentation has been suggested.

Note also, that react-native 0.61 project template, continues to assert that configuring custom root (where package.json) resides, by specifying root in project.ext.react (in app/build.gradle) – is still a valid and supported option

https://github.com/facebook/react-native/blob/master/template/android/app/build.gradle

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
vladpcommented, Apr 21, 2020

@thymikee Please re-open this issue. The problem I reported is not resolved.

I am getting '[node, -e, console.log(require('react-native/cli').bin);]' in directory 'C:\Users\v\home\devel\myrepo\FRONTEND\a\u1b' command failed.

React native 0.62. using most recent CLI release 4.7.0

when executing the build from android studio or when running gradlew task In the u1b , the edirectory where I have my android project (see my directory structure fully described October 16, 2019 comment)

FRONTEND-OF-MYAPP
|-- a  (* this is where android app code resides. Android app has just 1 activity that uses RN, RN's js code is rn.common *)
|   `-- u1b


The problem still seems to be same (using 4.7.0 release of cli. Tried both on Windows and Linux). The cli is trying to execute java script commands in my Android app directory (u1b), and not in the correct directory which is rn.common

My settings.gradle is the same as when reported the issue 6 months ago

rootProject.name = 'u1b'
def myRnAppRootDir_abs="${rootDir}/../../rn.common"
def myRnAppRootDir_rel="../../rn.common"
apply from: file("${myRnAppRootDir_abs}/node_modules/@react-native-community/cli-platform-android/native_modules.gradle")
applyNativeModulesSettingsGradle(settings,myRnAppRootDir_rel)
...

Same build.gradle:

plugins {
    id 'com.android.application'
    id("com.github.triplet.play") version "2.6.1"
}


import com.android.build.OutputFile


def myRnAppRootDir_abs="${rootDir}/../../rn.common"
def myRnAppRootDir_rel="../../rn.common"

def myMinSDKVer=21

apply from: 'config/gradle_cfg/quality.gradle'

project.ext.react = [

        entryFile: "index.android.js",
        bundleInDebug: false,
        bundleInRelease: true,
        bundleCommand: "ram-bundle",
        enableHermes: false,  // clean and rebuild if changing
        root: "${myRnAppRootDir_abs}"
]


apply from: "${myRnAppRootDir_abs}/node_modules/react-native/react.gradle"
apply from: "${myRnAppRootDir_abs}/node_modules/react-native-vector-icons/fonts.gradle"

...

apply from: file("${myRnAppRootDir_abs}/node_modules/@react-native-community/cli-platform-android/native_modules.gradle");
applyNativeModulesAppBuildGradle(project,"${myRnAppRootDir_rel}")


NODE_MODULES environment variable is not set

v12.16.1

The problem, clearly the same as before

When settings.gradle invokes applyNativeModulesSettingsGradle(settings) CLI’s node_modules/@react-native-community/cli-platform-android/native_modules.gradle fails to figure out where my JS directory is (rn.common) – because that capability was removed with CLI release in rn 0.61.

To confirm that this is same problem, I modified native_modules.gradle and applied the same solution as @thymikee created before (getting the JS directory from the path of nativ_modules.gradle)

// find current directory and set is as the one to use for subsequent autolinking machinery
def jsAppDir = buildscript.sourceFile.toString().split("node_modules/@react-native-community/cli-platform-android/native_modules.gradle")[0]
def projectRoot =  (jsAppDir != null)? new File(jsAppDir) : rootProject.projectDir;
def autoModules = new ReactNativeModules(logger,projectRoot)

That allowed to build to pass the above mentioned error… But without the above change to CLI code, I simply do not know how to get this to even start the build.

2reactions
josephmbeveridgecommented, Dec 4, 2019

@grabbou I have a monorepo structure set up almost identical to the one you suggested earlier, but the RN CLI doesn’t work properly in root. I don’t want a react-native package in root, and would like to instead force each sub-folder to specify the RN version in their package.json. However, after doing this the CLI can’t find run-android as a command for some reason when running from root.

I checked and I’m sure that react-native, along with all the other packages I need, are correctly hoisted up to root. The react-native command even runs, but it doesn’t think that run-android is a valid sub-command. I’m currently running react-native run-android --root "./projectName". Is there any more configuration that is needed? To note, this works perfectly fine if I leave react-native in the root package.json.

Looking through the global .bin for react-native it appears to kick it to my node_modules/@react-native-community/cli package after going through the react-native one. There is also node_modules/@react-native-community/cli-platform-android which appears to have the commands I am missing, but I’m not sure how the main cli links to it.

Structure just to be clear:

package.json
 -> project1
      package.json
      -> android
      -> ios
 -> project2
      package.json
      -> android
      -> ios

where the sub-projects have essentially this as their package.json

{
  ...
  "dependencies": {
    "react-native": "0.61.5"
    ...
  }
  ...
}

and root has ideally nothing in its dependencies (or very little).

Also please feel free to send me to another issue if this isn’t a good place to ask.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Android build failure with different errors without ...
Reason for Failures : The build failures for Android was due to the publish of the React Native version 0.71.0-rc0 to Maven and...
Read more >
Upgrading from 5.x - React Navigation
To upgrade react-native-safe-area-context and react-native-screens to the latest supported versions, do the following: NOTE: If your react-native Version is ...
Read more >
react-native-community/datetimepicker - npm
Latest version : 6.7.1, last published: a month ago. ... React Native date & time picker component for iOS, Android and Windows.
Read more >
Troubleshooting - React Native
These are some common issues you may run into while setting up React Native. If you encounter something that is not listed here, ......
Read more >
Design and Develop an Android App with React Native and ...
In order to build the eventual APK (the format of the app you'll upload to the store), you need to install Android Studio....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found