简体   繁体   中英

Is it possible to Proguard an Android library module, but not the application module?

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 (ie 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!

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, eg:

-keep,allowshrinking class com.example.androidsupportlibproject.YourClass {
    public <methods>;
    protected <methods>;
    public static <methods>;
    public <init>(...);
}

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.

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.

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