Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "minifyEnabled" and "useProguard" in the Android Plugin for Gradle?

I see that the Android Plugin for Gradle has a minifyEnabled property as well as a useProguard property, as follows:

android {
    buildTypes {
        debug {
            minifyEnabled true
            useProguard false
        }
        release {
            minifyEnabled true
            useProguard true
        }
    }
}

What's the difference between these two properties? Or, rather, what's the meaning of each?

like image 604
Adil Hussain Avatar asked May 03 '16 14:05

Adil Hussain


People also ask

What is minifyEnabled in Android?

minifyEnabled true. // Enables resource shrinking, which is performed by the. // Android Gradle plugin. shrinkResources true. // Includes the default ProGuard rules files that are packaged with.

What is the use of minifyEnabled?

This explains it. So minifyEnabled removes dead code but does not obfuscate or optimize. Seems to directly contradict this answer which is saying that minify does obfuscate a bit stackoverflow.com/questions/17290023/… updated link to the documentation : developer.android.com/studio/build/…

Should I use R8 or ProGuard?

R8 is having a faster processing time than Proguard which reduces build time. R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4.

What is R8 Android studio?

R8 is an app shrinking tool that is used to reduce the size of your application. This tool present in Android Studio works with the rules of Proguard. R8 will convert your app's code into optimized Dalvik code.


3 Answers

Quoting from tools.android.com:

Built-in shrinker

Version 2.0 of Android Plugin for Gradle ships with an experimental built-in code shrinker, which can be used instead of ProGuard. The built-in shrinker supports fast incremental runs and is meant to speed up iteration cycles. It can be enabled using the following code snippet:

android {
    buildTypes {
        debug {
            minifyEnabled true
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    }
}

The built-in shrinker can only remove dead code, it does not obfuscate or optimize. It can be configured using the same files as ProGuard, but will ignore all flags related to obfuscation or optimization.

Unlike ProGuard, we support using the built-in shrinker together with Instant Run: depending on the project, it may significantly decrease the initial build and install time. Any methods that become reachable after a code change will appear as newly added to the program and prevent an Instant Run hotswap.

like image 135
Mattia Maestrini Avatar answered Oct 13 '22 11:10

Mattia Maestrini


You don't need useProguard true anymore.

Code shrinking with R8 is enabled by default when you set the minifyEnabled property to true.

When you build your project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler to handle the tasks according to the official document.

If you want to use ProGuard instead of R8. Add this line in the gradle.properties file

 android.enableR8=false
like image 25
Allen Avatar answered Oct 13 '22 12:10

Allen


I set minifyEnabled true for my release buildType and it removed an entire enum which it thougt to be unused code i guess. This made my app crash due to a NoSuchFieldException. Took me 4 hours to find the reason for this crash. 0/10 can not recommend minifyEnabled.

like image 19
J7bits Avatar answered Oct 13 '22 12:10

J7bits