Flutter:任务':app:transformClassesAndResourcesWithProguardForRelease'

问题描述

每次尝试运行Flutter build appbundle时,我都会不断收到错误消息。

这是在终端中显示错误消息:

Warning: there were 10 unresolved references to classes or interfaces.
         You may need to add missing library jars or update their versions.
         If your code works fine without the missing classes,you can suppress
         the warnings with '-dontwarn' options.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)

FAILURE: Build Failed with an exception.

* What went wrong:
Execution Failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
> java.io.IOException: Please correct the above warnings first.

* 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 2m 2s

不确定是什么意思。有什么想法吗?

解决方法

我希望你现在已经解决了这个问题,但想留下一个答案以供将来参考:

  1. 在 app 文件夹中创建一个名为 proguard-rules.pro 的文件(项目目录 -> android -> app -> proguard-rules.pro)
  2. 在文件里面,写
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-ignorewarnings
  1. 更新 build.gradle(项目目录 -> android -> app -> build.gradle),将此行添加到 buildTypes 块:
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'

来自 build.gradle 的示例代码段:

defaultConfig {
        applicationId "com.company.appname"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            shrinkResources true
            minifyEnabled true
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }

然后运行 ​​flutter build appbundleflutter build apk --split-per-abi 应该不会导致任何问题。