cli icon indicating copy to clipboard operation
cli copied to clipboard

@react-native-community/cli-platform-android Build failed with an exception

Open b1ngx opened this issue 7 years ago • 57 comments

Environment

react-native info

yarn run v1.19.1
$ /Users/b1ng/rn/haokan/node_modules/.bin/react-native info
info Fetching system and libraries information...
System:
    OS: macOS 10.15.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 53.23 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.12.0 
    Yarn: 1.19.1 
    npm: 6.12.1 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5900203
    Xcode: /undefined - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.4 => 0.61.4
✨  Done in 2.66s.

react-native --version

yarn run v1.19.1
$ /Users/b1ng/rn/haokan/node_modules/.bin/react-native --version
3.0.0-alpha.7
✨  Done in 0.32s.

Description

yarn android

yarn run v1.19.1
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 864 file(s) to forward-jetify. Using 4 workers...
info Starting JS server...
info Installing the app...

FAILURE: Build failed with an exception.

* Where:
Script '/Users/b1ng/rn/haokan/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 182

* What went wrong:
A problem occurred evaluating script.
> Unable to determine the current character, it is not a string, number, array, or object

  The current character read is 'i' with an int value of 105
  Unable to determine the current character, it is not a string, number, array, or object
  line number 1
  index number 0
  info Run "react-native --help" to see a list of all available commands.
  ^

* 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 1s

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* Where:
Script '/Users/b1ng/rn/haokan/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 182

* What went wrong:
A problem occurred evaluating script.
> Unable to determine the current character, it is not a string, number, array, or object

  The current character read is 'i' with an int value of 105
  Unable to determine the current character, it is not a string, number, array, or object
  line number 1
  index number 0
  info Run "react-native --help" to see a list of all available commands.
  ^

