Mateusz Herych
Mateusz Herych

Reputation: 1605

react-native can't find gradle wrapper when building the android app

I'm in a process of upgrading React Native from 0.61.5 to 0.64.2 and I have a problem running the Android app.

This is the output I get when trying to start it:

$ react-native run-android --verbose
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
(node:14959) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
Jetifier found 1313 file(s) to forward-jetify. Using 12 workers...
info JS server already running.
info Installing the app...
debug Running command "cd android && ./gradlew installDebug -PreactNativeDevServerPort=8081"

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: spawn ./gradlew ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:269:19)
    at onErrorNT (internal/child_process.js:465:16)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

My Android environment is configured, and I can actually run the exact same command that react-native runs, and it works without the issue:

$ cd android && ./gradlew installDebug -PreactNativeDevServerPort=8081
<-------------> 0% CONFIGURING [7s]
> root project > Resolve dependencies of :classpath > maven-metadata.xml

(gradle eventually finishes the build with a success).

Permissions for gradlew seem ok:

$ ls -la ./android/gradlew
-rwxr-xr-x  1 mateusz  139652859  5766 20 Jul 13:44 ./android/gradlew

gradle-wrapper.properties looks as follows:

$ cat android/gradle/wrapper/gradle-wrapper.properties 
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

React Native CLI is up-to-date:

$ react-native --version
react-native-cli: 2.0.1
react-native: 0.64.2

I found several people had a similar problem but none of them to be exactly the same.

Things work fine on the main branch which still uses 0.61.5. I suspect it may be just me not updating some config file, however I have no idea how to get some more verbose hint on what exactly I have to change.

Any ideas? Thanks!

Upvotes: 1

Views: 2382

Answers (1)

Mateusz Herych
Mateusz Herych

Reputation: 1605

Turns out react-native was looking for gradlew in a wrong directory!

It looked for it in android/myApp, whereas it should be just android/.

What fixed the issue was a config change in react-native.config.js:

What I had before:

const android = require('@react-native-community/cli-platform-android');

module.exports = {
  platforms: {
    android: {
      linkConfig: android.linkConfig,
      projectConfig: android.projectConfig,
      dependencyConfig: android.dependencyConfig,
    },
  },
  project: {
    android: {
      sourceDir: './android/myApp',
      stringsPath: './src/main/res/values/strings.xml',
      manifestPath: './src/main/AndroidManifest.xml',
      buildGradlePath: './build.gradle',
      settingsGradlePath: '../settings.gradle',
      assetsPath: './src/main/assets',
      mainFilePath: './src/main/java/com/company/myapp/MyApp.java',
    },
  },
};

What I have now (it works):

const android = require('@react-native-community/cli-platform-android');

module.exports = {
  platforms: {
    android: {
      linkConfig: android.linkConfig,
      projectConfig: android.projectConfig,
      dependencyConfig: android.dependencyConfig,
    },
  },
  project: {
    android: {
      sourceDir: './android',
      stringsPath: './myApp/src/main/res/values/strings.xml',
      manifestPath: './myApp/src/main/AndroidManifest.xml',
      buildGradlePath: './build.gradle',
      settingsGradlePath: './settings.gradle',
      assetsPath: './myApp/src/main/assets',
      mainFilePath: './myApp/src/main/java/com/company/myapp/MyApp.java',
    },
  },
};

Upvotes: 1

Related Questions