0%

兵马未动,粮草先行 -- Flutter开发环境配置

对于Flutter开发环境的配置,官方网站 Install - Flutter 中已经介绍的非常详细了,只要按照步骤来操作即可。

我也是按照官方的教程来操作的,这里只是做一个记录。另外,我的操作并不是完全按照官方的流程来的,这里也仅仅是给各位做一个参考。


关于系统

我的电脑是 Mac,系统版本为 macOS Mojave 10.14.3

配置Flutter SDK

我安装的时候Flutter的版本还是 v1.0.0 ,今天看已经更新到 v1.2.1 了。不得不说Flutter的更新是真给力。

下载

从网站地址 MacOS install - Flutter 下载 Flutter SDK 安装包。

我将 flutter sdk 安装在 ~/sdk (或者写成 $HOME/sdk) 目录下:

1
2
$ cd ~/sdk
$ unzip ~/Downloads/flutter_macos_v1.0.0-stable.zip
配置

添加 flutter 相关工具到环境变量 path 中:

编辑 $HOME/.bash_profile 文件,在底部添加如下命令:

1
export PATH="$PATH:[PATH_TO_FLUTTER_GIT_DIRECTORY]/flutter/bin"

其中的 [PATH_TO_FLUTTER_GIT_DIRECTORY] 表示你解压 flutter sdk 的目录,我这里就是 ~/sdk。即:

1
export PATH="$PATH:~/sdk/flutter/bin"

另外,在国内由于 众所周知 的原因,还需要设置一下pub源,用以下载相关的依赖文件。其中 PUB_HOSTED_URLFLUTTER_STORAGE_BASE_URL 是google为国内开发者搭建的临时镜像。

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

所以,需要添加的就是上面的三条命令:

1
2
3
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=[PATH_TO_FLUTTER_GIT_DIRECTORY]/flutter/bin:$PATH

其中 PATH_TO_FLUTTER_GIT_DIRECTORY 为你 flutter sdk 的路径。如我这里就是改成(推荐用这种方式):

1
export PATH=$HOME/sdk/flutter/bin:$PATH

刷新bash:

1
$ source $HOME/.bash_profile

如果使用的是 zsh ,则执行:

1
$ source $HOME/.zshrc

查看:

1
echo $PATH

验证是否配置成功:

1
flutter -h

2019-10-20 更新:最近在安装最新的 flutter_1.9.1 版本时发现,配置好上面的环境变量后,第一次运行 flutter -h 不会立即显示出下面的结果,就是我们通常所说的 卡住了 。观察发现在第一次执行 flutter -h 命令的时候会去执行一些后台的操作(比如git),所以第一次执行的时候会需要等待一段时间。

这个时候应该能展示flutter的命令帮助:


检查flutter环境

在安装环境的过程中,Flutter官方提供了一个依赖检查的命令 flutter doctor ,可以查看到哪些依赖项或程序没有安装成功。

1
$ flutter doctor

另外,该命令的 -v 选项在开发过程中也非常有帮助,执行 flutter doctor -v 查看。

