react-native: Build from source fails at, could not find ANDROID_NDK_VERSION

Description

I am trying to build from source, and it seems I have set up everything correctly but the build still fails.with the following error. Any help would be appreciated

❯ ./gradlew clean

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/ashish/projects/tarana/node_modules/react-native/ReactAndroid/build.gradle' line: 450

* What went wrong:
A problem occurred evaluating project ':ReactAndroid'.
> Could not get unknown property 'ANDROID_NDK_VERSION' for extension 'android' of type com.android.build.gradle.LibraryExtension.

* 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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.9/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 6s

Version

0.67.2

Output of npx react-native info

❯ npx react-native info
warn Package lint-staged has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in /Users/ashish/projects/tarana/node_modules/lint-staged/package.json
info Fetching system and libraries information...
System:
    OS: macOS 11.6.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
    Memory: 515.24 MB / 20.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.10 - ~/n/bin/node
    Yarn: 1.22.11 - ~/n/bin/yarn
    npm: 6.14.16 - ~/n/bin/npm
    Watchman: 2022.01.03.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.2 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3
    Android SDK:
      API Levels: 28, 29, 30, 31, 32
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 31.0.0, 32.0.0, 32.1.0
      System Images: android-29 | Google APIs Intel x86 Atom
      Android NDK: 21.4.7075529
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7935034
    Xcode: 13.2.1/13C100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.11 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: Not Found
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

I ran ./gradlew clean and it appeared

Snack, code example, screenshot, or link to a repository

Not available

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 24 (8 by maintainers)

Most upvoted comments

Was experiencing a similar issue after upgrading to react-native@0.68.1

The proposed addition to settings.gradle did not help:

includeBuild('../node_modules/react-native/packages/react-native-gradle-plugin') {
   dependencySubstitution {
       substitute(module("com.facebook.react:react-native-gradle-plugin")).using(project(":"))
   }
}

but using the following line

includeBuild('../node_modules/react-native-gradle-plugin')

image

from the upgrade guide did help. https://react-native-community.github.io/upgrade-helper/?from=0.67.4&to=0.68.1

A problem occurred configuring root project ‘tarana’.

Could not resolve all artifacts for configuration ‘:classpath’. Could not find com.facebook.react:react-native-gradle-plugin:.

The fix for this specific issue is to update your settings.gradle as follows:

-includeBuild('../node_modules/react-native/packages/react-native-gradle-plugin')
+includeBuild('../node_modules/react-native/packages/react-native-gradle-plugin') {
+   dependencySubstitution {
+       substitute(module("com.facebook.react:react-native-gradle-plugin")).using(project(":"))
+   }
+}

The problem is that you’re using an older revision of the repo, therefore the wiki is updated to use the latest main. Building from source from different revision is generally not supported (i.e. you’re on your own) as there are those kinds of incompatibilities, and we can’t provide instructions for all the possible RN versions/branches.

Adding this in build.gradle

classpath("com.facebook.react:react-native-gradle-plugin")

throws this error

yarn android
yarn run v1.22.11
$ react-native run-android
warn Package lint-staged has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in /Users/ashish/projects/tarana/node_modules/lint-staged/package.json
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1113 file(s) to forward-jetify. Using 8 workers...
info JS server already running.
info Installing the app...

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'tarana'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not find com.facebook.react:react-native-gradle-plugin:.
     Required by:
         project :

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

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'tarana'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not find com.facebook.react:react-native-gradle-plugin:.
     Required by:
         project :

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

    at makeError (/Users/ashish/projects/tarana/node_modules/execa/index.js:174:9)
    at /Users/ashish/projects/tarana/node_modules/execa/index.js:278:16
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async runOnAllDevices (/Users/ashish/projects/tarana/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5)
    at async Command.handleAction (/Users/ashish/projects/tarana/node_modules/@react-native-community/cli/build/index.js:192:9)
info Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

settings.gradle::

includeBuild('../node_modules/react-native-gradle-plugin') {
   dependencySubstitution {
       substitute(module("com.facebook.react:react-native-gradle-plugin")).using(project(":"))   }
}

Was experiencing a similar issue after upgrading to react-native@0.68.1

The proposed addition to settings.gradle did not help:

includeBuild('../node_modules/react-native/packages/react-native-gradle-plugin') {
   dependencySubstitution {
       substitute(module("com.facebook.react:react-native-gradle-plugin")).using(project(":"))
   }
}

but using the following line

includeBuild('../node_modules/react-native-gradle-plugin')

image

from the upgrade guide did help. https://react-native-community.github.io/upgrade-helper/?from=0.67.4&to=0.68.1

This helped a ton. I missed it out cause my brain was dead and I was filtering out changes that were for the new architecture which made me miss over that change while skimming

Does that mean that you need to add react-native-gradle-plugin inside your package.json dependencies ?

@cortinico is it possible for the Wiki to be moved into the repo itself?

Not really, but we do have the reactnative.dev website which is versioned. So we could move this page there 👍

@KunalFarmah98 did you delete your comment can you tell me which file it was that i need to update with NDK path ?

@afkcodes Actually this was not working as gradle.properties only supports strings and no function calls. The build.gradle of ReactAndroid suggest that we hardcode the NDK_VERSION and PATH in the properties file but that is not a good practice for an app that is maintained by a lot of developers. So I ended up modifying the code of build.gradle for ReactAndroid by using environment variables for both NDK_VERSION and PATH.

I added the following lines in their build.gradle file:

def ANDROID_NDK_VERSION = System.getenv(‘ANDROID_NDK_VERSION’) def ANDROID_NDK_PATH = System.getenv(‘ANDROID_NDK_PATH’).

Now the code syncs fine.

You might also need to use the ANDROID_NDK_PATH wherever the gradle file needs path for NDK if you experience NDK is not installed bug in their file like me.

I updated the package.json with react native git branch0.67-stable so that it uses the git repo.

You should follow this guide to setup the build from source: https://github.com/facebook/react-native/wiki/Building-from-source

One of the prerequisite of the guide is to git clone react native inside node_modules.

We haven’t tested the wiki with a Github dependency such as facebook/react-native#0.67-stable.

Also, why are you trying to build from 0.67-stable. The assumption behind build from source is that you’ll use the latest main.

Adding this in build.gradle throws this error

Have you followed all the steps in the Wiki I linked? Also the changes to the settings.gradle file?