flutter build apk 打包报错

Posted hiperion

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter build apk 打包报错相关的知识,希望对你有一定的参考价值。

之前打包都是正常的,最近一直在开发新版,有段时间没打包了。上周五想打个测试包,发现报错了,各种clean找问题。但这打印信息甚少,无法定位。错误内容如下

Running Gradle task 'assembleRelease'...                        


FAILURE: Build failed with an exception.

* Where:
Script '/Users/xxx/Library/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1102

* What went wrong:
Execution failed for task ':app:compileFlutterBuildArm32Release'.
> Process 'command '/Users/xxx/Library/flutter/bin/flutter'' finished with non-zero exit value 1

* 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

BUILD FAILED in 1m 39s
Running Gradle task 'assembleRelease'...                          100.6s
Gradle task assembleRelease failed with exit code 1

根据这些信息似乎完全无法找到问题所在,我们可以在命令上加-v参数,这样就可以活动更详细的信息了。

flutter build apk -v

于是得到了如下信息:

[   +1 ms] [  +17 ms] Target aot_android_asset_bundle failed: IconTreeShakerException: Invalid ConstFinder result. Expected
"fontPackage" to be a String, "fontFamily" to be a String, and "codePoint" to be an int, got: codePoint: 0, fontFamily: null,
fontPackage: null, matchTextDirection: false.
[   +2 ms]            To disable icon tree shaking, pass --no-tree-shake-icons to the requested flutter build command
[        ]            #0      IconTreeShaker._parseConstFinderResult
(package:flutter_tools/src/build_system/targets/icon_tree_shaker.dart:311:9)
[        ]            #1      IconTreeShaker._findConstants
(package:flutter_tools/src/build_system/targets/icon_tree_shaker.dart:299:12)

我们似乎看到了关键的一句:
To disable icon tree shaking, pass --no-tree-shake-icons to the requested flutter build command
没错,在我们要执行的打包命令之后加上 --no-tree-shake-icons 参数就会忽略这个错误,可以顺利打包成功,但从AS的菜单中就无法进行打包操作了,因为无法自动添加这个参数。只能在terminal中指定打包命令。
可打包的命令如下:

flutter build apk -v --no-tree-shake-icons

实际问题所在是我们使用了Icon和IconData,但必须是const进行修饰,否则就会报这个错误。具体的原因还需要查询资料。但其实并不是所有的Icon前面都能加const,因为有时IconData中是变量为参数。
目前能想到的解决方法就是,尽量直接传const Icon()进来,不要传IconData。还有不要使用IconData(0),直接使用具体的IconData数据(Icons.error_outline)即可正常编译。

以上是关于flutter build apk 打包报错的主要内容,如果未能解决你的问题,请参考以下文章

flutter build apk 打包报错

flutter电子木鱼flutter 打包 android apk,记录配置签名的过程/调试的过程及flutter build apk放到手机上用。

flutter电子木鱼flutter 打包 android apk,记录配置签名的过程/调试的过程及flutter build apk放到手机上用。

Flutter App Build apk报错但运行正确解决

android studio 打包报错:Generate Signed APK: Errors while building APK. You can find the errors in the &

AS打包报错:Generate Signed APK: Errors while building APK. You can find the errors in the 'Messages&