MILAN MARWADI
MILAN MARWADI

Reputation: 101

How to optimize the Flutter App size?

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.

Upvotes: 5

Views: 20084

Answers (7)

Sheetal Savani
Sheetal Savani

Reputation: 1428

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

Upvotes: 4

Mobin Ansar
Mobin Ansar

Reputation: 710

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

Upvotes: 1

Kab Agouda
Kab Agouda

Reputation: 7289

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

Upvotes: 3

Utkarsh Mankad
Utkarsh Mankad

Reputation: 247

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

Upvotes: 0

Sadisha
Sadisha

Reputation: 571

Try this,

flutter build apk --split-per-abi

Upvotes: 0

vivek yadav
vivek yadav

Reputation: 1547

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.

Upvotes: 0

JLouage
JLouage

Reputation: 269

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

Upvotes: 9

Related Questions