Flutter实战-flutter混合开发报错:Failed to apply plugin [class ‘FlutterPlugin‘]

也忘了之前改过什么了,前几天有个朋友给我留言说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的目录地址导致的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失落夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值