第一次运行时间会有点长(所以。。。如果看到没反应的话千万别慌,等等就好了;另外,如果你是安装最新的flutter版本,在执行 flutter -h卡住了,那么在执行 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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices
✗ No valid Android SDK platforms found in /usr/local/Caskroom/android-platform-tools/latest/platforms. Directory was
empty.
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ Xcode requires additional components to be installed in order to run.
Launch Xcode and install additional required components when prompted.
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[!] Android Studio (not installed)
[!] VS Code (version 1.31.1)
[!] Connected device
! No devices available

! Doctor found issues in 5 categories.

通过上面的提示信息,可以看到 Android toolchainiOS toolchain 是必须安装其一的。这里我是都要安装,毕竟两个平台都想要体验一下的。

开发工具呢除了之前 官方Android StudioXCode 之前,Flutter非常推荐通过轻量级的 VS Code 来进行开发。


配置VS Code

安装VS Code ,安装1.20.1或更高版本.

安装Flutter插件
  1. 启动 VS Code
  2. 调用 View > Command Palette…
  3. 输入 install, 然后选择 Extensions: Install Extension action
  4. 在搜索框输入 flutter , 在搜索结果列表中选择 Flutter, 然后点击 Install(会自动附带安装好 Dart 插件)
  5. 选择 OK 重新启动 VS Code
通过Flutter Doctor验证您的设置
  1. 调用 View > Command Palette…
  2. 输入 doctor, 然后选择 Flutter: Run Flutter Doctor 选项
  3. 查看 “OUTPUT” 窗口中的输出是否有问题

运行 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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices
✗ No valid Android SDK platforms found in /usr/local/Caskroom/android-platform-tools/latest/platforms. Directory was
empty.
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[!] Android Studio (not installed)
[✓] VS Code (version 1.31.1)
[✓] Connected device (1 available)

! Doctor found issues in 3 categories.

可以看到 [✓] VS Code (version 1.31.1) 一项已经打钩了。

创建Flutter项目

通过 VS Code 创建Flutter项目非常简单。直接调出VS Code的命令模式 Cmd(Control) + Shift + P 或者选择菜单 View > Command Palette… 输入 flutter new 选择 Flutter: New Project 选项,回车,输入项目目录名称,会让你选择项目保存目录,然后等待自动初始化项目。


Android环境配置

Android toolchain 中要求必须安装好 Android SDK ,而 Android SDK 的安装方法常见的有两种:

  1. 通过安装 Android Studio 会自动给安装好 Android SDK ,这种方法操作起来比较简单,基本上 Android Studio 都帮你配置好了;
  2. 直接下载 Android SDK 安装包,但需要自己去手动配置,而该配置过程稍显复杂;

虽然开发工具上面已经配置了 VS Code也就没必要再安装 Android Studio 了,不过为了方便快捷,这里我选择采用第一种方式来安装。

这里需要注意的是:最好自备 梯子 ,有些Android依赖包下载的过程中可能会由于网络原因需要多次重试。

安装 Android Studio 及配置

打开官网 ,会直接根据当前系统显示推荐下载版本:

直接点开安装即可。结果弹出了一个错误页面:

出现这个错误的原因是:在第一次安装AS,启动后,检测到电脑没有安装 Android SDK。

相应的解决方法是直接点击 Cancel 选项,在后续的界面中再安装SDK。

点击 Next 后,我这里选择的是 Custom (自定义)选项。

可以看到 Android SDK 是默认勾选的。另外两项我这里全部勾选上了。还可以在这里选择自定义安装目录。

下一页是Android模拟器的内存设置:

SDK Path 默认为 /Users/xyz/Library/Android/sdk

然后就是等待下载安装的过程:

由于某些资源包是访问的 google.com 网址,如果没有 梯子 ,是需要等待很久。。。很久。。。的。

等待安装完成后,再次查看:

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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
✗ Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[✓] Android Studio (version 3.3)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.31.1)
[!] Connected device
! No devices available

! Doctor found issues in 3 categories.
解决 Android licenses 问题

看上面的提示,这里不得不夸赞一下Flutter工具的🐂🍺了。有什么问题直接在下面显示并给出解决方法,不会让一些小白再需要去网上搜索了。

看上面的错误提示:

1
2
[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
✗ Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

直接执行命令:

1
2
3
4
5
6
➜ flutter doctor --android-licenses
Warning: File /Users/xyz/.android/repositories.cfg could not be loaded..
6 of 6 SDK package licenses not accepted. 100% Computing updates...
Review licenses that have not been accepted (y/N)?
...
...

按照提示,一直输入 y 即可。直到看到:

1
All SDK package licenses accepted

就说明配置成功了。再次查看:

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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[✓] Android Studio (version 3.3)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.31.1)
[!] Connected device
! No devices available

! Doctor found issues in 2 categories.

发现 [✓] Android toolchain 配置成功了。

安装Flutter插件

看上面的错误提示:

1
2
3
[✓] Android Studio (version 3.3)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.

看提示,需要在 Android Studio 中安装两个必要的插件:Flutter pluginDart plugin

打开 Android Studio,可以不用新建项目,直接选择菜单栏中的 Android StudioPreferences – 选择或者搜索 Plugins 菜单 – 然后搜索 Flutter 插件,可以看到会自动提示安装依赖的 Dart 插件:

之后,需要重启。

再次查看:

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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[✓] Android Studio (version 3.3)
[✓] VS Code (version 1.31.1)
[!] Connected device
! No devices available

! Doctor found issues in 2 categories.
创建Flutter项目

使用 Android Studio 创建 Flutter 项目也非常便捷。直接选择启动页中的 Start a new Flutter project 菜单,然后选择 Flutter ApplicationNext – 输入 Project name 及保存目录后,即可初始化一个 Flutter 项目。


连接Android真机运行

上面 Android 的环境已经配置好了,我就有些迫不及待的想要在手机上运行了。

跑起来

连接上安卓手机,开始运行 flutter run 命令:

1
2
3
4
5
6
7
➜ flutter run   
No connected devices.

Run 'flutter emulators' to list and start any available device emulators.

