32

I already made a Flutter app. The release apk is about 14MB. I searched methods to minify this and found this ons: https://flutter.io/android-release/#enabling-proguard

But my question is, how can I get to know all my used additional libraries for step 1? Are there any commands to know them or is it just all the dependencies that I added to the pubspec.yaml ?

How do I need to implement them in this file /android/app/proguard-rules.pro?

Jan D.M.
  • 2,284
  • 2
  • 20
  • 32
  • 1
    I doubt pro guard will do much. Dart compile use tree shaking already. So unless you code directly in Java (which is unlikely), the win is minimal. Maybe that size comes from your assets. – Rémi Rousselet Sep 20 '18 at 16:08
  • That's possible, but at least I could give it a try if I know what rules I need to add to the proguard-rules.pro file. – Jan D.M. Sep 20 '18 at 16:18
  • Possible duplicate of [Flutter apps are too big in size](https://stackoverflow.com/questions/49064969/flutter-apps-are-too-big-in-size) – Robert Sep 20 '18 at 18:26
  • 1
    https://medium.com/@swav.kulinski/flutter-and-android-obfuscation-8768ac544421 – Dhaval Parmar Apr 10 '19 at 11:19
  • Did you find the answer for your question ? If yes, please share the answer. – K Pradeep Kumar Reddy Oct 25 '20 at 18:45

3 Answers3

55

First, we will enable shrinking and obfuscation in the build file. Find build.gradle file which sits inside /android/app/ folder and add lines in bold

android {

    ...

    buildTypes {
        release {
            signingConfig signingConfigs.debug     

            minifyEnabled true
            useProguard true

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

        }
    }
}

Next we will create a configuration which will preserve entire Flutter wrapper code. Create /android/app/proguard-rules.pro file and insert inside:

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

Note: Use signingConfigs.release instead signingConfigs.debug to build a apk or appbundle

Community
  • 1
  • 1
Andrii Kovalchuk
  • 4,351
  • 2
  • 36
  • 31
30

I will leave this answer here as an addition for any poor soul that has to deal with this issue and encounters this thread.

As of December 2021 with the latest Android Sdk, Studio and Flutter versions, if you try to douseProguard true it will not work because it's obsolete. Sadly, Gradle will not tell you this, instead, you will get an error like this:

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]

Which as you can see it is complete gibberish and not useful. The secret is to just not use useProguard at all. By default Flutter is setup to use R8 which handles minification now on Android.

Here is a so post about this for reference: Gradle : DSL element 'useProguard' is obsolete and will be removed soon

BananyaDev
  • 583
  • 5
  • 13
2

If you are using firebase, see Flutter build crashes using ProGuard with Firebase Auth

Next, you need to consider the dependencies in pubspec.yaml You can immediately ignore any pure Dart packages - you are just looking for plugins. Of these plugins, you are just interested in ones that make use of existing libraries. You will likely have added these to gradle. Those are the ones you need to protect from name shortening.

The simplest approach may just be to try it and see what package names pop up in the NoClassDefFoundError and keep iteratively adding them.

As Remi says, your gain will be minimal, so is it really worth the hassle. You should see some improvements in APK sizes over the coming releases.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112