Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - What is the purpose of multiDexKeepProguard ? How does it compare to proguardFiles close in gradle?

I have created a multidex app. But in regards to proguard i have the following in build.gradle:

    android {
    defaultConfig {
        ...
        multiDexEnabled true
    }
    productFlavors {
        dev {
            // Enable pre-dexing to produce an APK that can be tested on
            // Android 5.0+ without the time-consuming DEX build processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the production version.
            minSdkVersion 14
        }
    }
    buildTypes {
        release {
            minifyEnabled true
        ***    proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile 'com.android.support:multidex:1.0.1'
}

My question is about the progardFiles vs using multiDexKeepProguard. The documentation states:

File multiDexKeepProguard

Text file with additional ProGuard rules to be used to determine which classes are compiled into the main dex file.

If set, rules from this file are used in combination with the default rules used by the build system.

So if i do not use the multiDexKeepProguard then my classes still get compiled but may not end up in the main dex file, is that correct ? I am not clear how this differs from proguardFiles.

Android documentation also references this.

like image 979
j2emanue Avatar asked Apr 12 '17 22:04

j2emanue


People also ask

What is the use of minifyEnabled in Android?

Shrink your code. Code shrinking with R8 is enabled by default when you set the minifyEnabled property to true . Code shrinking (also known as tree shaking), is the process of removing code that R8 determines is not required at runtime.

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/…

What is the use of multidex in Android?

In Android, the compilers convert your source code into DEX files. This DEX file contains the compiled code used to run the app.

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.


1 Answers

If you're enabling proguard in your application it's usually necessary to define proguard rules. proguardFiles are meant to be the instructions for progurard to minify or obfuscate your app.

multiDexKeepProguard is specifically for telling multidex which files are important to load at app startup and therefore what to keep in the main dex. As far as i'm aware, it just uses the proguard syntax as a convenience. This is optional and will usually only be set if there is an issue at runtime.

like image 164
Travis Castillo Avatar answered Oct 04 '22 22:10

Travis Castillo