Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio consumerProguardFiles for the library doesn't work

I'd like to use proguard on my library, but the file (rules) should be set inside the library. That means I don't want to set the rules w(hich belong to the library) explicitly in my app module.

I found that there is something like consumerProguardFiles property. My settings:

library gradle:

buildTypes {
    debug {
        debuggable true
        minifyEnabled false
    }
    release {
        minifyEnabled true
        consumerProguardFiles 'proguard-rules.pro'
    }
}

app gradle:

buildTypes {
    debug {
        applicationIdSuffix ".debug"
        debuggable true
        minifyEnabled false
        signingConfig signingConfigs.debug
    }
    release {
        debuggable false
        minifyEnabled true
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Why does configuration above doesn't work? I get errors that packages/symbols from the library cannot be found.


Edited:

What is important my proguard-rules.pro for the library and proguard-rules.pro for the main app module are both empty.

Exemplary errors:

Error:(3, 60) error: package (my library package here) does not exist
(...)
Error:(16, 9) error: cannot find symbol class NavigationStructureModel
(...)

More then one handrued errors. As I can see my all classes from the library are missing.

like image 319
AppiDevo Avatar asked Mar 12 '17 21:03

AppiDevo


People also ask

How do I add a library project to Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

What is consumerProguardFiles?

consumerProguardFiles 'consumer-rules.pro'} A consumer proguard rules file is like any other proguard rules files with the caveat that the rules inside it are automatically applied to the consuming application when the application is being built in a proguard enabled mode.


1 Answers

You need to figure out if you want to

  1. Run proguard on the library
  2. Run proguard on the app (Hint: It's this one.)

Run proguard on the library

That's BEFORE it reaches the application. Your library proguard rules are responsible for making all code accessible and present but you may obfuscate internal classes.

Here's the library build.gradle:

android {
    defaultConfig {
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    buildTypes {
        release {
            minifyEnabled true // Yes, run proguard on the library itself.
        }
    }
}

If you release this library as open source you don't need this. If you don't release the library to public you don't need this.

I get errors that packages/symbols from the library cannot be found.

Proguard builds a graph of dependencies among classes. At this point the library is a standalone unit so if you didn't supply any rules, none of the classes are ever used so proguard removes them all.

Run proguard on the app

Let the consumer (application) know, which classes of the library are to be kept.

Here's the library build.gradle:

android {
    defaultConfig {
        // Here's what proguard on the app should do on the library's behalf.
        consumerProguardFiles 'proguard-consumer-rules.pro' 
    }
    buildTypes {
        release {
            minifyEnabled false // No, don't proguard the library itself.
        }
    }
}

How does proguard work

You need to tell proguard what code to keep and what names to keep. Proguard removes all unused code and scrambles names you didn't want to stick around.

You don't tell proguard what to remove, you tell proguard what to keep.

like image 143
Eugen Pechanec Avatar answered Sep 27 '22 20:09

Eugen Pechanec