ProGuard in Flutter


ProGuard is a code optimization tool that helps you improve the performance of your Flutter apps.

In this blog post, we'll take a look at how ProGuard works and how you can use it to improve the performance of your Flutter apps.



ProGuard in Flutter


Let us first understand,

What is ProGuard used for?


ProGuard is a free Java class file shrinker, optimizer, and obfuscator. ProGuard project website states that ProGuard makes Java programming more efficient and secure.


ProGuard can detect and remove unused classes, fields, methods, and attributes. It can then optimize bytecode and remove unused instructions. Finally, ProGuard can rename classes and methods, and change the access modifiers of classes and methods.


ProGuard is most commonly used by Android developers to shrink and obfuscate their code. By using ProGuard, developers can reduce the size of their Android applications and make them more difficult to reverse engineer.


OK, so ProGuard is used to shrink Java class files. The next question that comes naturally to mind is.



Does Flutter use ProGuard?


ProGuard is a tool that is typically used on Java & Kotlin code.


To use ProGuard with your Flutter app, you will need to add the following to your app's build.gradle file:


buildTypes { 
	release { 
		minifyEnabled true 
		useProguard true 
		proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 
	} 
}


The first line enables ProGuard for the release build type. The second line tells ProGuard to use the default configuration file (proguard-android-optimize.txt) as well as a custom configuration file (proguard-rules.pro).

The default configuration file contains a set of rules that are known to work with most Java apps. The custom configuration file is where you will add any rules that are specific to your app.


Here is an example of a custom configuration file that you might use for a Flutter app:

-keep class * extends io.flutter.plugin.common.Plugin { *; } 
-keep public class * extends io.flutter.app.FlutterActivity { *; } 
-keep public class * extends io.flutter.view.FlutterView { *; }


The first line tells ProGuard to keep any class that extends the Plugin class. This is necessary because Flutter uses a plugin architecture. 

The second and third lines tell ProGuard to keep any public class that extends the FlutterActivity or FlutterView class. 

This is necessary because these are the two main types of activity that you will use in a Flutter app.


Here is another example that is often used in Flutter apps:

-dontwarn android.**
-keep class io.flutter.** {*;}
-dontwarn io.flutter.**

-keep class androidx.lifecycle.DefaultLifecycleObserver


To learn from more examples (8), please follow this article "How to enable ProGuard?".


You can add as many rules as you need to the configuration file. The more rules you add, the more time ProGuard will spend processing your code. 

You should only add rules for classes that you are using in your code. ProGuard will automatically remove any unused classes from your code.


Once you have added the ProGuard configuration to your app, you can build a release version of your app and ProGuard will process your code. 

ProGuard will output a report that shows you the names of the classes and methods that it was able to remove from your code. 

You can find this report in the build/outputs/mapping/release/ directory.


ProGuard is a powerful tool that can help you reduce the size of your app and make it more difficult to reverse-engineer your code. 

When used correctly, it can be a valuable addition to your app development toolkit.



Should you enable ProGuard?


Some developers choose to enable ProGuard in order to shrink the size of their APKs, optimize the code, and make it more difficult for reverse engineers to decompile their app.


However, ProGuard can also introduce bugs and make your app more difficult to debug. If you are not careful, you can end up with a broken build.


Therefore, it is important to weigh the pros and cons of enabling ProGuard before you decide to enable it in your own projects.


On the plus side, ProGuard can help you reduce the size of your APK. It can also optimize your code and make it more difficult for reverse engineers to decompile your app.


However, there are also some drawbacks to using ProGuard. It can introduce bugs into your code and make your app more difficult to debug. If you are not careful, you could end up with a broken build.


Only you can decide whether the benefits of ProGuard outweigh the risks. If you decide to enable ProGuard, be sure to test your app thoroughly before releasing it to the public.

Anil Rao K

Author: Anil Rao K

This website uses cookies to improve your experience. By continuing to use our site, you consent to our use of cookies. Learn More