我正在尝试在使用android库项目的android应用程序中使用Proguard。 我的核心android项目依赖于两个android库项目。核心和库项目都依赖于android支持库(在我的情况下是:android-support-v4.jar)。 当我尝试使用ProGuard混淆它时,我总是会得到以下错误(如果在Gradle脚本中禁用了Proguarg,则编译正常运行):
: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]))
我的核心项目Gradle构建脚本:
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')
}
其中一个库项目的Gradle构建脚本:
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']
}
}
}
相关ProGuard配置如下:
-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*
Gradle
,请将这些依赖项更改为Maven依赖项。然后使用android.packagingOptions
来排除导致问题的文件。 - Jared Burrows