无法构建用于发布的 flutter apk

Can't build flutter apk for release

我可以正常构建调试 apk,但是当我尝试构建发布或配置文件 apk 时,出现以下错误:

FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 896

* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command 'C:\src\flutter\flutter\bin\flutter.bat'' 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 1s
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done                      62.3s
Gradle task assembleRelease failed with exit code 1

这是flutter.gradle中的第896行:

project.exec {
            logging.captureStandardError LogLevel.ERROR
            executable flutterExecutable.absolutePath
            workingDir sourceDir
            if (localEngine != null) {
                args "--local-engine", localEngine
                args "--local-engine-src-path", localEngineSrcPath
            }
            if (verbose) {
                args "--verbose"
            } else {
                args "--quiet"
            }
            args "assemble"
            args "--depfile", "${intermediateDir}/flutter_build.d"
            args "--output", "${intermediateDir}"
            if (performanceMeasurementFile != null) {
                args "--performance-measurement-file=${performanceMeasurementFile}"
            }
            if (!fastStart || buildMode != "debug") {
                args "-dTargetFile=${targetPath}"
            } else {
                args "-dTargetFile=${Paths.get(flutterRoot.absolutePath, "examples", "splash", "lib", "main.dart")}"
            }
            args "-dTargetPlatform=android"
            args "-dBuildMode=${buildMode}"
            if (trackWidgetCreation != null) {
                args "-dTrackWidgetCreation=${trackWidgetCreation}"
            }
            if (splitDebugInfo != null) {
                args "-dSplitDebugInfo=${splitDebugInfo}"
            }
            if (treeShakeIcons == true) {
                args "-dTreeShakeIcons=true"
            }
            if (dartObfuscation == true) {
                args "-dDartObfuscation=true"
            }
            if (dartDefines != null) {
                args "--DartDefines=${dartDefines}"
            }
            if (bundleSkSLPath != null) {
                args "-iBundleSkSLPath=${bundleSkSLPath}"
            }
            if (extraGenSnapshotOptions != null) {
                args "--ExtraGenSnapshotOptions=${extraGenSnapshotOptions}"
            }
            if (extraFrontEndOptions != null) {
                args "--ExtraFrontEndOptions=${extraFrontEndOptions}"
            }
            args ruleNames
        }

我在安装了相同 flutter 包的同一台笔记本电脑上构建了另一个应用程序,没有任何问题。

我读到 Pubspec.yaml 的包裹可能是原因,所以我做了以下操作:

1- 我试图在 pubspec.yaml 删除我的包并重建应用程序,但没有成功。

2- 我将相同的包添加到另一个应用程序并构建它,它是正常构建的。

所以,似乎软件包不是原因。

这是我的 Pubspec.yaml :

version: 1.0.0+1

environment:
  sdk: ">=2.5.2 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.0
  sqflite: ^1.1.7+1
  path: ^1.6.4
  path_provider: ^1.4.0
  provider: ^3.1.0+1
  http: ^0.12.2
  connectivity: ^0.4.9+2
  carousel_slider: ^2.2.1
  share: ^0.6.5
#  admob_flutter: ^0.3.4
  url_launcher: ^5.5.3
  photo_view: ^0.9.2
  firebase_core: ^0.5.0
#  firebase_messaging: ^6.0.12
  firebase_auth: ^0.18.0+1
#  cloud_firestore: ^0.14.0+2
#  firebase_dynamic_links: ^0.5.0+11
#  firebase_analytics: ^5.0.16
  google_sign_in: ^4.5.3
#  rxdart: ^0.24.1
  cached_network_image: ^2.3.1
#  in_app_purchase: ^0.3.4+5
#  firebase_mlkit_language: ^1.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter




flutter:
  fonts:
    - family:  Sons
      fonts:
       - asset: assets/fonts/Sons.ttf
       - asset: assets/fonts/sons_of_sons.ttf


  uses-material-design: true

  assets:
    - assets/varty.sqlite
    - assets/images/photo.jpg

Evan 的评论很有效并解决了问题:

This is a shot in the dark but do you use custom icons for fonts? I noticed you have two font files. If yes, try running the release apk command with --no-tree-shake-icons, I recently had an error like this with the same line number 896. Also, when you run the debug version, does the console say something like "this application cannot tree shake icons fonts. "?

只记得构建调试 apk,然后配置文件,最后使用相同的命令发布。