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

I hope someone can answer what I consider the main Gradle / Proguard question.

I have a very simple Android project. This project contains a main application module called app and a library module for Android support libraries called AndroidSupport .

I want to run Proguard exclusively on AndroidSupport (i.e. NOT in the general application), because I have problems with testing the hardware in the application when it is Proguard-ed. I hope that I can minimize AndroidSupport myself, so that I don’t need Proguard my application code (and thus avoid problems running the tests).

Here is my app build.gradle . Please 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 this:

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

If the app has Proguard enabled and AndroidSupport disabled by Proguard, I can use consumerProguardFiles proguard-rules.pro to minimize AndroidSupport .
But when I use the above 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 is possible? Enable Proguard ONLY on the dependent library module, but not on the application itself?

Thanks in advance!

+4
source share
3 answers

In Android Studio, you can skip only the library module, not the hole project. I am creating android inapp sdk. There is a lib and app module in my project, I have successfully obfuscated only the lib module.

Some tips: Add the code below to the lib buildue file. Lib.exe

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

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

+1
source

Don't you want some interfaces to display the API of your library module? To access from your application module? In this case, you must add the -keep options to your ProGuard library, for example:

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

It is not possible to run Proguard exclusively on a module, and not on a shared application. Whenever I try to do this, Proguard seems to strip all the code from my APK, because it probably cannot resolve the dependencies between testing apk and verified apk.

The solution is to run Proguard throughout the application and figure out how to perform unit tests. Note that the testProguardFile in the Proguard configuration refers to the configuration for testing apk, and not to the verified apk.

0
source

All Articles