Skip to content

Commit 9f1fc44

Browse files
committed
doc: 增加react-native 常见问题文档
1 parent ea15781 commit 9f1fc44

File tree

10 files changed

+299
-1
lines changed

10 files changed

+299
-1
lines changed
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
<!--idoc:ignore:start-->
2+
React Native
3+
===
4+
<!--idoc:ignore:end-->
5+
6+
一些 React Native 开发上遇到的问题简单记录。
7+
8+
<!--idoc:ignore:start-->
9+
10+
## 目录
11+
12+
- [修改 App 在手机上展示的名称](#修改-app-在手机上展示的名称)
13+
- [修改 App 在手机上展示的图标](#修改-app-在手机上展示的图标)
14+
- [判断 Release/Debug 用于调试](#判断-releasedebug-用于调试)
15+
- [开发模式弹出开发者菜单刷新应用](#开发模式弹出开发者菜单刷新应用)
16+
- [设置允许 HTTP 请求访问](#设置允许-http-请求访问)
17+
- [真机配置 IP 调试](#真机配置-ip-调试)
18+
- [Xcode 不用数据线真机调试](#xcode-不用数据线真机调试)
19+
- [打包修改 APP 版本号](#打包修改-app-版本号)
20+
- [常见错误](#常见错误)
21+
- [应用反应缓慢出现卡顿问题](#应用反应缓慢出现卡顿问题)
22+
- [Android: Gradle project sync failed](#android-gradle-project-sync-failed)
23+
- [Android: The device needs more free storage to install the application](#android-the-device-needs-more-free-storage-to-install-the-application)
24+
- [Android: Cannot convert string value 'UNIFIED_TEST_PLATFORM'](#android-cannot-convert-string-value-unified_test_platform)
25+
- [iOS: library not found for -lDoubleConversion](#ios-library-not-found-for--ldoubleconversion)
26+
- [iOS: symbol(s) not found for architecture i386](#ios-symbols-not-found-for-architecture-i386)
27+
- [iOS: Command PhaseScriptExecution failed with a nonzero exit code](#ios-command-phasescriptexecution-failed-with-a-nonzero-exit-code)
28+
29+
<!--idoc:ignore:end-->
30+
31+
## 修改 App 在手机上展示的名称
32+
33+
#### Android
34+
35+
修改 `android/app/src/main/res/values/strings.xml` 配置
36+
37+
```xml
38+
<resources>
39+
<string name="app_name">这里填写名称</string>
40+
</resources>
41+
```
42+
43+
#### iOS
44+
45+
修改 `ios/<应用名称>/Info.plist` 配置
46+
47+
```xml
48+
<key>CFBundleDisplayName</key>
49+
<string>这里填写名称</string>
50+
```
51+
52+
## 修改 App 在手机上展示的图标
53+
54+
#### Android
55+
56+
修改替换 `android/app/src/main/res/mipmap-(*)` 下面的图标
57+
58+
图标分为 方形图标(`ic_launcher.png`) 和 圆形图标(`ic_launcher_round.png`)
59+
60+
#### iOS
61+
62+
修改 `ios/<应用名称>/Images.xcassets/AppIcon.appiconset/Contents.json` 配置,及修改配置目录 `ios/<应用名称>/Images.xcassets/AppIcon.appiconset` 下的图标文件。
63+
64+
通过 xcode 下图拖拽更换图标更方便。
65+
66+
![](./img/img01.png)<!--rehype:style=max-width: 650px;width: 100%;-->
67+
68+
## 判断 Release/Debug 用于调试
69+
70+
#### Android
71+
72+
修改 `android/app/src/main/res/values/strings.xml` 配置
73+
74+
```java
75+
// 在Android Studio项目中
76+
if(BuildConfig.DEBUG){
77+
// debug模式
78+
}else{
79+
// release模式
80+
}
81+
```
82+
83+
#### iOS
84+
85+
```objective-c
86+
#ifdef DEBUG
87+
// debug模式
88+
#else
89+
//release 模式
90+
#endif
91+
```
92+
93+
#### React Native
94+
95+
```js
96+
if (__DEV__) {
97+
// debug 模式
98+
} else {
99+
// release 模式
100+
}
101+
```
102+
103+
## 开发模式弹出开发者菜单刷新应用
104+
105+
命令行支持*打开开发者菜单*,和其它的一些操作
106+
107+
- r - 重新加载应用
108+
- d - 打开开发者菜单
109+
- i - 在 iOS 上运行
110+
- a - 在 Android 上运行
111+
112+
113+
#### Android
114+
115+
按两次 <kbd>R</kbd> 键或从开发者菜单(<kbd>⌘</kbd><kbd>M</kbd>)中选择重新加载(Reload)以预览您的更改。
116+
117+
> 如果没有起作用可以在命令行使用 `adb shell input keyevent 82` 命令唤起**开发者菜单**
118+
119+
#### iOS
120+
121+
使用 <kbd>⌘</kbd><kbd>R</kbd> 让您的 IOS 模拟器重新加载本地项目,使用 <kbd>⌘</kbd><kbd>T</kbd> 弹出开发者菜单。
122+
123+
## 设置允许 HTTP 请求访问
124+
125+
#### Android
126+
127+
创建配置文件 `android/app/src/main/res/xml/network_security_config.xml` 内容如下:
128+
129+
```xml
130+
<?xml version="1.0" encoding="utf-8"?>
131+
<network-security-config>
132+
<base-config cleartextTrafficPermitted="true" />
133+
</network-security-config>
134+
```
135+
136+
修改配置 `android/app/src/main/AndroidManifest.xml`
137+
138+
```diff
139+
<application
140+
android:name=".MainApplication"
141+
android:label="@string/app_name"
142+
android:icon="@mipmap/ic_launcher"
143+
android:roundIcon="@mipmap/ic_launcher_round"
144+
android:allowBackup="false"
145+
+ android:networkSecurityConfig="@xml/network_security_config"
146+
android:theme="@style/AppTheme">
147+
</application>
148+
```
149+
150+
#### iOS
151+
152+
修改 `ios/<应用名称>/Info.plist` 配置
153+
154+
```xml
155+
<key>NSAppTransportSecurity</key>
156+
<dict>
157+
<key>NSAllowsArbitraryLoads</key>
158+
<true/>
159+
</dict>
160+
```
161+
162+
## 真机配置 IP 调试
163+
164+
#### 配置说明
165+
166+
1. ⚠️ 首先保证真机和 pc 在同一个局域网络下。
167+
2. 摇晃你的实体真机,调出配置弹窗。
168+
3. 团队开发可以不安装开发环境。
169+
170+
**`摇晃手机`** => `Configure Bundler` => 设置 `ip:端口`
171+
172+
默认端口:`8081` 可以通过参数更改默认端口 `react-native start --port 9999`
173+
174+
#### Android 设置
175+
176+
177+
#### iOS 设置
178+
179+
设置 `Build Configuration``Debug` 模式连接真机打包 APP。
180+
181+
> `Xcode` => `Product` => `Scheme` => `Edit Scheme...` => `Run` => `Info` => `Build Configuration` => `Debug`
182+
183+
## Xcode 不用数据线真机调试
184+
185+
通过菜单 `Xcode` => `Product` => `Destination` => `Add Additional Simulators...` 打开设置界面,勾选 `Connect via network`
186+
187+
![](./img/devices.png)<!--rehype:style=max-width: 650px;width: 100%;-->
188+
189+
如果是第一次操作, 可能会需要先进行配对操作;
190+
191+
1. 可在以上面弹出的界面中,点击左侧的设备,然后右健选`unpair device`
192+
2. 然后再去勾选 `connect via network`
193+
3. 这时手机上会提示信任界面,点击确认即可。
194+
195+
## 打包修改 APP 版本号
196+
197+
#### Android
198+
199+
修改 `android/app/build.gradle` 配置
200+
201+
```java
202+
android {
203+
.....
204+
defaultConfig {
205+
....
206+
versionName "2.1.1"
207+
}
208+
}
209+
```
210+
211+
#### iOS
212+
213+
修改 `ios/<应用名称>/Info.plist` 配置
214+
215+
```xml
216+
<key>CFBundleShortVersionString</key>
217+
<string>1.2.0</string>
218+
```
219+
220+
## 常见错误
221+
222+
### 应用反应缓慢,出现卡顿问题
223+
224+
#### 可能存在的问题
225+
226+
- 查看是否 console 日志打印过度造成。
227+
- React Native Debugger 页面放到最前面,浏览器窗口不要放到选项卡里面。
228+
229+
### Android: Gradle project sync failed.
230+
231+
#### 问题解决方法
232+
233+
在 Android Gradle 同步失败,导致项目无法启动,只需重新同步 Gradle 即可(可能需要翻墙),方法如下图。
234+
235+
![](./img/img02.png)<!--rehype:style=max-width: 650px;width: 100%;-->
236+
237+
### Android: The device needs more free storage to install the application
238+
239+
#### 问题解决方法
240+
241+
![](./img/img04.png)<!--rehype:style=max-width: 650px;width: 100%;-->
242+
243+
### Android: Cannot convert string value 'UNIFIED_TEST_PLATFORM'
244+
245+
```bash
246+
convert string value 'UNIFIED_TEST_PLATFORM' to an enum value of type 'com.android.builder.model.AndroidGradlePluginProjectFlags$BooleanFlag' (valid case insensitive values: APPLICATION_R_CLASS_CONSTANT_IDS, TEST_R_CLASS_CONSTANT_IDS, TRANSITIVE_R_CLASS, JETPACK_COMPOSE, ML_MODEL_BINDING)
247+
```
248+
249+
#### 问题解决方法
250+
251+
你需要下载最新版 [`android-studio-2021.2.1.16-mac_arm.dmg`](https://developer.android.google.cn/studio/archive) 。
252+
253+
### iOS: library not found for -lDoubleConversion.
254+
255+
#### 问题解决方法
256+
257+
Xcode 打开工程文件错误,使用 `*.xcodeproj` 打开工程会报这个错误。
258+
259+
> 请打开 `*.xcworkspace` 的工程文件,错误将得到解决。
260+
261+
### iOS: symbol(s) not found for architecture i386.
262+
263+
#### 问题解决方法
264+
265+
可能使用的某个包,不支持 i386 模拟器,使用 x86 模拟器或真机。
266+
267+
> 设置 `Build Configuration``Debug` 模式下可能会解决问题。
268+
> `Xcode` => `Product` => `Scheme` => `Edit Scheme...` => `Run` => `Info` => `Build Configuration`
269+
270+
![](./img/img03.png)<!--rehype:style=max-width: 650px;width: 100%;-->
271+
272+
### iOS: Command PhaseScriptExecution failed with a nonzero exit code
273+
274+
> React-Core-AccessibilityResources Command CodeSign failed with a nonzero exit code
275+
276+
#### 问题解决方法
277+
278+
打开 `Kaychain Access(钥匙串访问)` 应用删除 `Apple Worldwide Developer Relations Certification Authority` 证书
279+
280+
![](./img/img05.png)<!--rehype:style=max-width: 650px;width: 100%;-->
269 KB
Loading
1.31 MB
Loading
146 KB
Loading
151 KB
Loading
321 KB
Loading
197 KB
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Preview from 'src/component/Preview';
2+
import md from './README.md';
3+
4+
const transformImageUri = (url: string) => {
5+
const reqImage = (require as any).context!('./', true, /\.(png|gif|jpg|svg)$/);
6+
const urlAddr = reqImage(url);
7+
return urlAddr;
8+
};
9+
10+
const DEMO = () => (
11+
<Preview {...md} transformImageUri={transformImageUri} path="website/src/pages/docs/question/README.md" />
12+
);
13+
export default DEMO;

website/src/routes/menus.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ export const docsMenus: MenuData[] = [
9595
{ divider: true, name: '发布应用商店' },
9696
{ path: '/docs/app-store/ios', name: '发布 iOS 应用商店' },
9797
{ path: '/docs/app-store/android', name: '发布 Android 应用商店' },
98+
{ divider: true, name: 'react-native常见问题' },
99+
{ path: '/docs/questions', name: '问题总结' },
98100
{ divider: true, name: '其它' },
99-
// { path: '/docs/react-native-template', name: 'React Native Template' },
100101
{ path: '/docs/awesome-react-native', name: 'Awesome React Native' },
101102
{ href: 'https://github.com/facebook/react', target: '_blank', name: 'React 官方文档' },
102103
{ href: 'https://github.com/facebook/react-native', target: '_blank', name: 'React Native 官方文档' },

website/src/routes/router.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export const routeData = [
8181
path: '/docs/react-native-template/log-management',
8282
component: lazy(() => import('../pages/docs/react-native-template/log-management')),
8383
},
84+
{
85+
path: '/docs/questions',
86+
component: lazy(() => import('../pages/docs/questions')),
87+
},
8488
{
8589
path: '/components/about',
8690
component: lazy(() => import('../pages/components/about')),

0 commit comments

Comments
 (0)