My ReactNative Journey - 3 - Configuring VS Code, Using the Expo CLI and setting up Expo GO on the emulator
Configure VSCode for React Native Development
VSCode provides settings for Themes, Editors etc for comfortable viewing of the code editing environment. It provides built in support for Code Completion, Navigation and Refactoring. It helps with Syntax highlighting, Bracket Matching, Auto Indentation, Box Selection, adding Code Snippets and more.
It also provides optional 3rd party code libraries, known as extensions. These are very helpful in making the development environment much easier.
Some interesting plug-ins are:
- Material Icon Theme - to change the file icons that we see in the folder/file explorer view.
- VS Code ES7 React/Redux/GraphQL/React Native Snippets - This extension helps complete your lines of code and includes shortcuts for repetitive componens.
- React Native Tools - for debugging and running projects easily
- Color Highlight - Highlight web colors in your editor.
- Bracket Pair Coloriser - This used to be an extension, but is now part of VSCode itself.
- npm Intellisense - to autocomplete npm statements
- GitLens - Cleans cluttered code. Use Alt-Shift-F to structure your code.
Explore much more in the native VSCode toolbars. It provides lots of features to make development easier.
Running the App
Installing Expo GO on the emulator
This is a step missing from the documentation. We need to install Expo Go on the device (emulator or physical device, whichever is being used). Type this command from the project folder on the terminal.
expo client:install:android
This will install expo Go on the device.
*** Note that the command 'expo start' will install 'Expo Go' on the device/emulator by itself.
Running the app
From the app folder, ipen the terminal, and run any of the three commands below.
› Metro waiting on exp://192.168.31.27:19000
› Press w │ open web
› Press r │ reload app
› Press m │ toggle menu
› Press d │ show developer tools
› shift+d │ toggle auto opening developer tools on startup (disabled)
› Press ? │ show all commands
> Task :app:installDebug
Installing APK 'app-debug.apk' on 'Redmi Y2 - 9' for app:debug
Installing APK 'app-debug.apk' on 'Pixel_XL_API_28(AVD) - 9' for app:debug
> Task :app:installDebug FAILED
37 actionable tasks: 2 executed, 35 up-to-date
Unable to install D:\ReactNative\MainApp\android\app\build\outputs\apk\debug\app-debug.apk
Soltion: I think this may be because I already have an emulator that is running another application. On the emulator, went back, and switched the emulator off and on. I guess, one can also uninstall the previous application to solve the problem.
Now I get BUILD SUCCESSFUL. I can now see the app running on the emulator.
2. Soemtimes, the emulator won't open up, or act weird. There is a process called adb.exe that runs in the background.
Solution: Terminating this process in the task manager allows a new version of the emulator to open up.
3. Sometimes, despite terminating the adb.exe process, there will be issues opening the emulator. e.g.
throw ex;
^
RuntimeError: abort(Error: protocol fault (couldn't read status): connection reset). Build with -s ASSERTIONS=1 for more info.
at process.abort (D:\ReactNative\wordle\node_modules\metro-hermes-compiler\src\emhermesc.js:402:15)
netsh interface portproxy reset
4. Error: adb server version (36) doesn't match this client (41); killing...
1. Terminate adb.exe,
2. close the emulator,
3. Open Android Studio,
4. select the emulator, 5. save, 6. Run the emulator. 7. From the folder, run the application using npm start instead of expo start.
From your application folder, open the terminal. The command is 'npm run eject'
RN will confirm committin the changes to git working tree. Type yes to proceed.
Next, it will ask you what your Android package name should be. Note that this should be atleast two words separated by a '.'. In my case, I gave the name com.first_app. This will create the native project, and also update package.json and adds index.js entry point for both iOS and Android.
Make sure the emulator is running. At the terminal prompt, type 'npx react-native run-android. This shows up the application on the emulator. The expo project has been successfully converted to an RN CLI project.
I see lots of expo references in the files, including .expo and .expo-shared folders being retained. I decide to come to that later.
Running the program post eject.
I restart the emulator. Then, from my app folder, I type into a terminal - react-native run-android (my package.json file says that is how to start android).
Bang! I get an error.. 'react-native' is not recognized as an internal or external command, operable program or batch file.
The path mentions C:\Users\{user}\Appdata\roaming\npm, so path is not the issue.
I try installing react-native globally with 'npm -g install react-native-cli' and check.
Bingo. It worked!!
Comments
Post a Comment