* 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 1s

    at checkExecSyncError (child_process.js:621:11)
    at execFileSync (child_process.js:639:15)
    at runOnAllDevices (/Users/b1ng/rn/haokan/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:94:39)
    at buildAndRun (/Users/b1ng/rn/haokan/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/index.js:158:41)
    at /Users/b1ng/rn/haokan/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/index.js:125:12
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Command.handleAction (/Users/b1ng/rn/haokan/node_modules/@react-native-community/cli/build/index.js:164:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Reproducible Demo

b1ngx avatar Nov 07 '19 08:11 b1ngx

We use npx because it should be universally available along with Node.js. Using yarn to install deps shouldn't matter, but we've seen some reports of npx not behaving as advertised.

thymikee avatar Dec 18 '19 10:12 thymikee

It stopped working when upgrading react-native from 0.61.4 to 0.61.5. But then after downgrading the problem is still happening so it could also have something to do with the global CI config (app center). Let me investigate this further.

jfrolich avatar Dec 18 '19 10:12 jfrolich

It's definitely the npx command that not behaving well. The old version of cli-platform-android works perfectly fine. The working version is: "3.0.0-alpha.2"

jfrolich avatar Dec 18 '19 11:12 jfrolich

For other people that are affected, this in your package.json will fix it for you:

  "resolutions": {
    "@react-native-community/cli-platform-android": "3.0.0-alpha.2"
  },

jfrolich avatar Dec 18 '19 11:12 jfrolich

Uncommenting /* dir */ also fixes the issue for me. Here are more specifics:

  • This is happening on Microsoft AppCenter
  • My React Native app does not live in the root of the repo. It's in a mobile/ directory.
  • AppCenter doesn't cd mobile. Instead it's just firing off mobile/android/gradlew from the root
  • Because the second argument dir is commented out, I think npx is being run at the root of my repo, which isn't going to work.

rzane avatar Dec 18 '19 20:12 rzane

I believe there are several duplicate issues here:

  • https://github.com/react-native-community/cli/issues/804
  • https://github.com/react-native-community/cli/issues/854
  • https://github.com/react-native-community/cli/issues/863
  • https://github.com/react-native-community/cli/issues/890

They appear to be a result of this change: https://github.com/react-native-community/cli/pull/852

rzane avatar Dec 18 '19 20:12 rzane

Uncommenting /* dir */ also fixes the issue for me. Here are more specifics:

  • This is happening on Microsoft AppCenter
  • My React Native app does not live in the root of the repo. It's in a mobile/ directory.
  • AppCenter doesn't cd mobile. Instead it's just firing off mobile/android/gradlew from the root
  • Because the second argument dir is commented out, I think npx is being run at the root of my repo, which isn't going to work.

Ah! this is exactly the same situation as we have!

jfrolich avatar Dec 19 '19 05:12 jfrolich

I indeed removed 'react-native-cli' and 'react-native' global packages with

yarn global remove react-native-cli react-native and npm uninstall -g react-native-cli react-native

But I guess I only really needed to enter android folder and run ./gradlew clean

Kinda of what @sagargulati pointed out. Except I did not edit the native_modules.gradle file.

I wish you all nice holidays

AlexandreLage avatar Dec 24 '19 01:12 AlexandreLage

manually removed the caches and daemon folders in _%HOME%/user/.gradle/_ then navigate to android folder in your project i.e. /myproject/android/ run _gradlew --stop_ // this stops the running daemon

then run gradlew clean

might take a while to rebuild so b patient

** Happy Holidays and Happy Coding.**

cahhvoy avatar Dec 29 '19 23:12 cahhvoy

I indeed removed 'react-native-cli' and 'react-native' global packages with

yarn global remove react-native-cli react-native and npm uninstall -g react-native-cli react-native

But I guess I only really needed to enter android folder and run ./gradlew clean

Kinda of what @sagargulati pointed out. Except I did not edit the native_modules.gradle file.

I wish you all nice holidays

Do you also have a monorepo where the app resides in a subfolder when compiling on app center?

jfrolich avatar Dec 30 '19 01:12 jfrolich

None of the answers works,

I run npm uninstall -g react-native-cli react-native for being sure that the tools removed globally. but I just got up to date in 0.034s message in cmd. (I don't know it's normal or not) still doesn't fix.

here is my result of npx.cmd --quiet --no-install react-native config

{
  "root": "C:\\Users\\SAEED\\Desktop\\MyTSProject",
  "reactNativePath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native",
  "dependencies": {
    "react-native-svg": {
      "root": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg",
      "name": "react-native-svg",
      "platforms": {
        "ios": {
          "sourceDir": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg\\ios",
          "folder": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg",
          "pbxprojPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg\\ios\\RNSVG.xcodeproj\\project.pbxproj",
          "podfile": null,
          "podspecPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg\\RNSVG.podspec",
          "projectPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg\\ios\\RNSVG.xcodeproj",          "projectName": "RNSVG.xcodeproj",
          "libraryFolder": "Libraries",
          "sharedLibraries": [],
          "plist": [],
          "scriptPhases": []
        },
        "android": {
          "sourceDir": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg\\android",
          "folder": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\node_modules\\react-native-svg",
          "packageImportPath": "import com.horcrux.svg.SvgPackage;",
          "packageInstance": "new SvgPackage()"
        }
      },
      "assets": [],
      "hooks": {},
      "params": []
    }
  },
  "commands": [
    {
      "name": "log-ios",
      "description": "starts iOS device syslog tail"
    },
    {
      "name": "run-ios",
      "description": "builds your app and starts it on iOS simulator",
      "examples": [
        {
          "desc": "Run on a different simulator, e.g. iPhone 5",
          "cmd": "react-native run-ios --simulator \"iPhone 5\""
        },
        {
          "desc": "Pass a non-standard location of iOS directory",
          "cmd": "react-native run-ios --project-path \"./app/ios\""
        },
        {
          "desc": "Run on a connected device, e.g. Max's iPhone",
          "cmd": "react-native run-ios --device \"Max's iPhone\""
        },
        {
          "desc": "Run on the AppleTV simulator",
          "cmd": "react-native run-ios --simulator \"Apple TV\"  --scheme \"helloworld-tvOS\""
        }
      ],
      "options": [
        {
          "name": "--simulator [string]",
          "description": "Explicitly set simulator to use. Optionally include iOS version betweenparenthesis at the end to match an exact version: \"iPhone 6 (10.0)\"",
          "default": "iPhone 11"
        },
        {
          "name": "--configuration [string]",
          "description": "Explicitly set the scheme configuration to use",
          "default": "Debug"
        },
        {
          "name": "--scheme [string]",
          "description": "Explicitly set Xcode scheme to use"
        },
        {
          "name": "--project-path [string]",
          "description": "Path relative to project root where the Xcode project (.xcodeproj) lives.",
          "default": "ios"
        },
        {
          "name": "--device [string]",
          "description": "Explicitly set device to use by name.  The value is not required if you have a single device connected."
        },
        {
          "name": "--udid [string]",
          "description": "Explicitly set device to use by udid"
        },
        {
          "name": "--no-packager",
          "description": "Do not launch packager while building"
        },
        {
          "name": "--verbose",
          "description": "Do not use xcpretty even if installed"
        },
        {
          "name": "--port [number]",
          "default": 8081
        },
        {
          "name": "--terminal [string]",
          "description": "Launches the Metro Bundler in a new window using the specified terminal path."
        }
      ]
    },
    {
      "name": "log-android",
      "description": "starts logkitty"
    },
    {
      "name": "run-android",
      "description": "builds your app and starts it on a connected Android emulator or device",
      "options": [
        {
          "name": "--root [string]",
          "description": "Override the root directory for the android build (which contains the android directory)",
          "default": ""
        },
        {
          "name": "--variant [string]",
          "description": "Specify your app's build variant",
          "default": "debug"
        },
        {
          "name": "--appFolder [string]",
          "description": "Specify a different application folder name for the android source. If not, we assume is \"app\"",
          "default": "app"
        },
        {
          "name": "--appId [string]",
          "description": "Specify an applicationId to launch after build.",
          "default": ""
        },
        {
          "name": "--appIdSuffix [string]",
          "description": "Specify an applicationIdSuffix to launch after build.",
          "default": ""
        },
        {
          "name": "--main-activity [string]",
          "description": "Name of the activity to start",
          "default": "MainActivity"
        },
        {
          "name": "--deviceId [string]",
          "description": "builds your app and starts it on a specific device/simulator with the given device id (listed by running \"adb devices\" on the command line)."
        },
        {
          "name": "--no-packager",
          "description": "Do not launch packager while building"
        },
        {
          "name": "--port [number]",
          "default": 8081
        },
        {
          "name": "--terminal [string]",
          "description": "Launches the Metro Bundler in a new window using the specified terminal path."
        },
        {
          "name": "--tasks [list]",
          "description": "Run custom Gradle tasks. By default it's \"installDebug\""
        },
        {
          "name": "--no-jetifier",
          "description": "Do not run \"jetifier\" – the AndroidX transition tool. By default it runs before Gradle to ease working with libraries that don't support AndroidX yet. See more at: https://www.npmjs.com/package/jetifier.",
          "default": false
        }
      ]
    }
  ],
  "assets": [],
  "platforms": {
    "ios": {},
    "android": {}
  },
  "haste": {
    "providesModuleNodeModules": [
      "react-native"
    ],
    "platforms": [
      "ios",
      "android"
    ]
  },
  "project": {
    "ios": {
      "sourceDir": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\ios",
      "folder": "C:\\Users\\SAEED\\Desktop\\MyTSProject",
      "pbxprojPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\ios\\MyTSProject.xcodeproj\\project.pbxproj",
      "podfile": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\ios\\Podfile",
      "podspecPath": null,
      "projectPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\ios\\MyTSProject.xcodeproj",
      "projectName": "MyTSProject.xcodeproj",
      "libraryFolder": "Libraries",
      "sharedLibraries": [],
      "plist": [],
      "scriptPhases": []
    },
    "android": {
      "sourceDir": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\app",
      "isFlat": false,
      "folder": "C:\\Users\\SAEED\\Desktop\\MyTSProject",
      "stringsPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\app\\src\\main\\res\\values\\strings.xml",
      "manifestPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\app\\src\\main\\AndroidManifest.xml",
      "buildGradlePath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\app\\build.gradle",
      "settingsGradlePath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\settings.gradle",
      "assetsPath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\app\\src\\main\\assets",
      "mainFilePath": "C:\\Users\\SAEED\\Desktop\\MyTSProject\\android\\app\\src\\main\\java\\com\\mytsproject\\MainApplication.java",
      "packageName": "com.mytsproject"
    }
  }
}

SaeedZhiany avatar Jan 06 '20 14:01 SaeedZhiany

check if your running react-native run-android twice in a separate terminal that could be causing the problem

nabil-hamisa avatar Jan 08 '20 22:01 nabil-hamisa

it's for a few days ago, so I can't remember exactly, but usually I start one react-native instance (using react-native start) to make sure all of next commands using same react-native instance process and then run react-native run-android instances. However, I'll check it again, Thanks

SaeedZhiany avatar Jan 09 '20 06:01 SaeedZhiany

it's for a few days ago, so I can't remember exactly, but usually I start one react-native instance (using react-native start) to make sure all of next commands using same react-native instance process and then run react-native run-android instances. However, I'll check it again, Thanks

Pretty much what worked for me. I wasn't running react-native start before react-native run-android. That and doing cd android then gradlew clean also helps.

GoldenMaximo avatar Jan 10 '20 15:01 GoldenMaximo

Surprisingly, a lot of issues here were resolved by doing something totally irrelevant to the CLI.

I believe the title of this issue is misleading because many builds can "fail with an exception". This is the standard Android failure that can happen on many occasions.

I am going to close this issue and encourage you to open other issues in the future with more verbose title/description to not confuse anyone.

Also, npx is going away in the next release - #886 - so many of these errors should be resolved as well.

grabbou avatar Jan 13 '20 13:01 grabbou

same issue still persists but i found a work around : cd into the android app of your app and run gradlew --stop then run gradlew clean i found it works better on a terminal with elevated privileges

after it finishes run your app the normal way

cahhvoy avatar Jan 19 '20 09:01 cahhvoy

I was seeing the same issue. I did the following to fix it:

  • Delete node_modules/ & /android/app/build
  • install dependencies npm install
  • Open project in android studio, and make a build
  • Profit

running npx react-native run-android now works again 😆

camilo86 avatar Jan 23 '20 03:01 camilo86

Okay for me it was something different. I was using nvm with lower node(7.4.0) as a default version. I thought I specified nvm to use latest for this Project folder, but it did not work.

After setting latest node as global default in nvm it fixed the issue.

Also, funny thing is project was running fine with run-android but would not sync in Android Studio.

andriadze avatar Jan 26 '20 18:01 andriadze

yarn add @react-native-community/[email protected] yarn add [email protected]

Simply , below code solved for me :

yarn add @react-native-community/[email protected]
yarn add [email protected]

iamadityaaz avatar Jan 26 '20 19:01 iamadityaaz

@trrfrr

Sorry for late, I created a new project using

npx react-native init TestProject --template react-native-template-typescript

When I run react run-android it opens a new cmd to start nodejs (so I'm pretty sure always one run-android command is running) but when it tries to build android apk it gives me this error:

unlike to @b1ngx's log file, I have an error in line 200. I check the line in native_modules.gradle file, an exception thrown at the line that its message is already printed in the console log, so it does not help me to find what is wrong.

:ReactNative:Failed to parse React Native CLI configuration: java.lang.RuntimeException: Unable to load FastStringService

FAILURE: Build failed with an exception.

* Where:
Script 'C:\Users\SAEED\workspace\TestProject\node_modules\@react-native-community\cli-platform-android\native_modules.gradle' line: 200

* What went wrong:
A problem occurred evaluating script.
> Failed to parse React Native CLI configuration. Expected running 'npx.cmd --quiet --no-install react-native config' command from 'C:\Users\SAEED\workspace\TestProject' directory to output valid JSON, but it didn't. This may be caused by npx resolving to a legacy global react-native binary. Please make sure to uninstall any global 'react-native' binaries: 'npm uninstall -g react-native react-native-cli' and try again

* 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 2s

@iamadityaaz I checked cli-platform-android version, the init command install version 3.0.3 itself, so there is no need to installing it manually

image

Also, I have previously uninstalled the global react-native and react-native-cli packages

SaeedZhiany avatar Jan 29 '20 14:01 SaeedZhiany