5

I am a native Android developer and I started using flutter SDK. I developed a simple app by following official flutter doc. But I found that the debug app size is 46 MB which is too large for this simple app. Is there any way to optimize the app size? because Flutter app size is larger than the native Android App.

MILAN MARWADI
  • 101
  • 1
  • 1
  • 7

7 Answers7

8

Flutter debug app is very big, to optimize the app you should build it as release version by using

flutter build apk (flutter build defaults to --release).

Preparing an Android App for Release

Preparing an iOS App for Release

JLouage
  • 259
  • 1
  • 3
  • Official Statement- ` The app size of a debug build is large due to the debugging overhead that allows for hot reload and source level debugging.` – Yash Jun 04 '20 at 17:04
  • Already tried twice, not solved my issue and app size. Any other suggestion? – Kamlesh Jul 30 '21 at 11:42
3

There are many possibilities:

First , build your application in release mode by using :

In your terminal : flutter build --release

or just specify the target :
For Android Apk : flutter build apk --release
For Android App Bundle: flutter build app bundle --release
For IOS : flutter build ios --release

By default, flutter run compiles to debug mode .This explains the large size of the application . Debug mode (Hot reload , Dart Devtools etc ..) vs Release Mode (Simple Application)

By default flutter build build for release mode . So you can just do flutter build

Using --split-debug-info flag can dramatically reduce code size. For an example of using this flag, see Obfuscating Dart code.

Some of the other things you can do to make your app smaller are:

  • Remove unused resources
  • Minimize resource imported from libraries
  • Compress PNG and JPEG files

Your can learn more about flutter app size here

Kab Agouda
  • 6,309
  • 38
  • 32
3

To Decrease/Optimise the App Size

Step 1: Compress assets all assets (Example: using tinypng or any other option)

Step 2: Delete unused resource

You can easily search for unused resources from Android Studio. Just press Ctrl + Alt +Shift + i and type "unused resources" (without quotes). That will execute lint. Super easy way to run lint commands (and other stuff from IDE). OR In Android Studio Menu > Refactor > Remove Unused Resources... Select the resources you want to remove. You can exclude resources you want to keep by right-clicking on the resource item. Use Do Refactor to remove all Resources at once. Update: use ⌘ +Option +Shift + i for mac

Step 3: Put this code in app/build.gradle

Refer to this link to know more about android ABIS (https://developer.android.com/ndk/guides/abis)

///Note: only working build release app ( cause debug run issue so comment it while run in debug mode )

  android {
    defaultConfig {
      minSdkVersion 19 
      .....
       //comment this code while app is in debug mode
      ndk {
           abiFilters "armeabi", "armeabi-v7a","arm64-v8a"
      }
    }
  }

Step 4: Remove unused plugins from pubspec.yaml

Step 5: Build Your App Bundle using the following command

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

For More

You can Use Proguard Rules

buildTypes { release { minifyEnabled true // add this proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // add this signingConfig signingConfigs.release // this is default for release } }

in the android main directory, you are able to find proguard-rules.pro if not then new-> file -> proguard-rules.pro create this file

Put this code in that file and also appropriate rules of plugins you have added in your pubspec.yaml Note: Add all proguards which is required by plugin otherwise android app will not work properly

## 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.google.firebase.** { *; } // uncomment this if you are 
using firebase in the project
-dontwarn io.flutter.embedding.**
-ignorewarnings

Also go to your gradle.properties and add :

extra-gen-snapshot-options=--obfuscate

in your app/build.gradle

buildTypes {
    release {
        minifyEnabled true // added previously
        shrinkResources true // add this
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro' // added previously
        signingConfig signingConfigs.release // added previously
    }
}

Another Command for build app

Old Apk with

  • flutter build apk --release Its

New Apk With

  • flutter build apk --split-per-abi

enter image description here

Sheetal Savani
  • 1,300
  • 1
  • 7
  • 21
0

TO REDUCE APK SIZE:

If you are building android apk or bundle, then make sure you are considering

Minify

Proguard

Some common things to keep in mind

clean codes

remove unused plugins

remove unused assets

remove unused fonts etc.

Community
  • 1
  • 1
vivek yadav
  • 1,367
  • 2
  • 12
  • 16
0

Try this,

flutter build apk --split-per-abi
Sadisha
  • 364
  • 1
  • 2
  • 10
  • Already tried twice, not solved my issue and app size. Any other suggestion? – Kamlesh Jul 30 '21 at 11:42
  • Flutter app size is large when comparing to a native android application's size. You can try removing unnecessary assets, compressing your assets and remove unnecessary things from imports. When you're uploading your app to play store, try to upload an app bundle, instead of apk files. Besause play store prefer app bundles. – Sadisha Aug 03 '21 at 10:32
  • Sadisha - Thanks for your quick reply. My app bundle size is 30.5 MB and I have removed all unnecessary plugins & files and also compressed all the images files and used google fonts instead of TTF font files but still not get any good success. I want to set my app size to 4-6 MB so that more and more users can download it easily. FYI this is just an application not game application. My app does not have any video. Any other suggestion, will be appreciated. Thanks a lot. – Kamlesh Aug 03 '21 at 12:48
  • App bundle contains all apk files for different devices. One of my project's bundle has a size of 19MB. But in play store, it shows 7.2MB. – Sadisha Aug 04 '21 at 14:48
0

Follow the official doc - https://flutter.dev/docs/perf/app-size

Some of the obvious things you can do to make your app smaller are:

  1. Remove unused resources
  2. Minimize resource imported from libraries
  3. Support a limited number of screen densities
  4. Compress PNG and JPEG files
Utkarsh Mankad
  • 247
  • 1
  • 6
  • 18
0

Android Studio includes an APK Analyzer that provides immediate insight into the composition of your APK or Android App Bundle after the build process completes. Using the APK Analyzer can reduce the time you spend debugging issues with DEX files and resources within your app, and help reduce your APK size. It's also available from the command line with apkanalyzer.

Open the project:

  • Drag an APK or app bundle into the Editor window of Android Studio.
  • Switch to the Project perspective in the Project window and then double-click the APK in the default build/output/apks/ directory.
  • Select Build > Analyze APK in the menu bar and then select your APK or app bundle

There's an explanation for this: https://developer.android.com/studio/command-line/apkanalyzer

Mobin Ansar
  • 631
  • 2
  • 13