7

Don’t use the wrong JDK for React Native if you’re using an M1 Mac

 2 years ago
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.
neoserver,ios ssh client

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

Speed testing my new 2021 M1 Max MacBook Pro (left) and 2019 Intel i9 MacBook Pro (right)

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:

1*i6pB7UppZko09rndG548bQ.png?q=20
dont-use-the-wrong-jdk-for-react-native-if-you-re-using-an-m1-mac-252533dd47a2

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:

Any questions, just ask on Twitter and other developers are quite helpful. You can tag me but I might not have the answer.

Enjoy!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK