React Native项目初始化问题记录及排查解决
Madman 后端工程师

前言

React Native 是一种生态活跃且成熟的现代跨平台解决方案,目前已经有很多大公司(FB, Tesla 等)正在使用React Native构建ios和安卓原生应用。那我们就来看看按照官方文档构建一个React Native项目有多简单。
我的环境配置:

  • MacOS Big sur v11.2
  • Node v15.12.0
  • Cocoapods v1.8.4
  • Xcode v12.5
  • vsCode v1.56.0

创建项目React Native

按照官方文档,我们使用npx 脚本来创建React Native项目

1
npx react-native init MyTestApp

执行脚本前面是顺利的,项目也创建成功了,但是项目内部的ios项目模块的pod install按照失败了。报错如下:

1
2
3
4
5
✖ Installing CocoaPods dependencies (this may take a few minutes)
✖ Installing CocoaPods dependencies (this may take a few minutes)
error Error: Failed to install CocoaPods dependencies for iOS project, which is required by this template.
Please try again manually: "cd ./MyTestApp/ios && pod install".
CocoaPods documentation: https://cocoapods.org/

pod install 按照失败的原因可能就比较多了,我先查看了一下pod –version得到我的pod版本好是1.8.4

然后我就进入项目的MyTestApp/ios目录,做过ios都知道这个目录下面就是一个oc代码的一个原生ios项目的结构,有.xcodeproj 还有一个Podfile文件。
因为我们pod install还没成功,所以还没有.xcworkspace文件。
我尝试直接在这个ios项目的目录下面直接执行 pod install,得到报错如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
➜  ios pod install         
Analyzing dependencies

――― MARKDOWN TEMPLATE ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

### Command


/usr/local/Cellar/cocoapods/1.8.4/libexec/bin/pod install


### Report

* What did you do?

* What did you expect to happen?

* What happened instead?


### Stack

CocoaPods : 1.8.4
Ruby : ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]
RubyGems : 3.0.3
Host : macOS 11.2 (20D64)
Xcode : 12.5 (12E262)
Git : git version 2.22.0
Ruby lib dir : /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib
Repositories : mafeng-bryant - git - https://github.com/mafeng-Bryant/Specs.git @ d5a4174277606abff4018d727701fdb8a3e3700a
master - git - https://github.com/CocoaPods/Specs.git @ dc48fb42d4da9ff168194324f817416eea583a47
pinterest - git - git@github.com:pinterest/ios-pdk.git @ dc22537d7634a2fef6dbefc6917723d46ad2adfe
trunk - CDN - https://cdn.cocoapods.org/

### Plugins

cocoapods-deintegrate : 1.0.4
cocoapods-plugins : 1.0.0
cocoapods-search : 1.0.0
cocoapods-stats : 1.1.0
cocoapods-trunk : 1.4.1
cocoapods-try : 1.1.0

### Podfile


require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '10.0'

target 'MyTestApp' do
config = use_native_modules!

use_react_native!(
:path => config[:reactNativePath],
# to enable hermes on iOS, change `false` to `true` and then install pods
:hermes_enabled => false
)

target 'MyTestAppTests' do
inherit! :complete
# Pods for testing
end

# Enables Flipper.
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
# you should disable the next line.
use_flipper!()

post_install do |installer|
react_native_post_install(installer)
end
end

### Error

RuntimeError - [Xcodeproj] Unknown object version.
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/xcodeproj-1.13.0/lib/xcodeproj/project.rb:227:in `initialize_from_file'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/xcodeproj-1.13.0/lib/xcodeproj/project.rb:112:in `open'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer/analyzer.rb:1098:in `block (2 levels) in inspect_targets_to_integrate'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer/analyzer.rb:1097:in `each'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer/analyzer.rb:1097:in `block in inspect_targets_to_integrate'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/user_interface.rb:64:in `section'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer/analyzer.rb:1092:in `inspect_targets_to_integrate'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer/analyzer.rb:106:in `analyze'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer.rb:410:in `analyze'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer.rb:234:in `block in resolve_dependencies'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/user_interface.rb:64:in `section'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer.rb:233:in `resolve_dependencies'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/installer.rb:156:in `install!'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/command/install.rb:52:in `run'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/claide-1.0.3/lib/claide/command.rb:334:in `run'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/lib/cocoapods/command.rb:52:in `run'
/usr/local/Cellar/cocoapods/1.8.4/libexec/gems/cocoapods-1.8.4/bin/pod:55:in `<top (required)>'
/usr/local/Cellar/cocoapods/1.8.4/libexec/bin/pod:23:in `load'
/usr/local/Cellar/cocoapods/1.8.4/libexec/bin/pod:23:in `<main>'

――― TEMPLATE END ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

[!] Oh no, an error occurred.

Search for existing GitHub issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=%5BXcodeproj%5D+Unknown+object+version.&type=Issues

If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new

