也忘了之前改过什么了,前几天有个朋友给我留言说flutterMix的工程github的链接打不开。我才想起来前些时间为了应付公司信息安全部门的要求,把我很多项目都给删掉了,如果想恢复只能重新创建上传。
本地flutter项目重新搭起来,报这个错误。百度谷歌了下,没有找到答案,好吧,只能靠自己了。
报错如下:
Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.
FAILURE: Build failed with an exception.
* Where:
Script '/Users/xxxxx/develop/develop_tool/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 151
* What went wrong:
A problem occurred evaluating script.
> Failed to apply plugin [class 'FlutterPlugin']
> flutter.sdk must point to the Flutter SDK directory
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
CONFIGURE FAILED in 0s
Cause: flutter.sdk must point to the Flutter SDK directory
报错发生在flutter的sdk目录下面,OK,那我们就打开flutter_tools这个工程看一下原因:
flutter_tools/gradle/flutter.gradle下面,
apply plugin: FlutterPlugin
class FlutterPlugin implements Plugin<Project> {
可以确定,基本上应该是这个类FlutterPlugin内部报错。
继续向下搜索错误原因:flutter.sdk must point to the Flutter SDK directory,我们找到了这一行:
String flutterRootPath = resolveProperty("flutter.sdk", System.env.FLUTTER_ROOT)
if (flutterRootPath == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file or with a FLUTTER_ROOT environment variable.")
}
flutterRoot = project.file(flutterRootPath)
if (!flutterRoot.isDirectory()) {
throw new GradleException("flutter.sdk must point to the Flutter SDK directory")
}
看起来,应该是flutterRoot指向错误的原因。
resolveProperty方法点进去看一下,大体上就是优先读取工程下面local.properties文件中的flutter.sdk值,如果没有,那么就去系统环境变量中找。
一开始我以为是没有设置环境变量中的flutter.sdk导致的,但是后来设置上了,还是不行报同样的错误。
所以后来灵机一动,直接编辑了上面的flutter.gradle文件,添加下面的这行代码:
String flutterRootPath = resolveProperty(project, "flutter.sdk", System.env.FLUTTER_ROOT)
if (flutterRootPath == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file or with a FLUTTER_ROOT environment variable.")
}
flutterRoot = project.file(flutterRootPath)
//添加这行代码,直接打印出来所有数据
throw new GradleException("FLUTTER_ROOT:"+System.env.FLUTTER_ROOT+",flutterRootPath:"+flutterRootPath+",project.projectDir.parentFile:"+project.projectDir.parentFile.getAbsolutePath())
if (!flutterRoot.isDirectory()) {
throw new GradleException("flutter.sdk must point to the Flutter SDK directory")
}
最后报错:
A problem occurred evaluating script.
> Failed to apply plugin [class 'FlutterPlugin']
> FLUTTER_ROOT:/Users/xxxx/develop/develop_tool/flutter,flutterRootPath:/Users/xxxx/develop/flutter,project.projectDir.parentFile:/Users/xxxx/develop/git_ware/flutter_mix/flutterMix/my_flutter/.android
OK,这样我们就知道了,原来读取的是
Users/xxxx/develop/git_ware/flutter_mix/flutterMix/my_flutter/.android
下面的local.properties文件中的flutter.sdk变量导致的,我们改下这个变量,指向正确的目录地址就解决问题了。
那么为什么会出现这个问题的原因也浮出了水面,肯定是我不注意间挪了flutter.sdk的目录地址导致的。