Flutter体验—Windows 10环境搭建问题记录
环境信息
- 系统:Windows 10
- 工具:VSCode
准备工作
- 安装最新版 git,注意git需要将http缓存设置大一点,否则sdk拉取失败
- 安装最新版 Android Studio,注意要是最新的
- 安装jdk 17,注意不能是8或11,构建Android时会用到
- 配置全局gradle镜像,或者直接上代理最稳妥
按照官网:开始在 Windows 上构建 Flutter Android 应用 ,用vscode安装flutter。
用vscode创建模板项目,下载flutter sdk,android sdk:
[my_flutter_app] flutter create --template app --overwrite .
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Creating project ....
Resolving dependencies...
Downloading packages...
Got dependencies.
Wrote 129 files.
All done!
You can find general documentation for Flutter at: https://docs.flutter.dev/
Detailed API documentation is available at: https://api.flutter.dev/
If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev
In order to run your application, type:
$ cd .
$ flutter run
Your application code is in .\lib\main.dart.
安装检查
先运行一下 flutter doctor --android-licenses
然后执行 flutter doctor -v
检查环境
问题1:Could not determine java version
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
X Could not determine java version
明明已经安装了jdk17,JAVA_HOME和java -version都显示正确,就是找不到java版本,后排查发现装的 Android studio 下面有个 jre 目录,flutter 检测到的java总是指向这个目录。
解决方法:卸载安装最新版本Android studio,再次检测正常。
问题2:Unable to find bundled Java version.
[!] Android Studio (version 4.1)
• Android Studio at D:\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
X Unable to find bundled Java version.
• Try updating or re-installing Android Studio.
安装最新版本Android studio后发现这一项又检测不到java版本了,搜索得知 flutter 默认找的是 AS下的jre目录中的java,最新版AS没有这个目录,有个jbr
解决方法:将jrb目录发送到桌面快捷方式,然后移动到同目录下,重命名为jre,再次检测正常。
Windows构建
运行flutter run 后,选择 1 ,桌面环境,Windows很容易编译运行成功。
release构建命令
flutter build windows --release
编译过程
E:\Code\Flutter\my_flutter_app>flutter build windows --release
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Building Windows application... 54.1s
√ Built build\windows\x64\runner\Release\my_flutter_app.exe
Android构建
flutter在构建Android包的过程中不显示gradle的构建过程,可能不知道进度或失败在哪里了,可以先用Android studio打开项目下android目录,等依赖下载完成后再用命令行构建。
Android构建命令
flutter build apk --release
过程
E:\Code\Flutter\my_flutter_app>flutter build apk --release
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Font asset "MaterialIcons-Regular.otf" was tree-shaken, reducing it from 1645184 to 1384 bytes (99.9% reduction). Tree-shaking can be disabled by providing the --no-tree-shake-icons flag when building your app.
Running Gradle task 'assembleRelease'... 125.8s
√ Built build\app\outputs\flutter-apk\app-release.apk (17.9MB)
注意:gradle与jdk版本
按 Windows 搭建Flutter开发环境教程 配好gradle全局仓库镜像,执行Android构建,一直报错JDK路径有问题,新建的模板项目默认使用的gradle是8.3.0,必须需要jdk17才行,安装最新jdk后配置环境变量再次尝试。
如果还是JDK版本不对,此时需要给gradle指定jdk版本,在gradle.properties中增加
org.gradle.java.home=D:/Java/jdk-17
android 构建失败时,一般都是依赖包下载不下来,有可能是配置的gradle镜像有问题,最好配置gradle代理,然后用Android studio ,方便排查问题。
配置gradle http代理,还是在gradle.properties中增加
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=10809
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=10809
如果Android studio中依赖下载完成,一切正常,然后开始运行build,应该没什么大的意外,release版本的安装包只有17M,代理才是最简单的gradle出错解决方法。
常用命令
运行各个平台
flutter run -d windows
flutter run -d macos
flutter run -d linux
flutter run -d android
flutter run -d ios
flutter run -d web