12

From this reference:

https://github.com/codepath/android_guides/wiki/Building-your-own-Android-library

I have the following gradle file:

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.2"

    buildTypes {
        release {
            minifyEnabled true
            consumerProguardFiles 'consumer-proguard-rules.pro'
        }
    }
}

And the following proguard file:

-dontobfuscate
-optimizations !code/allocation/variable

-keep public class * {
    public protected *;
}

However all of my classes in the aar are missing when I run the 'assembleRelease' task to build my release aar file.

Any ideas?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
lostintranslation
  • 23,756
  • 50
  • 159
  • 262

3 Answers3

11

You should set minifyEnabled to false (the default value) : consumerProguardFiles is not used for immediate minification (i.e. when building the library itself). It it used when the consumer of the library (e.g. an application) is build.

Minifying the library before consumption is not recommended, because you can not know what parts will actually be used before the final build. Libraries usually don't do it. e.g. LeakCanary, ACRA, Butter Knife

bwt
  • 17,292
  • 1
  • 42
  • 60
  • I was trying to following something like this. https://medium.com/google-developers/smallerapk-part-2-minifying-code-554560d2ed40 If I set minify to false does proguard still run? – lostintranslation Jan 09 '18 at 13:47
  • Not immediately, proguard will run if the *consumer* choose to do it (with its own `minifyEnabled`). If it does, your `consumer-proguard-rules.pro` will be merged and applied – bwt Jan 09 '18 at 14:07
  • 1
    What if it's not an open source library (and the only code that are accessible to consumer apps are excluded from obfuscation and minification)? – amar Sep 21 '22 at 13:19
5

in general, a library must keep the all the methods and method names, required to operate it; eg:

-verbose

# -libraryjars ../app/libs
# -dontpreverify
# -dontobfuscate
# -dontshrink
# -dontoptimize

# keep class BuildConfig
-keep public class **.BuildConfig { *; }

# keep class members of R
-keepclassmembers class **.R$* {public static <fields>;}

the answer to the question should probably be:

# keep all public and protected method names,
# which could be used by Java reflection.
-keepclassmembernames class * {
    public protected <methods>;
}

there are most likely some more rules required, while the -verbose flag tells one what to write into there. when building the library against other code (as suggested above), one nevertheless needs to add more or less the same rules for the library. there are also quite some, not open source, libraries with obfuscated class names.

a library's build.gradle might look about like this:

android {
    defaultConfig {
        consumerProguardFiles 'proguard-consumer-rules.pro' 
    }
    buildTypes {
        release {
            minifyEnabled false // don't proguard the library itself.
        }
    }
}

the default proguard-consumer-rules.pro explains it:

The Android Gradle plugin allows to define ProGuard rules which get embedded in the AAR.

These ProGuard rules are automatically applied when a consumer app sets minifyEnabled to true.

The custom rule file must be defined using the 'consumerProguardFiles' property in your build.gradle file.

When a library is not being processed itself, it still can bring it's ProGuard rules - in order to be consumed, when building the whole project.

Community
  • 1
  • 1
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

I am not getting what you actually want to do, but I can suggest you one thing that is just don't do assembleRelease instead import in your another project, clean project and build then use the generated aar from the /build/outputs/aar/ directory. And must check you are not using reference of obfuscated class anywhere.

Nilay Dani
  • 896
  • 6
  • 24