如何使用 Proguard 缩小 Flutter 应用程序?

Jan*_*.M. 16 android proguard minify dart flutter

我已经做了一个 Flutter 应用程序。发布的 apk 大约为 14MB。我搜索了缩小这个的方法,发现了这个:https : //flutter.io/android-release/#enabling-proguard

但我的问题是,我怎样才能知道我在第 1 步中使用的所有附加库?是否有任何命令可以了解它们,还是只是我添加到pubspec.yaml?

我需要如何在这个文件中实现它们/android/app/proguard-rules.pro

Ban*_*Dev 32

我将把这个答案留在这里,作为对任何必须处理这个问题并遇到这个线程的可怜灵魂的补充。

截至 2021 年 12 月,使用最新的 Android Sdk、Studio 和 Flutter 版本,如果您尝试这样做,useProguard true它将无法工作,因为它已经过时了。遗憾的是,Gradle 不会告诉你这一点,相反,你会收到如下错误:

A problem occurred evaluating project ':app'.

> No signature of method: build_7cqkbrda1q788z3b02yxbvrx9.android() is applicable for argument types: (build_7cqkbrda1q788z3b02yxbvrx9$_run_closure2) values: [build_7cqkbrda1q788z3b02yxbvrx9$_run_closure2@41108b15]
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,这完全是胡言乱语,没有用。秘诀就是useProguard根本不使用。默认情况下,Flutter 设置为使用 R8,它现在可以在 Android 上处理缩小。

这里有一篇关于此的帖子供参考:Gradle : DSL element 'useProguard' is obsolete and will be returned Soon

  • 已经为此奋斗了一个小时了。 (2认同)

Анд*_*чук 30

首先,我们将在构建文件中启用收缩和混淆。查找build.gradle位于/android/app/文件夹内的文件并以粗体添加行

android {

    ...

    buildTypes {

        release {

            signingConfig signingConfigs.debug     

            minifyEnabled true
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,我们将创建一个配置,该配置将保留整个 Flutter 包装器代码。创建/android/app/proguard-rules.pro文件并插入:

#Flutter Wrapper
-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.**  { *; }
Run Code Online (Sandbox Code Playgroud)

  • 不要使用此行来发布 apk-->signingConfigsigningConfigs.debug 使用-->signingConfigsigningConfigs.release (3认同)
  • flutter 包装器代码的 proguard 有更新吗?我在哪里可以找到这些默认的混淆器行? (2认同)

Ali*_*han 5

最新解决方案

首先你应该使用shrinkResources true而不是useProguard truein android/app/build.gradle

在第二个而不是使用proguard-android.txt你应该使用proguard-android-optimize.txt.

[更新] 注意:如果你不想优化,请使用proguard-android.txt配置文件而不是这个,这会关闭优化标志。添加优化会带来一定的风险,例如,并非 ProGuard 执行的所有优化都适用于所有版本的 Dalvik。要检查如何关闭各种优化,您需要阅读官方 Google Proguard 参考资料以了解更多信息。

buildTypes {
              
    release {
         signingConfig signingConfigs.release

         minifyEnabled true
         shrinkResources true
         proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
      }
}
Run Code Online (Sandbox Code Playgroud)

以下是新版本的混淆器配置规则,用于防止日志记录和其他改造保护:

# Flutter
-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.** { *; }

# Retrofit
-keepattributes Signature
-keepattributes Exceptions

# OkHTTP
-dontwarn okhttp3.**
-keep class okhttp3.**{ *; }
-keep interface okhttp3.**{ *; }

# Other
-keepattributes *Annotation*
-keepattributes SourceFile, LineNumberTable

# Logging 
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** i(...);
    public static *** w(...);
    public static *** e(...);
    public static *** wtf(...);
}

-assumenosideeffects class io.flutter.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** w(...);
    public static *** e(...);
}

-assumenosideeffects class java.util.logging.Level {
    public static *** w(...);
    public static *** d(...);
    public static *** v(...);
}

-assumenosideeffects class java.util.logging.Logger {
    public static *** w(...);
    public static *** d(...);
    public static *** v(...);
}

# Removes third parties logging
-assumenosideeffects class org.slf4j.Logger {
    public *** trace(...);
    public *** debug(...);
    public *** info(...);
    public *** warn(...);
    public *** error(...);
}
Run Code Online (Sandbox Code Playgroud)

要检查它,请安装jadx并打开your_app.apk并在 about 中搜索log。您应该对非 proguard .apk 和 proguard 配置的 .apk 日志搜索结果进行比较。

没有 Proguard 规则

proguard配置规则之前

使用 Proguard 规则

配置规则后

请阅读官方 Android 文档了解更多信息

阅读有关 Android 代码压缩的更多信息。


归档时间:

查看次数:

16569 次

最近记录:

4 年,10 月 前