【原创】Exception: Gradle build fail
2021-12-24 本文已影响0人
吉凶以情迁
为了解决这个问题我刚开始的方向是错了,包括群里问人,发issues,谷歌搜索
https://github.com/flutter/flutter/issues/95722
首先 要确认一下app有没有生成,
app项目生成在build/app/outputs下,
模块则是另外的地方,我这里是已经生成的,而且从欧五提示的目录也是正常的,但是提示不够详细,因此导致我绕了半天。
unning Gradle task 'assembleDebug'... (completed in 26.5s)
[ +2 ms] LocalFile: 'C:\project\flutter\sotrunx\android\build.gradle' provides AGP version: 4.1.0
[ +2 ms] Exception: Gradle build failed to produce an .apk file. It's likely that this file was generated under C:\project\flutter\sotrunx\build, but the tool couldn't find it.
[ ] "flutter run" took 29,521ms.
[ +3 ms]
#0 throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
#1 RunCommand.runCommand (package:flutter_tools/src/commands/run.dart:684:9)
<asynchronous suspension>
#2 FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1161:27)
<asynchronous suspension>
#3 AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
<asynchronous suspension>
#4 CommandRunner.runCommand (package:args/command_runner.dart:209:13)
<asynchronous suspension>
#5 FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:281:9)
<asynchronous suspension>
#6 AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
<asynchronous suspension>
#7 FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:229:5)
<asynchronous suspension>
#8 run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:62:9)
如果没有生成,就flutter build -v
输出一下,而我上面是看不到任何详细的错误的,最后我追踪了源码
Never _exitWithExpectedFileNotFound({
required FlutterProject project,
required String fileExtension,
required Logger logger,
required Usage usage,
}) {
assert(project != null);
assert(fileExtension != null);
final String androidGradlePluginVersion =
getGradleVersionForAndroidPlugin(project.android.hostAppGradleRoot, logger);
BuildEvent('gradle-expected-file-not-found',
type: 'gradle',
settings:
'androidGradlePluginVersion: $androidGradlePluginVersion, '
'fileExtension: $fileExtension',
flutterUsage: usage,
).send();
throwToolExit(
'Gradle build failed to produce an $fileExtension file. '
"It's likely that this file was generated under ${project.android.buildDirectory.path}, "
"but the tool couldn't find it."
);
}
追踪
_exitWithExpectedFileNotFound
image.png
image.png
我的项目是.metadata
是定义的project_type: app
没错,但是pubspec.yaml
下面# module:
节点没有注释,所以导致程序查找错了位置,因此注释掉此处解决,
为什么出现这种问题呢?
是因为我本来就是模块开发,但是发现测试很多不方便,干脆写了一个app项目,把lib,assets和yaml都拷贝过去,为了解决这个问题,我把包名都保持一致,都没有找到根本原因,
根据源码终于找到问题所在。