Java Flutter 应用体积过大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49064969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:57:00  来源:igfitidea点击:

Flutter apps are too big in size

javaandroiddartflutter

提问by Steephan Selvaraj

I have good experience in android app development using java. Recently I came to know about flutter. So, I have tried to create a simple android app with flutter based on official tutorial. But surprisingly the debug app size is 25MB and release apk costs more than 7MB. It is really larger when compare with native developed android app.

我在使用 java 开发 android 应用程序方面有很好的经验。最近我开始了解flutter。因此,我尝试根据官方教程创建一个带有 flutter 的简单 android 应用程序。但令人惊讶的是,调试应用程序大小为 25MB,发布 apk 成本超过 7MB。与原生开发的 android 应用程序相比,它确实更大。

Is there any way to optimize it?

有什么办法可以优化吗?

采纳答案by Vilokan Labs

Flutter team acknowledges it here.

Flutter 团队在这里承认这一点

There's an explanation for this here, quoting the same -

对此有一个解释here,引用相同的-

In August 2018, we measured the size of a minimal Flutter app (no Material Components, just a single Center widget, built with flutter build apk), bundled and compressed as a release APK, to be approximately 4.7MB.

For this simple app, the core engine is approximately 3.2MB (compressed), the framework + app code is approximately 840KB (compressed), the LICENSE file is 55KB (compressed), necessary Java code (classes.dex) is 57KB (compressed), and there is approximately 533KB of (compressed) ICU data.

Of course, YMMV, and we recommend that you measure your own app, by running flutter build apk and looking at build/app/outputs/apk/release/app-release.apk.

2018 年 8 月,我们测量了一个最小的 Flutter 应用程序(没有材料组件,只有一个中心小部件,使用 flutter build apk 构建)的大小,捆绑并压缩为发布 APK,大约为 4.7MB。

对于这个简单的app,核心引擎大约3.2MB(压缩),框架+app代码大约840KB(压缩),LICENSE文件55KB(压缩),必要的Java代码(classes.dex)57KB(压缩) ,并且有大约 533KB 的(压缩的)ICU 数据。

当然,YMMV,我们建议您通过运行 flutter build apk 并查看 build/app/outputs/apk/release/app-release.apk 来衡量您自己的应用程序。

Also, the relative differences in apk size would likely be smaller with larger apps. Flutter's overhead size is fixed.

此外,对于较大的应用程序,apk 大小的相对差异可能会更小。Flutter 的开销大小是固定的

回答by Hemanth Raj

Yes ofcourse, the size of the apk or ipa built with flutter will be minimum of ~7mb for a hello world app. This is because, flutter ships a core engine, framework, ICU data, LICENSE file etc with its build output which are mandatory for a flutter app to run.

是的,当然,对于 hello world 应用程序,使用 flutter 构建的 apk 或 ipa 的大小至少约为 7mb。这是因为,flutter 附带了一个核心引擎、框架、ICU 数据、许可证文件等及其构建输出,这些输出是 flutter 应用程序运行所必需的。

You can check out the FAQ hereto know more about what takes how much size when build.

您可以在此处查看常见问题解答以了解有关构建时需要多少大小的更多信息。

Hope that helps!

希望有帮助!

回答by nima moradi

all this kind of cross-platform app are larger from start
i did work with react-native and hello-world app is about 6 MB
all you can do is make a two release apk ,one for arm cpu and one for x86 cpu in this way you can lower the size about 4 MB but never gonna be small as android

所有这种跨平台应用程序从一开始就更大,
我确实使用了 react-native 和 hello-world 应用程序大约 6 MB
你所能做的就是制作两个版本的 apk,一个用于 arm cpu,一个用于 x86 cpu您可以将大小降低约 4 MB 但永远不会像 android 一样小

according to google

根据谷歌

one way to reduce the size of your APK is to create multiple APKs that contain files for specific screen densities or ABIs.

