現象
Android Studioでproguard付きでビルドした時に、以下のようなエラーが出ることがあります。
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:proguardDebug'.
> java.io.IOException: Can't write [/Users/tomoaki/Workspace/temp/someproject/app/build/intermediates/classes-proguard/debug/classes.jar] (Can't read [/Users/tomoaki/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-io/1.3.2/b6dde38349ba9bb5e6ea6320531eae969985dae5/commons-io-1.3.2.jar(;;;;;;!META-INF/MANIFEST.MF)] (Duplicate zip entry [commons-io-1.3.2.jar:org/apache/commons/io/CopyUtils.class]))
build.gradleはこんな感じ
build.gradle
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
debuggable true
applicationIdSuffix = '.alpha'
versionNameSuffix = 'a'
}
}
dependencies {
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.2'
compile project(':modules:Aviary-SDK')
compile project(':modules:facebook')
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.google.android.gms:play-services:6.1.11'
compile 'org.apache.httpcomponents:httpcore:4.3.2'
compile 'org.apache.httpcomponents:httpmime:4.3.5'
}
原因
dependenciesで設定したライブラリが内部的に同じライブラリ(ここではcommons-io)を呼び出していることが問題なようです。
調査方法と修正
dependenciesの依存関係を調べるためには以下のコマンドを実行します。
$./gradlew :[modules]:dependencies
[modules]にはモジュール名(app
とか)をいれます。
実行すると、依存関係の一覧が見られます
compile - Classpath for compiling the main sources.
+--- com.android.support:support-v4:21.0.0 -> 21.0.2
| \--- com.android.support:support-annotations:21.0.2
+--- com.android.support:appcompat-v7:21.0.2
| \--- com.android.support:support-v4:21.0.2 (*)
+--- project :modules:Aviary-SDK
| \--- com.aviary.android.feather.sdk:sdk-library:+ -> 3.5.1
| +--- it.sephiroth.android.library.hlistviewanimations:hlistviewanimations-library:1.0.1
| | +--- it.sephiroth.android.library.horizontallistview:hlistview:1.2.2
| | | \--- com.android.support:support-v4:19.1.+ -> 21.0.2 (*)
| | \--- com.nineoldandroids:library:2.4.0
| +--- com.nineoldandroids:library:2.4.0
| +--- it.sephiroth.android.library.disklruimagecache:DiskLruImageCache:1.0.2
| | +--- org.apache.commons:commons-io:1.3.2
| | | \--- commons-io:commons-io:1.3.2
| | \--- com.jakewharton:disklrucache:2.0.2
(中略)
| +--- com.android.support:support-v4:19.+ -> 21.0.2 (*)
| +--- it.sephiroth.android.exif:android-exif-extended:1.0.2
| | +--- org.apache.commons:commons-lang3:3.3.2
| | \--- org.apache.commons:commons-io:1.3.2 (*)
(以下略)
ログを見ると、support-v4とAviaryでcommons-ioが呼ばれているようです。
Aviaryの方でcommons-ioをexcludeしてあげます。
build.gradle
compile (project(':modules:Aviary-SDK')){
exclude group: 'commons-io', module: 'commons-io'
}
これでproguard付きビルドが通るようになります。