12

I'm hoping somebody can answer what I think is a basic Gradle/Proguard question.

I have a very basic Android project. This project contains the main app module, named app, and a library module for the Android Support libraries, named AndroidSupport.

I wish to run Proguard exclusively on AndroidSupport (i.e. NOT on the overall application) because I'm having trouble running instrumentation tests on the application when it is Proguard-ed. My hope is that I can minify AndroidSupport on its own so that I don't need to Proguard my application code (and thus, avoid the problem of tests not running).

Here's my app build.gradle. Note that Proguard is DISABLED:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.example.androidsupportlibproject"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            minifyEnabled false //Proguard DISABLED
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':AndroidSupport')
}

My AndroidSupport module has Proguard ENABLED:

apply plugin: 'com.android.library'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 22
    }
    buildTypes {
        release {
            minifyEnabled true  //Proguard ENABLED
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.android.support:support-annotations:22.2.0'
}

My AndroidSupport module proguard-rules.pro looks like:

-dontobfuscate
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }

When the app application has Proguard enabled and AndroidSupport has Proguard disabled, I can use consumerProguardFiles proguard-rules.pro to minify AndroidSupport.
But when I use the above pasted configuration, I get the following error:

Error:Execution failed for task ':AndroidSupport:proguardRelease'.
java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?`

Does anyone know if this set up is possible? To enable Proguard ONLY on a dependent library module, but not on the application itself?

Thanks in advance!

David Choi
  • 131
  • 1
  • 4
  • have a look here http://stackoverflow.com/questions/27277433/why-does-gradle-build-my-module-in-release-mode-when-the-app-is-in-debug – Ultimo_m May 05 '17 at 11:58

3 Answers3

0

Don't you want to keep some of the interfaces - APIs of your library module visible? To access from your application module? In that case, you should add -keep options into your library ProGuard, e.g:

-keep,allowshrinking class com.example.androidsupportlibproject.YourClass {
    public <methods>;
    protected <methods>;
    public static <methods>;
    public <init>(...);
}
peter.bartos
  • 11,855
  • 3
  • 51
  • 62
  • Thanks for your comment. My library module is just a container for the support libraries and doesn't contain additional application code. This is why I'm confused that `-keep class android.support.v4.** { *; } -keep interface android.support.v4.app.** { *; }` doesn't keep any code at all. – David Choi Aug 18 '15 at 14:41
0

It doesn't seem to be possible to run Proguard exclusively on a module and not on the overall application. Whenever I try this, Proguard seems to strip out all code from my APK because it likely can't resolve the dependencies between the testing apk and the tested apk.

The solution is to run Proguard on the entire application and figure out how to get unit tests to run. Note that testProguardFile in the Proguard configuration refers to the config for the testing apk, NOT the tested apk.

David Choi
  • 131
  • 1
  • 4
0

Its is possible to proguard only library module, not the hole project in 'Android Studio' project. I am creating an android inapp sdk. In my project, have lib and app module, i have obfuscated only lib module successfully.

Some hint: Add below code to your lib moduele build.gradle file.

 buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Then in same directory with build.gradle , create, proguard-rules.pro file. Your proguard code will go there.

Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51