减小 APK 大小的一种方法是创建多个包含特定屏幕密度或 ABI 文件的 APK。


check here for more info https://developer.android.com/studio/build/configure-apk-splits.html


在此处查看更多信息https://developer.android.com/studio/build/configure-apk-splits.html

UPDATE: if my it not fully supported now it ,will be in future for sure, in every phase of flutter development it will be this method work but not too much in beta release , but this method will work better by releasing more version of flutter

更新:如果我现在没有完全支持它,那么肯定会在未来,在flutter开发的每个阶段都会使用这种方法,但在beta版本中不会太多,但是通过发布更多版本的flutter,这种方法会更好地工作

回答by AlexPad

I also came across in this very apk condition when debugging, while in releases around 7-8 mb. This however we could see it in much larger apps, where natively speaking, we need to import many libraries, while with Flutter the work is optimized. So if we assume an app that natively should weigh around 30mb with Flutter it should be similar. What can scare you is in the very basic apps. The important thing however is to optimize the images

我在调试时也遇到了这种非常 apk 的情况,而在 7-8 mb 左右的版本中。然而,我们可以在更大的应用程序中看到它,从本机来说,我们需要导入许多库,而使用 Flutter 优化了工作。因此,如果我们假设一个应用程序在使用 Flutter 时本机应该重约 30mb,它应该是相似的。可以吓到您的是非常基本的应用程序。然而,重要的是优化图像

回答by Mr. Adrien Feudjio

Here is Official Android documentation that made my apps go From ~20mb To ~9mb

这是官方 Android 文档,它使我的应用程序从 ~20mb 到 ~9mb

Try the Proguardrecommendation

尝试Proguard建议

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

链接:https: //developer.android.com/topic/performance/reduce-apk-size

Before and After using ProGuard and moreenter image description here

使用 ProGuard 之前和之后等在此处输入图片说明

回答by Canaan Etai

One way that i use to reduce my app size is to use;

我用来减少应用程序大小的一种方法是使用;

flutter clean

before i run the build command;

在我运行构建命令之前;

flutter build appbundle --target-platform android-arm,android-arm64

When i run the build command without the clean command, i get around 32mb, but if i run the clean command first, i get around 18mb

当我在没有 clean 命令的情况下运行 build 命令时,我得到大约 32mb,但是如果我先运行 clean 命令,我得到大约 18mb

回答by Blasanka

First check these:

首先检查这些:

  • As other answer mentioned remove all unnecessary assets(images, fonts and files).
  • 正如其他答案所提到的,删除所有不必要的资产(图像、字体和文件)。

If you have too many fonts that will affect apk size heavily and flutter also made a solution for that by creating a package for you to get fonts from google fonts library(awesome package that give you access to so much fonts and flexibility to use anywhere). Get the package hereand Read more here.

如果你有太多的字体会严重影响 apk 的大小,flutter 也通过为你创建一个包来解决这个问题,让你从谷歌字体库中获取字体(很棒的包,让你可以访问如此多的字体和在任何地方使用的灵活性) . 在此处获取软件包在此处阅读更多信息

  • Remove unnecessary packages/ plugin that doesnt use(Not much affect though).

  • Remove unused resources

  • Minimize resource imported from libraries

  • Support a limited number of screen densities

  • Compress PNG and JPEG files

  • 删除不需要的包/插件(虽然影响不大)。

  • 删除未使用的资源

  • 尽量减少从图书馆导入的资源

  • 支持有限数量的屏幕密度

  • 压缩 PNG 和 JPEG 文件

Read this also: Measuring your app's size

另请阅读:测量应用的大小

Please note these too:

请注意这些:

If you build your apk using flutter build apkit will contains both arm-32 and arm-64 apks(Which flutter will show in conole when you building apk). If you are building app bundle this is not an issue and its size is much smaller.

