Don’t use the wrong JDK for React Native if you’re using an M1 Mac
source link: https://shift.infinite.red/dont-use-the-wrong-jdk-for-react-native-if-you-re-using-an-m1-mac-252533dd47a2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Don’t use the wrong JDK for React Native if you’re using an M1 Mac
…or you’ll be going 2x as slow and not know why
Note that this article will likely age poorly as React Native & adoptopenjdk are upgraded. It was written November 19, 2021.
While we are still working on updates to our free React Native installation guide to include Apple M1-specific instructions, I figured it would be a good idea to put out this quick-hit article on how to set up your M1 Mac with the right Apple Silicon / arm64 compatible JDK.
Pretty much every “Installing React Native on Apple M1” guide out there contains these instructions:
# (if you're lazy and not reading my article,
# don't actually run this please) 😂
brew install --cask adoptopenjdk8
The problem is that, while this will work on an M1 Mac, it’ll run under Rosetta 2, which is sort of a translation layer for Intel-compiled binaries.
In other words — it’ll be slow.
Use sdkman to install Azul’s Zulu JDK
Azul has put out a nice Java JDK that works natively on the Apple M1 chip and is much faster.
If you’ve already installed adoptopenjdk8
, uninstall it with brew uninstall adoptopenjdk8
.
Next, install sdkman, which is sort of like nvm or rvm for SDKs:
curl -s "https://get.sdkman.io" | bash
It’ll add itself to your .zshrc
if you have one — otherwise follow the instructions to make sure it’s in your path. You can run sdk
to see if it’s set up.
Then run this to install the right version of Zulu’s JDK:
sdk install java 8.0.312-zulu
Set Gradle to use the right JDK in Android Studio
Go into your Android Studio preferences and change this to be the right SDK:
Remove any other JDKs
If you flailed around for a while like I did, you might have some other JDKs installed. Run this to see what they are, and remove any softlinks or files that might get in the way (I got a weird The supplied javaHome seems to be invalid. I cannot find the java executable. Tried location:
error).
ls -al /Library/Java/JavaVirtualMachines
You can run this to remove all of the JavaVirtualMachines (run at your own risk):
rm -rf /Library/Java/JavaVirtualMachines/*
Restart your terminal
Restart your terminal and try compiling a React Native app in Android. It should be much, much faster (a bit over 40% faster on my machine!)
Other things to think about
I don’t have time right now to write up an exhaustive guide, but here are a few links to get you on the right path:
- If you used Migration Assistant (like I did), your homebrew installs are probably all Intel. Try this to upgrade to M1/arm64: https://blog.smittytone.net/2021/02/07/how-to-migrate-to-native-homebrew-on-an-m1-mac/
- Update any apps that you can to arm64 versions if they exist. About half my apps are upgraded while the others are still running on Rosetta 2.
- Here’s a decent guide for updating to M1; however, don’t use
adoptopenjdk8
(use my article’s advice instead): https://dev.to/davidkou/develop-android-apps-using-react-native-setup-on-mac-m1-and-troubleshooting-38bj - You can tell what is still running on Intel by opening Activity Monitor and then sorting by “Kind”
- Many Electron apps are still Intel, although the latest version of Electron does support the M1 Macs. Hopefully that changes soon. Slack and VS Code have M1 versions
Any questions, just ask on Twitter and other developers are quite helpful. You can tag me but I might not have the answer.
Enjoy!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK