Recently, I started to contact RN. The government started to install a lot of tools, and then a lot of problems appeared when I started the project. Here are some solutions for some problems I encountered
Mac, my development environment, has no big problem when I start IOS. I can start it directly. Here’s a hint. Because there may be multiple starts, I can specify the port when I start to prevent conflict react native run IOS -- port = XXX
but there are a lot of problems when I start Android, At the beginning of using the command react native run Android -- port = XXXX
, the command line reported an error
$ react-native run-android
Scanning folders for symlinks in /Users/ric/myprojs/albums/node_modules (6ms)
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '9.0.4'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/android-setup.html
We can’t judge the Java version. Here we look up some information. We just need to change the JDK to 1.8. We can install multiple versions of JDK, and then configure the environment variables to dynamically switch the version. Create. Bash in the root directory_ Profile file, add content and save
#set sdk catalog
export PATH=${PATH}:/Users/stevenzwzhai/Library/Android/sdk/platform-tools/:/Applications/Android\ Studio.app/sdk/platform-tools
#set idk 8
export JAVA_8_HOME=`/usr/libexec/java_home -v 1.8.0`
#set JDK 9
export JAVA_9_HOME=`/usr/libexec/java_home -v 9.0.4`
#default JDK 9
export JAVA_HOME=$JAVA_9_HOME
#alias Command to dynamically switch JDK versions
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk9="export JAVA_HOME=$JAVA_9_HOME"
source .bash_ Profile
configuration takes effect immediately. Then we can switch the JDK version, and the command line is jdk8. Continue to run the above command. If the error shows that the SDK cannot be found, you can create the file local.properties in the Android directory of the project and save the following content
sdk.dir = /Users/stevenzwzhai/Library/Android/sdk
Then we run the command again. This time, no error will be reported, but the Android emulator will be displayed in red
unable to load script from assets 'index.android.bundle'.Make sure your bundle ispackged correctly or you are running a packger server.
Here, There are some other people’s solutions:
1. Create new assets file under Android/APP/SRC/main;
2. Execute react-native bundle — platform Android — dev false — entry file index.android.js — bundle output Android/APP/SRC/main/assets/index.android.bundle — assets dest Android/APP/SRC/main/res in the root directory of the project;
3. Re-execute react native-run Android, but I don’t want to do it here It doesn’t work, so my problem should be the port. I found that when Android starts the program, the head shows 10.0. X.x: 8081, because the default is port 8081, but when we start it, we manually set it to another port. Here we need to modify the agent of the emulator, IP or IP of the mobile phone. Note that it’s not the IP displayed by WiFi, It’s the one in the green box at the head when the simulator starts our project. As long as the reload box is displayed, then change the port to the port when you start the project. So we can start the project without starting Android Studio or Xcode