Flutter - 如何减小应用在设备上的大小

25

我的应用在Google Play商店的下载大小大约为23 MB

enter image description here

但是为什么安装后,磁盘上的应用程序大小会显著增加到83 MB呢?

enter image description here

enter image description here

有什么想法为什么会发生这种情况,因为:
1)Flutter项目中的资产文件(图像、声音和字体)仅约为11 MB
2)磁盘上Flutter源代码(.dart)的大小仅为1.4 MB
3)Android本地资源/图标(Res)仅约为800 KB
关于我使用的Flutter插件:{{插件名称}}
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  angles: ^1.0.1
  app_settings: ^1.0.5
  archive: ^2.0.8
  cached_network_image: ^1.1.1
  carousel_slider: ^1.3.0
  cron: ^0.2.1
  crypto: ^2.0.6
  cupertino_icons: ^0.1.2
  date_format: ^1.0.5
  device_id: ^0.1.3
  dio: ^2.0.0
  easy_alert: ^0.0.2
  localstorage: ^2.0.0
  audioplayers: ^0.13.1
  path_provider: ^1.1.0
  esys_flutter_share: ^1.0.2
  flutter_compass: ^0.3.2
  flutter_html: ^0.9.6
  flutter_local_notifications: ^0.8.2
  flutter_offline: ^0.2.4+1
  flutter_swiper: ^1.1.6
  flutter_xlider: ^2.4.5
  font_awesome_flutter: ^8.2.0
  geolocator: ^5.0.1
  google_maps_flutter: ^0.2.0
  google_sign_in: ^4.0.1+1
  http: ^0.12.0+2
  libcalendar: ^0.2.0
  location : ^2.3.0
  numberpicker: ^1.0.0
  pigment: ^1.0.3
  rxdart: ^0.21.0
  shared_preferences: ^0.5.1+1
  shimmer: ^1.0.0
  unicode: ^0.1.0
  url_launcher: ^5.0.2
  validate: ^1.6.0
  vibrate: ^0.0.4
  after_layout: ^1.0.7+2
  draggable_scrollbar: ^0.0.4
  flutter_web_browser: ^0.11.0
  flutter_youtube: ^1.1.6
  google_api_availability: ^2.0.1
  indexed_list_view: ^0.0.5
  permission_handler: ^3.1.0
  queries: ^0.1.4
  rect_getter: ^0.0.1
  scroll_to_index: any
  sqflite: ^1.1.5
  tuple: ^1.0.2
  xml2json: ^3.0.3
  firebase_messaging: ^5.1.6
  firebase_analytics: ^3.0.2
  firebase_core: ^0.4.0+3
  firebase_auth: ^0.11.1+6
  firebase_admob: ^0.9.0+1
  firebase_in_app_messaging: ^0.0.1+3
  superellipse_shape: ^0.1.5
  device_info: ^0.4.0+2
  in_app_purchase: ^0.2.0+6
  sentry: any
  system_info: ^0.1.1
  highlighter_coachmark: ^0.0.3
  volume: ^0.1.0
  admob_flutter: ^0.3.2
  bubble_tab_indicator: "^0.1.4"
  material_design_icons_flutter: ^3.0.3289
  scoped_model: ^1.0.1
  flutter_slidable: ^0.4.9
  kiwi: ^0.1.0
  flutter_markdown: ^0.2.0
  flutter_downloader: ^1.2.1
  native_ads: ^0.2.0  
  onesignal_flutter: ^2.0.0

有什么想法可以减小应用程序的大小吗?

提前感谢您...

更新

如果我在build.gradle中设置minifyEnabled = trueshrinkResources = true(如Ravinder Kumar建议的那样),则会生成一个非常长的错误,以下是日志:

== Building for Android ==

