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.

Cannot open new React-Native project in Android Studio - Gradle sync fails

See original GitHub issue

Description

I am upgrading an existing React-Native project from 0.64.1 to 0.66.3. My project includes java native code for Android, therefore I cannot use Expo and I do a lot of coding in Android Studio to get the intellisense support for Java code and so on that it offers.

After upgrading from RN .64 to .66, I can get it to build and run on Android using the gradlew wrapper command line tool. The apk that is built installs and works correctly. I use Visual Studio Code for Javascript coding and the metro bundler and debugging tools that come with the React Native Tools extension

However, when it came time to modify the Java Native code, I brought up the Android project in Android Studio and immediately received an error from Gradle sync. It cannot build due to an exception.

After much debugging, I created a blank brand new React-Native project and simply brought it up in Android Studio as soon as it was created without modification and received the same exception:

/home/cschlesiger/Projects/newRN/node_modules/metro/src/Server.js:350  processRequest = (req, res, next) => {                 
^SyntaxError: Unexpected token =    
at Module._compile (internal/modules/cjs/loader.js:723:23)    
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)    
at Module.load (internal/modules/cjs/loader.js:653:32)    
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)    
at Function.Module._load (internal/modules/cjs/loader.js:585:3)    
at Module.require (internal/modules/cjs/loader.js:692:17)    
at require (internal/modules/cjs/helpers.js:25:18)    
at Object.<anonymous> (/home/cschlesiger/Projects/newRN/node_modules/metro/src/shared/output/bundle.js:12:16)    
at Module._compile (internal/modules/cjs/loader.js:778:30)    
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

I created a second fresh blank React-Native application using RN 0.64.1 and brought that one up in Android Studio the same way and it synchronized just fine - no errors.

I suppose there could be something wrong with my environment, however the fact that the only thing changing between these two fresh projects is the React-Native version makes me suspicious. If my environment were missing a variable or something like that it would fail for both.

Unless there is some new requirement for Android Studio for 0.66.3 that I missed in the documentation?

NOTE: Looking at the output of react-native info, I notice that it is not finding Android Studio under IDEs. I find that odd, since I know it is installed on this system via Ubuntu Software Center (Arctic Fox 2020.3.1 Patch 2: AI-203.7717.56.2031.7678000).

Version

0.66.3

Output of react-native info

System: OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa) CPU: (4) x64 Intel® Xeon® E-2176M CPU @ 2.70GHz Memory: 387.31 MB / 15.61 GB Shell: 5.8 - /usr/bin/zsh Binaries: Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node Yarn: 1.22.15 - ~/.nvm/versions/node/v16.13.0/bin/yarn npm: 8.1.3 - ~/.nvm/versions/node/v16.13.0/bin/npm Watchman: Not Found SDKs: Android SDK: API Levels: 25, 28, 29, 30 Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.2, 30.0.3 System Images: android-26 | Google APIs Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-30 | Google APIs Intel x86 Atom Android NDK: Not Found IDEs: Android Studio: Not Found Languages: Java: 14.0.2 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.2 => 17.0.2 react-native: 0.66.3 => 0.66.3 npmGlobalPackages: react-native: Not Found

Steps to reproduce

  1. Create a new blank React-Native project: npx react-native init newRN
  2. Open up Android Studio, open the newRN/android folder.
  3. Observe the exception as it tries to sync the gradle files.
  4. Create a second blank React-Native project using 0.64.1 npx react-native init newRN64 --version 0.64.1
  5. Open up Android Studio, open the newRN64/android folder.
  6. Observe that this second project synchronized with gradle successfully - no errors.

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

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

5reactions
MichaelBordecommented, Dec 2, 2021

Android studio might not be using your current Node version.

Try to use “Run anything” feature from Android Studio then type node --version or which node.

If you see something different than your Node: 16.13.0 it might be related to a nvm issue and/or a conflict with a Node system installation.

I don’t know if there a better way than this command:

sudo ln -s $(which node) /usr/local/bin/node
2reactions
cschlesigercommented, Dec 2, 2021

Android studio might not be using your current Node version.

Try to use “Run anything” feature from Android Studio then type node --version or which node.

If you see something different than your Node: 16.13.0 it might be related to a nvm issue and/or a conflict with a Node system installation.

I don’t know if there a better way than this command:

sudo ln -s $(which node) /usr/local/bin/node

That was it! Android Studio wasn’t using the node version set by nvm. It was instead using a system install of node that was an older version 10.19.0.

I set up the symbolic link you suggested, and I did some other searching to see if this was the preferred way (it is), and that cleared up the problem. Android Studio was successfully gradle syncing again, and can build my project.

Thank you very much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix Gradle Sync errors setting up new React-Native ...
When opening a project in Android Studio, open at the ProjectName/android/ folder not the ProjectName/android/app/ folder.
Read more >
Different Ways to fix "Error running android: Gradle project ...
Method 2: Sync your project with Gradle files ... This issue will not be solved if the folder containing Gradle files is deleted...
Read more >
Known issues with Android Studio and Android Gradle Plugin
To fix the issue for all future projects, click File > Close Project. You should see the welcome screen. Then click Configure >...
Read more >
Fixed: React Native new Android project Build Issue - YouTube
Fixed: React Native new Android project Build IssueIn this video, I will take you through a Step by step guide to fix Android...
Read more >
How to fix gradle sync failed in Android Studio - Quora
Click on Sync Project with gradle files. · Fix your gradle version as per your sdk. · For Rendering error - Go to...
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