App Optimization: Proguard Vs R8

Proguard and R8 are both code shrinkers for Android apps. They both work by analyzing your app’s code and resources and then removing unused code, renaming classes and methods, and inlining code. This helps to reduce the size of your app and make it run faster.

Proguard is the older of the two tools, and has been around since 2007. It has been the default code shrinker for Android apps for many years, and is still widely used. Proguard works by analyzing your app’s code and resources, and then removing unused code and renaming classes and methods to make them shorter. This can help to reduce the size of your app, and make it run faster.

One of the key benefits of Proguard is that it is very customizable. You can use a configuration file to specify exactly which code and resources should be preserved, and which should be removed. This can be especially useful if your app uses libraries or frameworks that Proguard might otherwise remove.

Here’s an example of a Proguard configuration file:

-keep class com.example.myapp.** { *; }
-dontwarn com.example.myapp.**

This configuration file tells Proguard to keep all classes in the com.example.myapp package, and not to generate any warnings for these classes. This can be useful if you have code in your app that Proguard might otherwise remove, but that you want to keep.

R8 is a more recent code shrinker for Android apps. It was introduced in 2018, and is now the default code shrinker for Android apps. R8 works by analyzing your app’s code and resources, and then removing unused code, renaming classes and methods, and inlining code. This can help to reduce the size of your app, and make it run faster.

One of the key benefits of R8 is that it is faster and more efficient than Proguard. R8 uses a new optimization algorithm that can analyze and optimize your app’s code much more quickly than Proguard. This can help reduce the time it takes to build and deploy your app and make your development process more efficient.

Here’s an example of a R8 configuration file:

-keep class com.example.myapp.** { *; }
-dontwarn com.example.myapp.**

This configuration file is similar to the Proguard configuration file shown above. It tells R8 to keep all classes in the com.example.myapp package, and not to generate any warnings for these classes. This can be useful if you have code in your app that R8 might otherwise remove, but that you want to keep.

So which code shrinker is better: Proguard or R8? It really depends on your specific needs and requirements. If you need a lot of customization, and want to be able to specify exactly which code and resources should be preserved, then Proguard might be a better choice. On the other hand, if you want a faster and more efficient code shrinker, then R8 might be a better choice. In general, though, R8 is probably the better choice for most Android developers, since it is faster and more efficient than Proguard.

If you’re interested in learning how to enable Proguard or R8 and want to get started, check out this post on code shrinking.

You may also like...
[instagram-feed]