If you expected your device to be detected, please run "flutter doctor" to diagnose
potential issues, or visit https://flutter.io/setup/ for troubleshooting tips.

结果却提示 No connected devices 。根据提示,执行:

1
2
3
4
5
6
7
8
9
10
11
12
➜ flutter emulators
2 available emulators:

Nexus_5X_API_28_x86 • Nexus 5X • Google • Nexus 5X API 28 x86
apple_ios_simulator • iOS Simulator • Apple

To run an emulator, run 'flutter emulators --launch <emulator id>'.
To create a new emulator, run 'flutter emulators --create [--name xyz]'.

You can find more information on managing emulators at the links below:
https://developer.android.com/studio/run/managing-avds
https://developer.android.com/studio/command-line/avdmanager

按照提示,执行 flutter emulators --launch <emulator id> 命令:

1
2
3
4
5
6
➜ flutter run -d Nexus_5X
No devices found with name or id matching 'Nexus_5X'


➜ flutter run -d Nexus_5X_API_28_x86
No devices found with name or id matching 'Nexus_5X_API_28_x86'

结果仍然是 找不到设备

到这里的结果就是:

  • 安卓手机发现不了设备。
  • Android模拟器一直连接失败,即使连接上了,在安装app的时候也会提示 no devices 的信息。好不容易安装成功后,发现网上提到的 hot reload 的功能却用不了。Android Studio 上的热加载 ⚡️ 按钮也是灰色的。
  • ios模拟器安装启动正常,而且热更新的功能也能正常使用。

后来再次查看了官方的安装文档,恍然大悟之间,才发现我忘记操作了一步:我只安装了 Android Studio但并没有配置 ANDROID_HOME 环境变量,而是直接就开始安装App了。

见官方文档:Set up your Android device - Flutter

配置 ANDROID_HOME 环境变量

查看当前环境变量:

打开 AS ,找到 PreferencesAppearance & BehaviorSystem SettingsAndroid SDK – 在右侧找到 Android SDK Location – 复制

在命令行中输入 echo $ANDROID_HOME,发现当前值确实为空。

然后在 $HOME/.bash_profile 中加入下列代码:

1
2
3
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools

重启命令行窗口或者执行 source $HOME/.bash_profile or source $HOME/.zshrc 刷新配置。

配置Android模拟器

打开 AS, Android Studio > Tools > Android > AVD Manager and select Create Virtual Device . (更多的去参考一下文档去吧)> Set up the Android emulator - Flutter

后来发现,如果没有配置,是会启动一个默认的模拟器。

在上面的操作中,由于我太着急,直接就运行程序了,所以默认给我创建了一个 Nexus 5X 的模拟器。


ADB server didn’t ACK

将手机通过usb连接,通过 adb devices 命令来查看设备连接状态:

1
2
3
4
5
➜ adb devices
List of devices attached
adb server version (40) doesn't match this client (39); killing...
ADB server didn't ACK
...

结果却报了 ADB server didn't ACK 的错误。

相应的解决方法如下:

  1. 查看端口被占用情况:在终端输入 lsof -i tcp:5037 查看占用 5037 端口的pid号
1
2
3
4
➜ lsof -i tcp:5037
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
adb 38752 xyz 3u IPv4 0xec93d79cdb5a9d91 0t0 TCP localhost:49653->localhost:5037 (CLOSED)
adb 38752 xyz 11u IPv4 0xec93x80a5492d411 0t0 TCP localhost:5037 (LISTEN)

2.去任务管理器(应用程序-其他-活动监视器)找到对应pid号(这里是38752)的进程,并关闭

  1. 再次查看:
1
2
3
4
5
➜ adb devices
List of devices attached
adb server version (40) doesn't match this client (39); killing...
* daemon started successfully
FA6AB0311758 device

现在,设备已经正常连接了。


Android license status unknown
1
2
3
4
5
6
7
8
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
✗ Android license status unknown.
[✓] Android Studio (version 3.3)
。。。
。。。

解决方法是:再执行一次 flutter doctor --android-licenses 命令,在提示后输入 y 确认,然后再通过 flutter doctor 查看。


某些安卓手机需要解锁

对于某些安卓设备默认情况下都没有启用开发者调试的功能,这次我换用一台小米手机来进行真机调试。

当有多个设备连接时,可以通过 -d <deviceId> 连接指定设备,或者使用 -d all 连接所有设备来调试。其中 <deviceId> 支持模糊匹配设备名称:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
➜ flutter run
More than one device connected; please specify a device with the '-d <deviceId>' flag, or use '-d all' to act
on all devices.

Redmi Note 4X • a1529b418604 • android-arm64 • Android 7.0 (API 24)
Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)



