First entry react-native notes - mac install Android emulator

As a front-end focusing on react, I recently started to learn react native. I have never developed native, and I find it difficult to read the official documents directly, so I found a teaching video that can lead me to get started. At the beginning, everything went well until I came across that I wanted to use the terminal to open the Android emulator:


1. Install node, xCode, androidStudio, jdk (there are pits here)


Second, use the mac terminal to start typing commands


1. Install the react command line tool: npm install -g react-native-cli ;


2. Initialize an RN project: react-native init 'rNapp' ; rNapp is the project name defined by itself


3. Run the project: react-native run-ios/react-native run-android,

Or run with xCode and androidStudio.

Here comes the problem: when running react-native run-android, you need an Android emulator. The video says just one sentence: use "emulator -avd a7" to start the Android emulator.

Naive me, I followed it, and the result was "emulator: command not found". At this time, I started to look confused, what does emulator do? -What is avd? what is a7? ? ?


3. Install the Android emulator on mac (the following are all command-line tools, some commands and operations I don't quite understand yet or there are other more convenient ways to implement them, and I will come back and add them later).


1. To install the Android SDK.

My own approach is a fool. When I opened the project with androidStudio for the first time, it prompted me to have something missing at the bottom. It was recommended to install. to open the project.


2. Find the SDK path .

The first way to open androidStudio→File→Other Settings→Default Project Structure is the SDK path.


3. Open  cd Library/Android/sdk/tools/ with the command ;

Then when you type android, an error will be reported, The "android" command is deprecated. This is a version problem.

So I found an article: http://www.jianshu.com/p/049871577392 , replace the tools folder under the sdk according to the article.

Then tap .android to pop up the Android SDK Manager interface, select the installation package you need to download, and click install to download.


4. After downloading, I happily tap emulator -avd .

It gave me a prompt emulator: command not found. It is another search for information, and I know that I have not configured the Android environment variables.

Or see the article: http://www.jianshu.com/p/84f3baf1682c .


5、然后我去试emulator -avd

为什么不加后面的a7呢,这时候我知道a7是模拟器的名字,而我自己的模拟器我还不知道名字。

然后emulator -avd -list-avds,这个命令,找到了我的模拟器Nexus_6_API_22。

再试emulator -avd Nexus_6_API_22 。完美用命令行打开了安卓模拟器!


四、react-native run-android

你以为到这就结束了吗,太天真了。

找到我的rnAPP目录下,敲击react-native run-android。满心欢喜的等待,结果等来三个问题。


1、FAILURE :Build failed with an exception. 

What went wrong :Could not determine java version from ‘9.0.1’。

这个是因为我下载的jdk版本过高了。

卸载:命令行运行sudo rm -rf /Library/Java/JavaVirtualMachines/jdk9.0.1.jdk

然后又下载一个8版本的。


2、FAILURE :Build failed with an exception. What went wrong:

problem occurred configuring root project 'rnApp '

Could not resolve al1files for configuration ':classpath'.

Could not find.com.android.tools.build:gradle:3.8.1.

Searched in the following locations:

   https: //jcenter.bintray.com/com/android/tools/build/gradle/3.8.1/gradle-3.8.1.pom

   https://jcenter.bintray.com/com/android/tools/build/gradle/3.0.1/grad1e-3.0.1.jar


这个问题是因为,我打开androidStudio之后它提示让我更新,我就傻傻的更新了。

导致项目里gradle-wrapper.properties文件的和build.gradle文件的版本相差太大了。

大神告诉我这两个版本要一一对应。(我现在还不懂是什么意思,待我研究之后再来补充。)

解决方式就是我去重新:react-native init了一个项目。


3、接下来,启动新项目启动起来了。但是模拟器上报错了,一片红.

React Native version mismatch.

JavaScript version: 0.50.4

Native version: 0.51.0

Make sure that you have rebuilt the native code.If the problem persists try clearing the Watchmanandon 0.50.packager caches with"watchman  watch-del- all&&1.51.0react-native start--reset-cache'.

这个就是js版本0.50.4和native版本0.51.0版本不匹配了。

解决方式还是重新react-native init xxxx --version 0.50.4,加了版本号。我暂时还没找到一劳永逸的解决方式。


到此,项目算是终于能启动了。期间有很多不必要的弯路,其实就是自己死磕非要用命令行启动项目造成的。也有很多地方虽然问题解决了不知道原理。接下来慢慢学。感激百度到的各位牛人的文章,和群里大神们的教导。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906160&siteId=291194637