简体   繁体   中英

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.

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.

Ok, since proguard might be eating up your classes, try something like this in your app 's proguard-rules.pro file:

keepclass my.library.package.name.** { *;}

that should tell proguard to save all classess from my.library.package.name so hopefully they won't be missing anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM