Could not find method exec() for arguments. Android Build Error
Asked Answered
T

3

5

react-native project randomly started failing with

Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method exec() for arguments [ReactNativeModules$_getCommandOutput_closure16@45d8ac1c] on object of type org.gradle.api.internal.provider.DefaultProviderFactory_Decorated.

while running yarn android

It starts the JS server starts the emulator then fails

We have not changed anything in gradle and all versions are mentioned.

Original CLI error

A problem occurred evaluating script. Could not find method exec() for arguments [ReactNativeModules$_getCommandOutput_closure16@2763a6b8] on object of type org.gradle.api.internal.provider.DefaultProviderFactory_Decorated.

Path/package where error is occuring

node_modules/@react-native-community/cli-platform-android/native_modules.gradle

Exact code snippet where error is occuring


  /**
   * Runs a specified command using providers.exec in a specified directory.
   * Throws when the command result is empty.
   */
  String getCommandOutput(String[] command, File directory) {
    try {
      def execOutput = providers.exec { // the error is occuring here
        commandLine(command)
        workingDir(directory)
      }
      def output = execOutput.standardOutput.asText.get().trim()
      if (!output) {
        this.logger.error("${LOG_PREFIX}Unexpected empty result of running '${command}' command.")
        def error = execOutput.standardError.asText.get().trim()
        throw new Exception(error)
      }
      return output
    } catch (Exception exception) {
      this.logger.error("${LOG_PREFIX}Running '${command}' command failed.")
      throw exception
    }
  }
Twibill answered 26/4 at 13:3 Comment(0)
P
4

Temporary workaround until the issue is addressed by RN CLI team:

Source: https://github.com/react-native-community/cli/issues/1981#issuecomment-2085242422

I found the solution to RN cli issues specifically for older RN versions as pointed by @nhokmoon. I am using RN 68.7. I added resolutions to my package.json to force use older RN CLI versions:

"resolutions": {
    "@react-native-community/cli": "^7.0.0",
    "@react-native-community/cli-config": "^7.0.0",
    "@react-native-community/cli-debugger-ui": "^7.0.0",
    "@react-native-community/cli-doctor": "^7.0.0",
    "@react-native-community/cli-platform-android": "^7.0.0",
    "@react-native-community/cli-platform-ios": "^7.0.0",
    "@react-native-community/cli-server-api": "^7.0.0",
    "@react-native-community/cli-tools": "^7.0.0",
    "@react-native-community/cli-types": "^7.0.0"   
} 

And now the Android build is succeeding πŸš€.

Check the compatible version of RN CLI version for your RN version: https://github.com/react-native-community/cli?tab=readme-ov-file#compatibility

Periphery answered 22/5 at 15:1 Comment(0)
H
2

I had this issue on my project, it seems that the latest react-native 0.74 version introduced issues on the @react-native-community/cli-platform-android library.

So you have to update your yarn.lock or package-lock.json file and search for the line react-native@*: then you have to remove all of dependencies and paste this code instead:

react-native@*:
  version β€œ0.73.6”
  resolved β€œhttps://registry.yarnpkg.com/react-native/-/react-native-0.73.6.tgz#ed4c675e205a34bd62c4ce8b9bd1ca5c85126d5b”
  integrity "your integrity sha512"
  dependencies:
    β€œ@jest/create-cache-key-function” β€œ^29.6.3"
    β€œ@react-native-community/cli” β€œ12.3.6"
    β€œ@react-native-community/cli-platform-android” β€œ12.3.6"
    β€œ@react-native-community/cli-platform-ios” β€œ12.3.6"
    β€œ@react-native/assets-registry” β€œ0.73.1"
    β€œ@react-native/codegen” β€œ0.73.3"
    β€œ@react-native/community-cli-plugin” β€œ0.73.17"
    β€œ@react-native/gradle-plugin” β€œ0.73.4"
    β€œ@react-native/js-polyfills” β€œ0.73.1"
    β€œ@react-native/normalize-colors” β€œ0.73.2"
    β€œ@react-native/virtualized-lists” β€œ0.73.4"
    abort-controller β€œ^3.0.0”
    anser β€œ^1.4.9"
    ansi-regex β€œ^5.0.0”
    base64-js β€œ^1.5.1"
    chalk β€œ^4.0.0”
    deprecated-react-native-prop-types β€œ^5.0.0"
    event-target-shim β€œ^5.0.1”
    flow-enums-runtime β€œ^0.0.6"
    invariant β€œ^2.2.4”
    jest-environment-node β€œ^29.6.3"
    jsc-android β€œ^250231.0.0”
    memoize-one β€œ^5.0.0"
    metro-runtime β€œ^0.80.3”
    metro-source-map β€œ^0.80.3"
    mkdirp β€œ^0.5.1”
    nullthrows β€œ^1.1.1"
    pretty-format β€œ^26.5.2”
    promise β€œ^8.3.0"
    react-devtools-core β€œ^4.27.7”
    react-refresh β€œ^0.14.0"
    react-shallow-renderer β€œ^16.15.0”
    regenerator-runtime β€œ^0.13.2"
    scheduler β€œ0.24.0-canary-efb381bbf-20230505”
    stacktrace-parser β€œ^0.1.10"
    whatwg-fetch β€œ^3.0.0”
    ws β€œ^6.2.2"
    yargs β€œ^17.6.2”

After that, remove your node-modules folder and install all again.

Homeric answered 26/4 at 22:45 Comment(2)
worked for me ! Thanks , Make sure we keep 12.3.6 version of @react-native-community/cli with 0.73.6 version of react-native. – Sheath
No problem, @JayantKumar yep we have to keep @react-native-community/cli: 12.3.6 version – Homeric
S
0

Got same exception after upgrde to new version (0.74) of RN.

In my case the problem was an old android\gradlew.bat It was created with previos RN version.

I copied gradlew.bat from template project, and this issue moved away.

According to RN docs, you may create reference project with command

npx @react-native-community/cli@latest init TestProject

P.S. There are a lot other differences in *.gradle files comes with new version of RN.

Skywriting answered 21/5 at 10:16 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Cistern

© 2022 - 2024 β€” McMap. All rights reserved.