Flutter开发环境搭建

配置国内镜像

打开mac的终端,按顺序执行以下命令:

1
2
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

或者直接添加为环境变量:

1
2
3
4
5
vi $HOME/.bash_profile

//之后往文件写入以下内容
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

注意,如果你使用了梯子并设置好了代理的话随意,这步可有可无。

Flutter SDK安装

可以到这下载Flutter SDK安装包(如果你所在的网络环境下得动的话):https://flutter.io/sdk-archive/#macos

如果下不动可以直接到github项目去release记录里下载:https://github.com/flutter/flutter/releases

下载完成后可以使用unzip命令解压文件,以下载到flutter目录,文件名flutter-0.10.0.zip为例:

1
2
cd ~/flutter
unzip flutter-0.10.0..zip

或者直接图形化界面解压。

又或者直接clone稳定的master分支:

1
git clone -b master https://github.com/flutter/flutter.git

之后配置环境变量:

1
2
3
4
vi $HOME/.bash_profile

//之后往文件写入以下内容
export PATH=你存放flutterSDK的路径/flutter/bin:$PATH

执行flutter doctor

1
flutter doctor

这个命令会检查依赖项是否已经安装完整。

比如我电脑诊断结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v0.10.1-pre.77, on Mac OS X 10.14 18A391, locale en-CN)
[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[!] iOS toolchain - develop for iOS devices (Xcode 10.0)
✗ libimobiledevice and ideviceinstaller are not installed. To install, run:
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install:
brew install ios-deploy
[✓] Android Studio (version 3.2)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[!] IntelliJ IDEA Ultimate Edition (version 2018.1.4)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] Connected device (1 available)

那么只需要按照打叉的项目一个个解决就可以了。

因为我同时安装了Android Studio及IntelliJ IDEA,所以后面的IntelliJ IDEA那一项warning是可以忽视的,使用Android Studio就可以了。

安装iOS开发依赖项

如果不打算打包ios版本,可以跳过这一步。

上面flutter doctor已经列出我们需要完成的东西,依次安装以下依赖:

1
2
3
brew install --HEAD libimobiledevice
brew install ideviceinstaller
brew install ios-deploy

安装libimobiledevice过程中可能会报以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
configure: error: Package requirements (libusbmuxd >= 1.1.0) were not met:

Requested 'libusbmuxd >= 1.1.0' but version of libusbmuxd is 1.0.10

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables libusbmuxd_CFLAGS
and libusbmuxd_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

READ THIS: https://docs.brew.sh/Troubleshooting

这个时候可以通过依次执行以下命令来解决:

1
2
3
4
5
6
7
brew update
brew uninstall --ignore-dependencies libimobiledevice
brew uninstall --ignore-dependencies usbmuxd
brew install --HEAD usbmuxd
brew unlink usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice

另外在你安装完所有依赖执行flutter doctor检查的时候可能报以下错误:

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
⣽Unhandled exception:
Exception: idevice_id returned an error:

#0 IMobileDevice.getInfoForDevice (package:flutter_tools/src/ios/mac.dart:122:9)
<asynchronous suspension>
#1 IOSDevice.getAttachedDevices (package:flutter_tools/src/ios/devices.dart:152:53)
<asynchronous suspension>
#2 IOSDevices.pollingGetDevices (package:flutter_tools/src/ios/devices.dart:112:57)
#3 PollingDeviceDiscovery.devices (package:flutter_tools/src/device.dart:163:52)
<asynchronous suspension>
#4 DeviceManager.getAllConnectedDevices (package:flutter_tools/src/device.dart:91:46)
<asynchronous suspension>
#5 DeviceValidator.validate (package:flutter_tools/src/doctor.dart:677:54)
<asynchronous suspension>
#6 Doctor.startValidatorTasks (package:flutter_tools/src/doctor.dart:107:52)
#7 Doctor.diagnose (package:flutter_tools/src/doctor.dart:174:41)
#8 _AsyncAwaitCompleter.start (dart:async/runtime/libasync_patch.dart:49:6)
#9 Doctor.diagnose (package:flutter_tools/src/doctor.dart:164:24)
#10 DoctorCommand.runCommand (package:flutter_tools/src/commands/doctor.dart:29:39)
#11 _AsyncAwaitCompleter.start (dart:async/runtime/libasync_patch.dart:49:6)
#12 DoctorCommand.runCommand (package:flutter_tools/src/commands/doctor.dart:28:42)
#13 FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:383:18)
#14 _asyncThenWrapperHelper.<anonymous closure> (dart:async/runtime/libasync_patch.dart:77:64)
#15 _rootRunUnary (dart:async/zone.dart:1132:38)
#16 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#17 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#18 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#19 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#20 Future._complete (dart:async/future_impl.dart:476:7)
#21 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#22 _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async/runtime/libasync_patch.dart:33:20)
#23 _rootRun (dart:async/zone.dart:1124:13)
#24 _CustomZone.run (dart:async/zone.dart:1021:19)
#25 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:947:23)
#26 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#27 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#28 _runPendingImmediateCallback (dart:isolate/runtime/libisolate_patch.dart:115:13)
#29 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:172:5)

此时可以依次执行以下命令解决:

1
2
3
4
5
6
7
brew uninstall ideviceinstaller
brew uninstall libimobiledevice
brew install --HEAD libimobiledevice
brew link --overwrite libimobiledevice
brew install --HEAD ideviceinstaller
brew link --overwrite ideviceinstaller
sudo rm -rf /var/db/lockdown/*

之后连接ios设备选择“信任”后,执行以下命令:

1
sudo chmod -R 777 /var/db/lockdown/

安装Android开发依赖项

Android版本可以使用Android Studio或IntelliJ IDEA,这里以Android Studio为例。

打开“Android Studio”-“Preferences”-“Plugins”,搜索“Flutter”关键字,安装,途中会询问是否安装“Dark”,选择“是”。

注意,如果下载不动,可能你需要设置一下代理。

抑或从网上下载安装包在Android studio里面以文件形式导入安装。

本文只是作为本人安装过程记录。

准备就绪

最后执行一次flutter doctor,显示如下:

1
2
3
4
5
6
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v0.10.1-pre.77, on Mac OS X 10.14 18A391, locale en-CN)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.0)
[✓] Android Studio (version 3.2)
[✓] Connected device (1 available)

证明环境检查通过,已经准备就绪。

文章作者: Kevin Wu
文章链接: https://kevinwu.cn/p/5ed7ef7d/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 KevinWu.CN
支付宝打赏