如果您使用flutter build apk它构建您的 apk ,它将同时包含 arm-32 和 arm-64 的 apk(在您构建 apk 时,它们会在控制台中显示)。如果您正在构建应用程序包,这不是问题,它的大小要小得多。

To avoid one flat apk containing arm-32 and arm-64, you can build them separately using below two commands:

为了避免一个包含 arm-32 和 arm-64 的平面 apk,您可以使用以下两个命令分别构建它们:

flutter build apk --target-platform=android-arm

Above will produce arm-32bit apk. Go to project -> build -> app -> releaseand rename the apk to this: app-armeabi-v7a-release.apk.

以上将产生arm-32位apk。转到project -> build -> app -> release并将 apk 重命名为:app-armeabi-v7a-release.apk

then increment version code in pubspec.yaml, next flutter pub getand do this:

然后在 pubspec.yaml 中增加版本代码,flutter pub get然后执行以下操作:

flutter build apk --target-platform=android-arm64

Above will produce arm-64bit apk. Go to project -> build -> app -> releaseand rename the apk to this: app-arm64-v8a-release.apk.

以上将产生arm-64位apk。转到project -> build -> app -> release并将 apk 重命名为:app-arm64-v8a-release.apk

Then you can submit two apks separately(lower apk version first).

然后你可以分别提交两个apk(先提交较低的apk版本)。

Since, you have to run two commands by incrementing version code, flutter made it easier by this command (flutter > 1.5.4 I think): flutter build apk --split-per-abi. That command will increment apk version code for the second apk and give you two renamed apks (Please note that this command will produce with higher version code(ex: 3222)).

因为,你必须通过增加版本代码运行两个命令,扑通过这个命令(扑> 1.5.4我认为),使得它更容易:flutter build apk --split-per-abi。该命令将增加第二个 apk 的 apk 版本代码,并为您提供两个重命名的 apk(请注意,此命令将生成更高的版本代码(例如:3222))。

From doc:

来自文档:

From the command line:

Enter cd <app dir>
(Replace <app dir> with your application's directory.)
Run `flutter build apk --split-per-abi`
(The flutter build command defaults to `--release`.)

This command results in two APK files:

<app dir>/build/app/outputs/apk/release/app-armeabi-v7a-release.apk
<app dir>/build/app/outputs/apk/release/app-arm64-v8a-release.apk
<app dir>/build/app/outputs/apk/release/app-x86_64-release.apk

Removing the --split-per-abiflag results in a fat APK that contains your code compiled for all the target ABIs. Such APKs are larger in size than their split counterparts, causing the user to download native binaries that are not applicable to their device's architecture

从命令行:

Enter cd <app dir>
(Replace <app dir> with your application's directory.)
Run `flutter build apk --split-per-abi`
(The flutter build command defaults to `--release`.)

此命令会生成两个 APK 文件:

<app dir>/build/app/outputs/apk/release/app-armeabi-v7a-release.apk
<app dir>/build/app/outputs/apk/release/app-arm64-v8a-release.apk
<app dir>/build/app/outputs/apk/release/app-x86_64-release.apk

删除该--split-per-abi标志会生成一个包含为所有目标 ABI 编译的代码的胖 APK。此类 APK 的大小比拆分的 APK 大,导致用户下载不适用于其设备架构的本机二进制文件

read more here.

在这里阅读更多

I also heard somewhere that in latest flutter update they have made flutter app size even smaller. There is a added issue for this also: https://github.com/flutter/flutter/issues/16833

我还在某处听说,在最新的 Flutter 更新中,他们使 Flutter 应用程序的大小变得更小。还有一个额外的问题:https: //github.com/flutter/flutter/issues/16833

回答by ABADA SAMUEL

You could use the apk analyzer in android studio to see what causes the app size to be that large.

您可以使用 android studio 中的 apk 分析器来查看导致应用程序大小如此之大的原因。

  • In the case of having large and multiple image assets, you might want to use SVGs instead of PNGs.
  • Where you have to use PNGs or JPGs, you should compress them.
  • You might also want to use cached_network_imageand call the image from some external service like Firebase. This will load the image from the internet on first launch and cache it to your app. You can check out cached_network_imageon pub.dev.
  • For your fonts, you should use google fontsinstead of binding the font files to your app. You can check out google fontson pub.dev.
  • You should also shrink resources and set minifyEnable to true in your build.gradle in /android/app.
  • For uploading to playstore, you should generate and upload an app bundle instead.
  • 在拥有大型和多个图像资产的情况下,您可能希望使用 SVG 而不是 PNG。
  • 在必须使用 PNG 或 JPG 的地方,您应该压缩它们。
  • 您可能还想使用cached_network_image并从某些外部服务(如 Firebase)调用图像。这将在首次启动时从 Internet 加载图像并将其缓存到您的应用程序中。您可以在pub.dev上查看 cached_network_image。
  • 对于您的字体,您应该使用谷歌字体而不是将字体文件绑定到您的应用程序。你可以在 pub.dev 上查看谷歌字体
  • 您还应该在 /android/app 的 build.gradle 中缩小资源并将 minifyEnable 设置为 true。
  • 要上传到 Playstore,您应该生成并上传应用程序包。

You can follow this read on reducing your flutter app sizes. Some of the recommendations are also applicable to native android development. Reducing flutter app size

您可以按照此阅读减少 Flutter 应用程序的大小。一些建议也适用于原生 android 开发。减少颤动应用程序的大小

回答by B.shruti

For a debug apkbelow are steps for optimize -

对于调试APK下面是优化步骤-

  1. Analyzefor apk using Analyze APKfrom build tool of Android studioor manually by extracting the zip.

  2. In my case the biggest folder was libwhich contains 4 different folder named arm64-v8a, armeabi-v7a,x86,x86_64

  3. So these all four folders are basically different processor architectureof mobile devices.So to figure out your device falls under which category,below are some examples-

    ARM: This is a mobile processor architecture first and foremost, and what the majority of phones run now. Qualcomm's Snapdragon, Samsung's Exynos, and MediaTek's mobile chips are all examples of ARM processors. Most modern chips are 64-bit, or ARM64.

    x86: This is the architecture specification for Intel chips. As dominant as Intel is in the computer market, these chips are far less common in Android handsets. x86_64 refers to 64-bit Intel chips.*

  4. After finding your desired/required apk ,you can now split your apks based on these processor architecture using the command flutter build apk --debug --split-per-abi

  5. And finally check the build/app/outputs/apk/debug/app-arm64-v8a-debug.apk(here replace with your choice) folder to get your desired and reduced apk.

  1. 使用Analyze APKfrom 构建工具Android studio或通过提取 zip 手动分析apk 。

  2. 就我而言,最大的文件夹是lib,其中包含 4 个不同的文件夹,名为arm64-v8a, armeabi-v7a, x86,x86_64

  3. 所以这四个文件夹基本上是移动设备的不同处理器架构。所以要弄清楚你的设备属于哪个类别,下面是一些例子-

    ARM:这首先是一种移动处理器架构,也是现在大多数手机运行的架构。高通的Snapdragon、三星的Exynos、联发科的移动芯片都是ARM处理器的例子。大多数现代芯片都是 64 位或 ARM64。

    x86:这是英特尔芯片的架构规范。尽管英特尔在计算机市场占据主导地位,但这些芯片在 Android 手机中并不常见。x86_64 指的是 64 位英特尔芯片。*

  4. 找到您想要/需要的 apk 后,您现在可以使用命令flutter build apk --debug --split-per-abi根据这些处理器架构拆分您的 apk

  5. 最后检查 build/app/outputs/apk/debug/app-arm64-v8a-debug.apk(这里替换为您的选择)文件夹以获得您想要的和简化的 apk。