Per Christian Henden
Per Christian Henden

Reputation: 1585

How to include a proguard configuration in my Android library (AAR)

Android libraries, per the AAR file spec, includes a 'proguard.txt' file. My understanding is that this file declares how the library correctly can be obfuscated and minified. In my case I need to preserve some API-classes.

How can I declare the library's proguard.txt file in the library's build.gradle? And will this file be automatically read when creating an application (APK) that uses my library?

I didn't find this information in Android's Gradle Plugin User Guide.

Upvotes: 31

Views: 16534

Answers (2)

yoAlex5
yoAlex5

Reputation: 34205

ProGuard artefact

[ProGuard workflow]

Artefact not minified, Consumer solve it

Library is open-sourced but as a library developer you can provide a ProGuard file which will be take into account by consumer(app) by demand(minifyEnabled true in consumer). consumerProguardFiles in you library build.gradle. It adds proguard.txt file(is the same as .pro) in an artefact

For example your library is open-source and application developer wants to minify all

android {
    defaultConfig {
        //consumerProguardFiles '<file_path>'
        consumerProguardFiles 'proguard-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }
    //...
}

Artefact is minified

Library is closed-source - you are able to use the next possibility:

android {

    buildTypes {
        release {
            minifyEnabled true
            //proguardFiles project(':<project_name>').file('<file_path>')
            proguardFiles 'proguard-rules.pro'
        }
    }
    //...
}

*Please note that:

  • minifyEnabled true and proguardFiles project both should be set.
  • If you use single minifyEnabled true or <file_path> is wrong - classes.jar is empty.
  • If single proguardFiles project - no effect

As for build process on the example of library - application - all .class files will be merged into single archive with .dex extension

Upvotes: 8

Johan Halin
Johan Halin

Reputation: 700

In your Gradle file's default configuration closure, specify your Proguard file with consumerProguardFiles instead of proguardFiles. For example:

defaultConfig {
    consumerProguardFiles 'proguard.txt'
}

Upvotes: 55

Related Questions