... ( I cut the error log because of very long, but full log can bee seen here: https://www.dropbox.com/s/jy92d1lmj9pgb6d/full_flutter_error_log.txt?dl=0 )

Note: the configuration keeps the entry point 'io.flutter.plugins.googlesignin.GoogleSignInPlugin$Delegate$3 { void onComplete(com.google.android.gms.tasks.Task); }', but not the descriptor class 'com.google.android.gms.tasks.Task'

Note: the configuration keeps the entry point 'io.flutter.plugins.inapppurchase.Translator { java.util.HashMap fromSkuDetail(com.android.billingclient.api.SkuDetails); }', but not the descriptor class 'com.android.billingclient.api.SkuDetails'

Note: the configuration keeps the entry point 'io.flutter.plugins.inapppurchase.Translator { java.util.HashMap fromPurchase(com.android.billingclient.api.Purchase); }', but not the descriptor class 'com.android.billingclient.api.Purchase'

Note: the configuration keeps the entry point 'io.flutter.plugins.inapppurchase.Translator { java.util.HashMap fromPurchasesResult(com.android.billingclient.api.Purchase$PurchasesResult); }', but not the descriptor class 'com.android.billingclient.api.Purchase$PurchasesResult'

Note: there were 7 references to unknown classes.

     You should check your configuration for typos.

    (http://proguard.sourceforge.net/manual/troubleshooting.html#unknownclass)

Note: there were 3 references to unknown class members.

     You should check your configuration for typos.

Note: there were 242 unkept descriptor classes in kept class members.

     You should consider explicitly keeping the mentioned classes

     (using '-keep').

     (http://proguard.sourceforge.net/manual/troubleshooting.html#descriptorclass)

Note: there were 68 unresolved dynamic references to classes or interfaces.

     You should check if you need to specify additional program jars.

     (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclass)

Warning: there were 25 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)

Warning: there were 2 unresolved references to library class members.

        You probably need to update the library versions.

        (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedlibraryclassmember)

Warning: Exception while processing task java.io.IOException: Please correct the above warnings first.

Thread(Tasks limiter_1): destruction Running Gradle task 'bundleRelease'... 690.7s (!)

************************************************************************>******************* The Gradle failure may have been because of AndroidX >incompatibilities in this Flutter app. See "https : //goo.gl / CP92wY" for more information on the problem and >how to fix it. ************************************************************************>******************* Gradle task bundleRelease failed with exit code 1

Build failed :| Failed to build for Android

顺便说一下,以下是我的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.**  { *; }
-keep class com.baseflow.** { *; }
#-keep class com.chartboost.** { *; }

-dontwarn com.google.common.base.**
-keep class com.google.common.base.** {*;}
-dontwarn com.google.errorprone.annotations.**
-keep class com.google.errorprone.annotations.** {*;}
-dontwarn com.google.j2objc.annotations.**
-keep class com.google.j2objc.annotations.** { *; }
-dontwarn java.lang.ClassValue
-keep class java.lang.ClassValue { *; }
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-keep class org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement { *; }

也许这与Dart快照有关。https://github.com/dart-lang/sdk/wiki/Snapshots - Ali Qanbari
3个回答

11

我的答案基于 官方文档


除此之外,你需要启用proguard来减小apk的大小并防止应用程序被反向工程。 在Flutter中,

为了使应用尽可能小,您应该在发布版本中启用缩小功能,以删除未使用的代码和资源。

默认情况下,Flutter不会混淆或缩小Android主机。如果您打算使用第三方Java、Kotlin或Android库,则可能需要减小APK的大小或保护该代码免受反向工程。

请按照此链接启用您的应用程序中的progaurd,

步骤1:配置Proguard 创建一个/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.**  { *; }
-dontwarn io.flutter.embedding.**

步骤2:启用混淆和/或缩小

android {

    ...

    buildTypes {

        release {

            signingConfig signingConfigs.release

            minifyEnabled true
            shrinkResources true// to shrink your image resource read more at https://developer.android.com/studio/build/shrink-code
            useProguard true// enables progaurd

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

        }
    }

注意: 仅在生产apk中使用Proguard。

此外,请阅读为什么我的应用程序在设备上的大小与APK或Play商店大小不同


一个应用程序也依赖于本机库,大多数本机库都是android框架库,在设备上可用,因此它们不会捆绑在apk / aab中。但是,这些库将包含在odex文件中,并在安装时增加其大小。

问题: 可能的预防措施是什么?

答案: 从库中最小化资源使用

如果某个库是为服务器或桌面设计的,则可能包含许多您的应用程序不需要的对象和方法。为了仅包括您的应用程序所需的库部分,如果许可证允许修改库,则可以编辑库的文件。您还可以使用替代的移动友好型库为您的应用程序添加特定功能。

请注意:代码缩小可以清理库中的一些不必要的代码,但它可能无法删除一些大的内部依赖项。

也请阅读此处提到的步骤

https://developer.android.com/topic/performance/reduce-apk-size#reduce-code


如果我将 minifyEnabled = trueshrinkResources = true,它会产生许多错误。 - questionasker
你是否已经创建了文件 /android/app/proguard-rules.pro?请阅读此链接:https://github.com/flutter/flutter/issues/15100#issuecomment-384183966。 - Ravinder Kumar
是的,请阅读我的更新答案,我在底部发布了“proguard-rules.pro”的内容... - questionasker
你可能想要阅读这篇文章,并且在Flutter的GitHub仓库中有一个开放问题与你的问题相关。看起来是Google或Firebase插件与Proguard冲突,否则它应该可以正常工作。 - Ravinder Kumar
@Racinder 通过查看 Github 的问题,现在没有减小应用程序大小的方法吗?我不认为他们会很快有解决方案... - questionasker
显示剩余8条评论

0
请使用以下命令。
flutter build apk --split-per-abi

已经尝试了两次,但是问题和应用程序大小仍未解决。还有其他建议吗? - Kamlesh

0

3
使用按ABI分割的目的是什么? - questionasker
已经尝试了两次,但是问题和应用程序大小仍未解决。还有其他建议吗? - Kamlesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接