最近将吃灰很久的电脑更新下 Flutter 开发环境 . 这里记录下所遇到的问题

更新直接按照官网文档 , 命令一敲直接等下载完成

Flutter 3 运行报错 :You need Java 11 or higher to build your app with this version of gradle

然后在运行项目到 Android 设备时报了 Flutter 3 运行报错 :You need Java 11 or higher to build your app with this version of gradle

这个在原生开发中一般都会遇到 , 现在常规都是设置 Android Studio 的 JDK 版本为内嵌的 Java 11 , 直接在 Project Structure 中设置即可 , 而这里我们是进行的 Flutter 开发 . 所以需要配置环境变量 . 在项目的 gradle.properties 添加 org.gradle.java.home=JAVA11路径 , 然后需要修改 build.gradle 文件

1
2
3
4
5
6
7
8
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = '11'
}

这样即可

iOS gem install 时权限问题的修正

运行项目到 iOS 设置时发现电脑的 cocoaspod 需要更新 , 就按照流程进行更新 , 后面发现遇到问题

1
2
3
4
$ gem install jekyll bundler
gem install jekyll bundler
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

可尝试下面这些解决方式。

根据这个回答,似乎加上 --user-install 参数就可以修正:

1
gem install name_of_gem --user-install

如果实测发现仍然不行:

1
2
3
4
5
6
$ gem install jekyll bundler --user-install
WARNING: You don't have /Users/wayongliu/.gem/ruby/2.6.0/bin in your PATH,
gem executables will not run.
Building native extensions. This could take a while...
ERROR: Error installing jekyll:
ERROR: Failed to build gem native extension.

不过这里的错误信息提示变了,和 PATH 有关,于是根据这个回答,修正一个 PATH:

1
$ PATH="`ruby -e 'puts Gem.user_dir'`/bin:$PATH"

fish 环境使用如下命令:

1
$ set PATH "`ruby -e 'puts Gem.user_dir'`/bin:$PATH"

或者添加如下命令到 shell 配置文件中:

1
set -gx PATH ~/.gem/ruby/2.6.0/bin $PATH

重装 Ruby

如果仍然不行,可以尝试使用 brew 重装 Ruby,抛弃 mac 自带的版本。安装过程输出的信息会有如下提示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
By default, binaries installed by gem will be placed into:
/usr/local/lib/ruby/gems/2.7.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
echo 'set -g fish_user_paths "/usr/local/opt/ruby/bin" $fish_user_paths' >> ~/.config/fish/config.fish

For compilers to find ruby you may need to set:
set -gx LDFLAGS "-L/usr/local/opt/ruby/lib"
set -gx CPPFLAGS "-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
set -gx PKG_CONFIG_PATH "/usr/local/opt/ruby/lib/pkgconfig"

根据提示,添加相应脚本的 shell 的配置中。

1
echo 'set -g fish_user_paths "/usr/local/opt/ruby/bin" $fish_user_paths' >> ~/.config/fish/config.fish

刷新配置:

1
$ source ~/.config/fish/fish.config

然后再执行 gem 安装相关的命令,应该就解决了。

1
2
3
4
5
6
7
8
9
10
$ gem install --user-install bundler jekyll
Fetching public_suffix-4.0.6.gem
Fetching i18n-1.8.5.gem
Fetching eventmachine-1.2.7.gem
Fetching http_parser.rb-0.6.0.gem
Fetching colorator-1.1.0.gem
Fetching addressable-2.7.0.gem
Fetching concurrent-ruby-1.1.7.gem
Fetching em-websocket-0.5.2.gem

检查安装:

1
2
$ jekyll -v                                                                                                  
jekyll 4.1.1

如果报 jekyll 命令找不到,检查 shell 配置中是 PATH 是否配置正确,见上面步骤。

Flutter检测不到 iOS 模拟器

安装的 Xcode 是 Beta 版本, 运行 Flutter 项目时检查不到模拟器

运行命令 flutter doctor 检查环境 , 可能会提示让执行

1
sudo xcode-select --switch /Application/Xcode/Contents/Developer

但执行后报错,提示没有这个文件

经检查,下载的Xcode在Application下的文件为Xcode.beta , 所以应该执行的命令为

1
sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer

再次运行 flutter doctor 问题解决