React Native Setup
August 20, 2023•511 words
Development Environment & Build
- PowerShell in Administrator mode Right-click Windows Terminal, run as Administrator
- Chocolatey for PowerShell Package Manager for Windows PowerShell, similar to Apt on Ubuntu Chocolatey
- Git for Windows choco install git
- Micro the In-terminal Editor choco install micro choco install nano # Optional
- NVM (Node.js Package Manager) choco install nvm Need to close PowerShell terminal and start back after installing NVM.
- Node.js Latest nvm install latest
- Install Java choco install microsoft-openjdk11 See the link in React Native Setup section below for the info about JDK version, newer versions won't work. Close terminal and reopen, don't create JAVA_HOME env var which is similar to C:\Program Files\Microsoft\jdk-11.0.18.10-hotspot done by Choco; also don't add the bin dir inside JAVA_HOME to PATH.
- Install Android Studio Download and Install Android Studio for Android SDK and the emulator. Set the env var ANDROID_HOME to the installation folder of Android SDK as in the installation dialogue. Add dir platform-tools in ANDROID_HOME to PATH.
- React Native npm i -g react-native Do not install react-native-cli, it's already inside.
- Init Project Go to project dir and run: npm config set legacy-peer-deps true # Accepts dependency conflicts react-native init app —template react-native-template-js
- Open Android Studio Just to see the code converted from JS to Java, open folder (don't change anything, it breaks the transpiled code by React Native): PROJECTDIR/app/android
- Run Metro Bundler cd app react-native start # Optional: —reset-cache Newer command (but run 'npm run android' in separate terminal): npm start # Use this
- Run App cd app react-native run-android Newer command (in another terminal beside 'npm start'): npm run android # Use this
Build-time Errors
Don't mind the error PreactNativeDevServerPort=8081, it's failed build and no app to connect to.
- Build-time Error: Due to Template Mismatch with React Native
- Execution failed for task ':app:checkDebugAarMetadata'
- https://stackoverflow.com/a/68221656/5581893
- In app/android/build.gradle, change the compileSdkVersion = 29 to the min value specified in error.
- Execution failed for task ':app:checkDebugAarMetadata'
- Build-time Error: Due to Wrong Kotlin Version
- Execution failed for task ':react-native-webview:compileDebugKotlin'
- https://stackoverflow.com/a/76219148/5581893
- In app/android/build.gradle, add kotlinVersion = "1.6.0" in buildscript.ext
- The version "1.6.0" is after Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is … which is the line after Error: Command failed: gradlew.bat app:installDebug` but not related to the port error.
- Execution failed for task ':react-native-webview:compileDebugKotlin'
android {
packagingOptions {
pickFirst '/.so'
}
}
- Build-time Error: Due to Multiple Lib Binaries Found
- Execution failed for task ':app:mergeDebugNativeLibs'
- More than one file was found with OS independent path 'lib/armeabi-v7a/libfbjni.so'
- https://stackoverflow.com/a/74526307/5581893
- Location for packagingOptions: https://gitlab.com/telosid/samples/onyx-react-native-example
- Fix with adding the following block to android/app/build.gradle, not android/build.gradle:
- Execution failed for task ':app:mergeDebugNativeLibs'
- Build-time Error: Due to Old Commands
- error: ReferenceError: SHA-1 for file
- Use 'npm start' and 'npm run android' in 2 separate terminals
- error: ReferenceError: SHA-1 for file
- Build-time Error: Due to New Node.js
- Failed to construct transformer: Error: error:0308010C:digital envelope routines::unsupported
- Node.js 17+ needs option for legacy OpenSSL
- Run with $env:NODE_OPTIONS="--openssl-legacy-provider"; npm start