flutter安装与环境配置

  • macOS系统上flutter安装与环境配置
  • 添加国内镜像
  • 获取flutter SDK
  • 获取Android SDK
  • 安装xcode
  • flutter安装和配置过程中的一些错误处理


macOS系统上flutter安装与环境配置

添加国内镜像

本地目录~/.bash_profile内,添加flutter国内镜像(command+shift+.查看隐藏文件,如没有.bash_profile文件,则新建一个)

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

获取flutter SDK

1.下载flutter,有两种方式
官网地址: https://flutter.dev/docs/development/tools/sdk/releases?tab=macos#macos github地址: https://github.com/flutter/flutter/releases

2.解压安装包到你想安装的目录,如:

cd ~/development
unzip ~/Downloads/flutter_macos_1.22.4-stable.zip

3.添加flutter相关工具到path中:

  • 针对当前命令行窗口设置PATH环境变量
export PATH=`pwd`/flutter/bin:$PATH
  • 永久将Flutter添加到PATH中(推荐),在~/.bash_profile内添加环境变量
export PATH=PATH_TO_FLUTTER_GIT_DIRECTORY/flutter/bin:$PATH

注意:PATH_TO_FLUTTER_GIT_DIRECTORY为你flutter的路径,比如“~/development”

export PATH=~/development/flutter/bin:$PATH

4.运行 source $HOME/.bash_profile刷新当前终端窗口
5.运行 flutter doctor,检测flutter SKD是否安装完成,同时查看其它安装项

flutter doctor

获取Android SDK

1.安装Android studio,安装过程中自动安装Android SDK
官网地址:https://developer.android.com/studio/index.html Android studio中文社区地址:http://www.android-studio.org

2.在~/.bash_profile内添加Android SDK的环境变量

export ANDROID_HOME="/Users/用户名/Library/Android/sdk"

注意:android sdk目录,“用户名”替换为你自己的即可
3.首次安装同意Android协议

flutter doctor --Android-licenses

4.安装Android studio plugins,安装flutter和dart插件
5.运行 flutter doctor,检测Android SKD是否安装完成

安装xcode

1.安装xcode
官网地址:https://developer.apple.com/download/

2.启动xcode,打开xcode > Preferences > locations,将Command Line Tools 设置为当前版本的xcode

3.切换路径

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

4.确保Xcode许可协议是通过打开一次Xcode或通过命令同意过了

sudo xcodebuild -license

5.运行 flutter doctor,检测xcode是否安装完成,如需安装依赖,按提示执行

flutter安装和配置过程中的一些错误处理

环境:(解决错误请注意自己的环境和版本信息)
mac 10.15.7
xcode 12.2
Command Line Tools for Xcode 12.2
Android studio 4.1

1.执行安装cocoapods时报错

sudo gem install cocoapods

错误信息:

package configuration for libffi is not found
"xcrun clang -o conftest -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/universal-darwin19 -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/backward -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -g -Os -pipe -DHAVE_GCC_ATOMIC_BUILTINS conftest.c  -L. -L/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib -L. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.Internal.sdk/usr/local/lib   -arch x86_64   -lruby.2.6   "
In file included from conftest.c:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/ruby.h:24:10: fatal error: 'ruby/config.h' file not found
#include "ruby/config.h"
         ^~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/ruby.h:24:10: note: did not find header 'config.h' in framework 'ruby' (loaded from '/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks')
1 error generated.
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

问题目录:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/universal-darwin19

问题原因:

安装需要的文件名,与自己系统内的文件名不一致

解决办法:

  • 查看自己电脑该路径下是universal-darwin20
  • 拷贝文件universal-darwin20,并重命名为universal-darwin19
  • 或者执行命令,创建软连接
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/universal-darwin20 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/universal-darwin19

2.flutter doctor提示Android studio没有安装flutter、dart插件,实际已经安装
问题原因:

在Android studio 4.1中plugin目录发生了变化

解决办法:

  • 用下面的指令软链接一下目录
ln -s ~/Library/Application\ Support/Google/AndroidStudio4.1/plugins ~/Library/Application\ Support/AndroidStudio4.1

3.flutter项目运行卡在Running Gradle task ‘assembleDebug’
问题原因:

由于网络无法访问到Google服务引起的

镜像链接:

maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }

解决办法:

  • 找到Flutte SDK目录下的Flutter打包配置文件flutter.gradle,路径为flutter\packages\flutter_tools\gradle\flutter.gradle,或者当前项目.flutter\packages\flutter_tools\gradle\flutter.gradle
  • 修改Flutter项目下的.android下的build.gradle,路径为.android/build.gradle