➜ flutter run -d Redmi
Launching lib/main.dart on Redmi Note 4X in debug mode...
Initializing gradle... 1.0s
Resolving dependencies... 7.7s
Gradle task 'assembleDebug'...
Gradle task 'assembleDebug'... Done 10.4s
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk... 8.1s
Error: ADB exited with exit code 1
adb: failed to install /Users/xyz/Learn/flutter/hello_world/build/app/outputs/apk/app.apk: Failure
[INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]
Error launching application on Redmi Note 4X.

可以看到,我这里设备选择成功了,但当运行到 Install 阶段报错了。

这里,需要更改一下手机设置,以小米手机为例:

  1. 打开手机的开发者模式: 设置我的设备全部参数MIUI版本 – 连续点击7下即可
  2. 再次点击 设置更多设置开发者选项 – 将 开启开发者选项 启用 、 USB调试 启用 、 USB安装 启用
  3. 再次执行 flutter run 命令进行安装,此时手机上会弹出 USB安装提示 的命令,选择 允许 即可
1
2
3
4
5
6
7
8
9
10
11
12
13
➜ flutter run
Launching lib/main.dart on Redmi Note 4X in debug mode...
Initializing gradle... 0.8s
Resolving dependencies... 1.5s
Gradle task 'assembleDebug'...
Gradle task 'assembleDebug'... Done 1.5s
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk... 14.6s
Syncing files to device Redmi Note 4X... 1.6s

🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on Redmi Note 4X is available at: http://127.0.0.1:61766/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

最终运行