Be sure to first read the contributing guide for details on how to properly submit a ticket:
https://github.com/CocoaPods/CocoaPods/blob/master/CONTRIBUTING.md

Don't forget to anonymize any private data!

Looking for related issues on cocoapods/cocoapods...
- [Xcodeproj] Unknown object version. Help Please
https://github.com/CocoaPods/CocoaPods/issues/10604 [open] [2 comments]
a week ago

- RuntimeError - [Xcodeproj] Unknown object version.
https://github.com/CocoaPods/CocoaPods/issues/10259 [closed] [7 comments]
2 weeks ago

- RuntimeError - [Xcodeproj] Unknown object version
https://github.com/CocoaPods/CocoaPods/issues/10432 [closed] [6 comments]
11 Mar 2021

and 59 more at:
https://github.com/cocoapods/cocoapods/search?q=%5BXcodeproj%5D%20Unknown%20object%20version.&type=Issues&utf8=✓

让后根据错误提示找到GitHub的解决方案:https://github.com/CocoaPods/CocoaPods/issues/10604
我执行如下命令

1
2
3
4
5
6
7
8
9
10
➜  ios gem uninstall xcodeproj
Gem 'xcodeproj' is not installed
➜ ios gem install xcodeproj
Fetching xcodeproj-1.19.0.gem
Fetching nanaimo-0.3.0.gem
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
➜ ios gem install xcodeproj
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

在执行gem install xcodeproj报错了目录权限不够。按照我的常规懒习惯直接加sudo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
➜  ios sudo gem install xcodeproj                                     
Password:
Fetching atomos-0.1.3.gem
Fetching claide-1.0.3.gem
Fetching xcodeproj-1.19.0.gem
Fetching colored2-3.1.2.gem
Fetching nanaimo-0.3.0.gem
Successfully installed atomos-0.1.3
Successfully installed claide-1.0.3
Successfully installed colored2-3.1.2
Successfully installed nanaimo-0.3.0
xcodeproj's executable "xcodeproj" conflicts with /usr/local/bin/xcodeproj
Overwrite the executable? [yN] y
Successfully installed xcodeproj-1.19.0
Parsing documentation for atomos-0.1.3
Installing ri documentation for atomos-0.1.3
Parsing documentation for claide-1.0.3
Installing ri documentation for claide-1.0.3
Parsing documentation for colored2-3.1.2
Installing ri documentation for colored2-3.1.2
Parsing documentation for nanaimo-0.3.0
Installing ri documentation for nanaimo-0.3.0
Parsing documentation for xcodeproj-1.19.0
Installing ri documentation for xcodeproj-1.19.0
Done installing documentation for atomos, claide, colored2, nanaimo, xcodeproj after 1 seconds
5 gems installed

问题解决,现在终于可以愉快的pod install了吧?
不幸的是,还是报错如下,pod版本不够,OpenSSL-Universal这个依赖需要>= 1.9的版本号

1
2
3
4
5
6
➜  ios pod install
Analyzing dependencies
Fetching podspec for `DoubleConversion` from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`
Fetching podspec for `RCT-Folly` from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`
Fetching podspec for `glog` from `../node_modules/react-native/third-party-podspecs/glog.podspec`
[!] `OpenSSL-Universal` requires CocoaPods version `>= 1.9`, which is not satisfied by your current version, `1.8.4`.

那我就只能把pod 升级了。注意,pod 升级不是 pod update 这个是更新pod依赖库的版本好。我们现在需要更新Cocoapods这个软件本身的版本。
我之前是用brew安装的Cocoapods, 那为了避免冲突,我还是用brew去升级这个Cocoapods软件。

1
brew upgrade cocoapods

更新Cocoapods版本到v1.10.1了,软件更新成功,使用 pod --version查看版本直接找不到pod这个命令zsh: command not found: pod。这种情况一般是软件引用链接没关联到zsh导致,看下面执行升级cocoapods命令时的报错:

1
2
3
4
5
6
7
8
9
10
11
12
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/xcodeproj
Target /usr/local/bin/xcodeproj
already exists. You may want to remove it:
rm '/usr/local/bin/xcodeproj'

To force the link and overwrite all conflicting files:
brew link --overwrite cocoapods

To list all files that would be deleted:
brew link --overwrite --dry-run cocoapods

有文件冲突,和文件引用链接 没关联,按照报错提升执行,先删除冲突文件。

1
rm '/usr/local/bin/xcodeproj'

然后强制brew link关联软件引用路径。

1
brew link --overwrite cocoapods

pod 版本更新成功看下图:

这个时候回到React Native项目的ios目录执行 pod install pod 安装成功,如下图:

模拟器上运行初始化好的项目效果如图

  • 本文标题:React Native项目初始化问题记录及排查解决
  • 本文作者:Madman
  • 创建时间:2021-05-10 14:58:38
  • 本文链接:https://www.patpat.site/开发/前端/React-Native项目初始问题记录及排查解决.html
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论