This Article Part of React Native Tutorial Series want to start from scrach follow below link
Today, we setup react native environment in macos for run react native app.
- Node.js and npm
- Java JRE and JDK
- React Native CLI
- Android Studio
Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system and Linux. The name is intended to suggest the idea of building software on the Mac depending on the user’s taste.
Install Homebrew using bellow command in Terminal.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Install Node.js and npm
React need only webpack to bundle, compile, transpile the code. Then why do we need Node.js? 🤔 React Native comes with multiple built-in components and APIs to interact with both native IOS and Android platform. In some cases, we need to use native functionality that doesn’t provide by React Native. Most of the Native modules are distributed as npm packages. Therefore, we need to require npm and to initialize the npm we need to have Node.js as recommended. ⚠️ Remember, Node.js is not compulsory since you can download the packages and add to your library.
Install Node.js using bellow command in Terminal.
brew install node
React Native uses watchman to detect real time code changes and it automatically build and push the update to your device without manually refreshing.
brew install watchman
Install Java JRE and JDK
If you don’t have a complete installation of Java, the build scripts for react-native tend to complain and fail . Downloading Android Studio is not enough solution since it comes bundled with its own JRE.
brew tap AdoptOpenJDK/openjdk
brew cask install adoptopenjdk8
Install React Native
npm install -g react-native-cli
Set up Xcode
Download Xcode via Mac App Store
- Open Xcode, Xcode > Preferences
- Goto the Locations tab
- Selecting the most recent version from the Command Line Tools dropdown
- Install cocoapods to MAC OS
sudo gem install cocoapods
sudo gem update — system
sudo gem install -n /usr/local/bin cocoapods
CocoaPods manages library dependencies for your Xcode projects.
Set up Android Studio
Download Android Studio from below link https://developer.android.com/studio/index.html
- Open Android Studio
- Goto the Configure > SDK Manager
- Go to Appearance & Behavior → System Settings → Android SDK and check ✅ the box of latest version from Hide Obsolete Packages.
- Also check ✅ the boxes of below in Show package details,
- Android SDK Platform 28
- Intel x86 Atom_64 System Image or Google APIs Intel x86 Atom System Image
- Click “Apply” to download and install the Android SDK and related build tools.
- Set up environment variables to getting start with native code, Open terminal
open -e .bash_profile
and paste below lines there.
export ANDROID_HOME=$HOME/Library/Android/sdk export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/tools export PATH=$PATH:$ANDROID_HOME/tools/bin export PATH=$PATH:$ANDROID_HOME/platform-tools
Then you can create new Android Virtual Device (AVD) to run the application.
- Open Android Studio
- Goto the Configure > AVD Manager
- Create Virtual Device > pick any Phone from the list and click “Next”,
Click “Next” then “Finish” to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it , then proceed to the next step.
Create First App
The very first step is to initialize the app from the place where we need to create app
react-native init first-app
If you want to start a new project with a specific React Native version,
react-native init first-app --version X.XX.X
If you want use typescript with React Native,
react-native init first-app --template typescript
Running your React Native Application in IOS
Running your React Native Application in AVD
The first time when you run this code you might get a build failed errors as,
🛑 Asking to add local.properties file
Create local.properties file inside android folder and add below code there.
sdk.dir = /Users/pabasara/Library/Android/sdk
🛑 Asking to add keystroke
Go to directory
paste below code in terminal
keytool -genkey -v -keystore debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000
Press enter until it gets ,
Is CN=y, OU=n, O=yes, L=no, ST=x, C=Y correct?
and type Y as response to terminate the command.
Here we go…. 👌 If everything working properly you can see app is running on both IOS emulator and AVD without errors. 😃 😃 😃