31

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.

fasteque
  • 4,309
  • 8
  • 38
  • 50
Per Christian Henden
  • 1,514
  • 1
  • 16
  • 26
  • This seems to be a duplicate of http://stackoverflow.com/questions/26983248/proguard-ignores-config-file-of-library – kevinpelgrims May 12 '15 at 21:44
  • 1
    @kevinpelgrims This is about distributing an AAR file with embedded proguard configuration for external library users, I don't expect the answer to be the same, i.e. the library classes wouldn't be minified twice in the multi-module build. The questions are similar though. – Per Christian Henden May 13 '15 at 08:05

2 Answers2

55

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

defaultConfig {
    consumerProguardFiles 'proguard.txt'
}
stkent
  • 19,772
  • 14
  • 85
  • 111
Johan Halin
  • 701
  • 7
  • 10
  • 4
    `consumerProguardFiles` should be specified in `defaultConfig` rather than `buildTypes/release` so that it works if the consuming application proguards in both debug and release mode (e.g. to avoid the 65k dex method limit). – ashughes Sep 24 '15 at 16:02
  • if you are using the gradle experimental plugin(0.7.2) it causes this Error:Cause: org.gradle.api.internal.ExtensibleDynamicObject. – Loebre Jul 07 '16 at 13:34
  • good example of build.gradle is in this link: https://github.com/artem-zinnatullin/RxJavaProGuardRules/blob/master/rxjava-proguard-rules/build.gradle – ultraon Jul 11 '17 at 20:53
7

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

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
  • what do you mean "create library" and " distribute your library as archive"? I guess when creating a library it is intended to be used as dependency in other lib or app. In that case shouldnt it always be minifyEnabled false, since the "library" module here it does not know what function will be referenced by other lib/apps, and hard to determine what to shrink/obfuscate? – lannyf Dec 30 '20 at 15:36