再次检查状态:

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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[✓] Android Studio (version 3.3)
[✓] VS Code (version 1.31.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

打开 VS Code, 在终端中运行 flutter run 命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
➜ flutter run
Launching lib/main.dart on Pixel in debug mode...
Initializing gradle... 1.1s
Resolving dependencies... 8.2s
Gradle task 'assembleDebug'...
Gradle task 'assembleDebug'... Done 12.4s
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk... 3.4s
Syncing files to device Pixel... 1.5s

🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on Pixel is available at: http://127.0.0.1:49724/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

可以看到,现在才是正常运行的状态。已经成功的连接上了我的 Pixel 手机,同时在手机端,已经打开了示例app的界面。输入 r 来实现热更新。


IOS环境配置

查看当前的状态:

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
➜ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[!] iOS toolchain - develop for iOS devices (Xcode 10.1)
✗ libimobiledevice and ideviceinstaller are not installed. To install with Brew, run:
brew update
brew install --HEAD usbmuxd
brew link usbmuxd
brew install --HEAD libimobiledevice
brew install ideviceinstaller
✗ ios-deploy not installed. To install with Brew:
brew install ios-deploy
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS platform side's plugin code that responds to your plugin usage on the Dart
side.
Without resolving iOS dependencies with CocoaPods, plugins will not work on iOS.
For more info, see https://flutter.io/platform-plugins
To install:
brew install cocoapods
pod setup
[✓] Android Studio (version 3.3)
[✓] VS Code (version 1.31.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

依照上面的提示信息,依次执行,安装 iOS toolchain 所有相关依赖。

连接IPhone真机运行

No valid code signing certificates were found

没想到第一次运行却报错了:

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
➜ flutter run
Launching lib/main.dart on xyz的iPhone in debug mode...
════════════════════════════════════════════════════════════════════════════════
No valid code signing certificates were found
You can connect to your Apple Developer account by signing in with your Apple ID
in Xcode and create an iOS Development Certificate as well as a Provisioning
Profile for your project by:
1- Open the Flutter project's Xcode target with
open ios/Runner.xcworkspace
2- Select the 'Runner' project in the navigator then the 'Runner' target
in the project settings
3- In the 'General' tab, make sure a 'Development Team' is selected.
You may need to:
- Log in with your Apple ID in Xcode first
- Ensure you have a valid unique Bundle ID
- Register your device with your Apple Developer Account
- Let Xcode automatically provision a profile for your app
4- Build or run your project again
5- Trust your newly created Development Certificate on your iOS device
via Settings > General > Device Management > [your new certificate] > Trust

For more information, please visit:
https://developer.apple.com/library/content/documentation/IDEs/Conceptual/
AppDistributionGuide/MaintainingCertificates/MaintainingCertificates.html

Or run on an iOS simulator without code signing
════════════════════════════════════════════════════════════════════════════════
No development certificates available to code sign app for device deployment

按照上面的提示,用 Xcode 打开示例项目中的 ios/Runner.xcworkspace 这个文件,

选择项目菜单 Runner – General 选项 ,可以看到 Signing 中的 Status 提示错误信息:

1
2
Signing for "Runner" requires a development team.
Select a development team in the projct editor.

点击 Team 选项的 Add Account... 按钮,登录 Apple ID ,没有则需要创建一个。回到 General 界面,选择 Team 名称。等待验证。

No profiles for xxx were found

现在又提示下面的错误:

在 VSCode中执行 flutter run 命令:

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
➜ flutter run
Launching lib/main.dart on xyz的iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project:
9Y41Q9PF28
Starting Xcode build...
Xcode build done. 1.3s
Failed to build iOS app
Error output from Xcode build:

** BUILD FAILED **


Xcode's output:

=== BUILD TARGET Runner OF PROJECT Runner WITH CONFIGURATION Debug ===
Code Signing Error: No profiles for 'com.example.helloWorld' were found: Xcode couldn't find any
iOS App Development provisioning profiles matching 'com.example.helloWorld'. Automatic signing is
disabled and unable to generate a profile. To enable automatic signing, pass
-allowProvisioningUpdates to xcodebuild.
Code Signing Error: Code signing is required for product type 'Application' in SDK 'iOS 12.1'
Code Signing Error: Code signing is required for product type 'Application' in SDK 'iOS 12.1'
Code Signing Error: Code signing is required for product type 'Application' in SDK 'iOS 12.1'

Could not build the precompiled application for the device.

It appears that your application still contains the default signing identifier.
Try replacing 'com.example' with your signing id in Xcode:
open ios/Runner.xcworkspace

Error launching application on xyz的iPhone.

在 XCode中编译,提示如下错误:

1
2
3
4
5
6
7
Failed to create provisioning profile. The app ID "com.example.helloWorld" cannot be registered to your development team. Change your bundle identifier to a unique string to try again.

No profiles for 'com.example.helloWorld' were found: Xcode couldn't find any iOS App Development provisioning profiles matching 'com.example.helloWorld'.

Code signing is required for product type 'Application' in SDK 'iOS 12.1'
Code signing is required for product type 'Application' in SDK 'iOS 12.1'
Code signing is required for product type 'Application' in SDK 'iOS 12.1'

通过一番查找,在 Stack Overflow 上找到了一个解决方法:

Bundle Identifier 的值改变一下,比如在结尾随机加上几个数字。

再次在 VS Code 中运行:

期间需要输入密码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜ flutter run 
Launching lib/main.dart on xyz的iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project:
9Y41Q9PF28
Starting Xcode build...
├─Assembling Flutter resources... 1.3s

└─Compiling, linking and signing... 3.4s

Xcode build done. 6.2s
Installing and launching... 4.6s
⢿2019-02-24 17:17:33.270 ios-deploy[15328:80609] [ !! ] Unable to locate DeviceSupport directory with suffix 'Symbols'. This probably means you don't have Xcode installed, you will need to launch the app manually and logging output will not be shown!
6.8s
Could not install build/ios/iphoneos/Runner.app on f1b5b10369882093413772c363995b5cda163c34.
Try launching Xcode and selecting "Product > Run" to fix the problem:
open ios/Runner.xcworkspace

Error launching application on xyz的iPhone.
Device is locked

使用 XCode编译,提示如下错误:

这个问题是需要在手机端开启允许调试。

依次点击 通用设备管理开发者应用 – 点击开发者账号 – 点击 信任应用

再次在 VS Code 中运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
➜ flutter run 
Launching lib/main.dart on xyz的iPhone in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project:
9Y41Q9PF28
Starting Xcode build...
├─Assembling Flutter resources... 1.4s

└─Compiling, linking and signing... 4.6s

Xcode build done. 7.7s
Installing and launching... 6.1s
12.0s
Syncing files to device xyz的iPhone... 1.7s

🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on xyz的iPhone is available at: http://127.0.0.1:1024/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

这次能够正常运行了,也支持热加载调试。


Flutter版本更新

对于Flutter的版本更新,看最近的形势迭代频率越来越快,而更新操作也很简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
➜ flutter doctor
╔════════════════════════════════════════════════════════════════════════════╗
║ A new version of Flutter is available! ║
║ ║
║ To update to the latest version, run "flutter upgrade". ║
╚════════════════════════════════════════════════════════════════════════════╝


Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.3)
[✓] VS Code (version 1.31.1)
[!] Connected device
! No devices available

! Doctor found issues in 1 category.

直接按照官方给出的提示执行 flutter upgrade 即可。


至此,Android和iOS的开发环境配置基本ok了。

如有疑问或需要技术讨论,请留言或发邮件到 service@itfanr.cc