I am trying to use Proguard in an android application which uses android library projects. My core android project depends on two android library projects. The core and the library projects have dependencies on android support library (in my case: android-support-v4.jar). When I try to obfuscate it with ProGuard, then I always get the following error (If the Proguarg is disabled in the Gradle script, then the compilation works fine.):
:MYANDROIDPROJECT:proguardDemoRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':MYANDROIDPROJECT:proguardDemoRelease'.
> java.io.IOException: Can't write [MYANDROIDPROJECT_PATH\build\intermediates\classes-proguard\demo\release\classes.jar] (Can't read [MYANDROIDPROJECT_PATH\build\intermediates\exploded-aar\com.android.support\support-v4\21.0.3\classes.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [classes.jar:android/support/v4/graphics/drawable/DrawableCompat$HoneycombDrawableImpl.class]))
The Gradle build script for my core project:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2+'
}
}
apply plugin: 'android'
android {
buildToolsVersion "19.1.0"
compileSdkVersion 19
lintOptions {
abortOnError false
}
signingConfigs {
localks {
storeFile file("keystore/keystore")
storePassword "xx"
keyAlias "xx"
keyPassword "xx"
}
}
buildTypes {
debug {
debuggable true
runProguard false
zipAlign true
signingConfig signingConfigs.localks
}
release {
debuggable false
zipAlign true
proguardFile 'proguard.txt'
runProguard true
signingConfig signingConfigs.localks
}
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
ndk{
moduleName "S3DClient"
abiFilter "armeabi-v7a"
stl "gnustl_static"
}
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
jni.srcDirs = []
}
paid {
manifest.srcFile 'src/paid/AndroidManifest.xml'
java.srcDirs = ['src/paid/java']
res.srcDirs = ['src/paid/res']
jni.srcDirs = []
}
demo {
manifest.srcFile 'src/demo/AndroidManifest.xml'
java.srcDirs = ['src/demo/java']
res.srcDirs = ['src/demo/res']
jni.srcDirs = []
}
}
productFlavors {
paid {
applicationId = "hu.myprojectpackage1"
}
demo {
applicationId = "hu.myprojectpackage2"
}
}
}
dependencies {
compile files('src/main/libs/sqliteassethelper-2.0.1.jar',
'src/main/libs/S3DXAndroidTools.jar',
'src/main/libs/roboguice-2.0.jar',
'src/main/libs/open-javageom.jar',
'src/main/libs/openawt.geom.jar',
'src/main/libs/libGoogleAnalyticsServices.jar',
'src/main/libs/javax.inject-1.jar',
'src/main/libs/guice-3.0-no_aop.jar',
'src/main/libs/commons-lang3-3.1.jar',
'src/main/libs/bugsense-3.6.jar')
compile fileTree(dir: "$buildDir/native-libs", include: '*.jar')
compile 'com.android.support:support-v4:21.0.+'
compile project(":lib/SlidingLayer")
compile project(':lib/BlutoothCommunicationHandler')
paidCompile project(':lib/Licencing')
}
The Gradle build script for one of the library projects:
apply plugin: 'android-library'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2+'
}
}
dependencies {
compile "com.android.support:support-v4:19.0.+"
}
android {
buildToolsVersion "19.1.0"
compileSdkVersion 19
defaultConfig {
minSdkVersion 4
targetSdkVersion 18
}
dexOptions {
preDexLibraries = false
}
lintOptions {
abortOnError false
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
The (relevant part of) ProGuard config is:
-libraryjars src/main/libs/android-support-v4.jar
-keep class android.** { *; }
-keep class android.support** { *; }
-dontnote
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*
-dontwarn org.openawt.**
-dontwarn android.support.**
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-assumenosideeffects class android.util.Log {
public static *** e(...);
public static *** w(...);
public static *** wtf(...);
public static *** d(...);
public static *** v(...);
}
-keepattributes Signature
-keepattributes *Annotation*
Main Project
build.gradle
:Take some time to convert these local jars:
To Maven Dependencies that can be managed easily(I tried to find most of them for you!):
After that, you should be able to use
android.packagingOptions
:Library Project
build.gradle
Your library project needs to be changed from:
to:
Your main issue was that you had two different
com.android.support:support-v4
, one in your library project and one in your main project.