Android Studio consumerProguardFiles for the library doesn't work

前端 未结 2 1605
余生分开走
余生分开走 2021-01-22 07:23

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) explicitl

相关标签:
2条回答
  • 2021-01-22 07:53

    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.

    0 讨论(0)
  • 2021-01-22 07:56

    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.

    0 讨论(0)
提交回复
热议问题