新しいノートブック

Android大好き

ADT(Eclipse)プロジェクトをAndroid Studioに移行した際の'duplicate entry'

ビルドエラー

Eclipseでexportしたものをimportしてビルドしようとするとこういうやつが出ます

Error:Execution failed for task ‘:app:transformClassesWithJarMergingForDebug'. > com.android.build.transform.api.TransformException: java.util.zip.ZipException: duplicate entry: android/support/annotation/AnimRes.class

原因

クラスの競合が発生。

ほとんどの場合依存モジュールのjarが悪さしてます(support-v4とか)。 gradle的に複数の同じjarを管理するのはダメらしい。

第51章 依存関係の管理 http://gradle.monochromeroad.com/docs/userguide/dependency_management.html

依存関係を確認する

右のタブ「Gradle」から対象のモジュールを選択. Tasks > android > androidDependencies 「Run ...」を選択 下のコンソールにBuildVariants毎の依存関係が表示される

もしくは

terminalに下記をうちこむ

./gradlew androidDependencies

ライブラリの依存関係が表示されるので、重複してるものを見つける

対策

build.gradleから適所excludeするなり, 物理的に削除するなりする。

dependencies {
    compile(project(':hoge')) {
        exclude module: 'support-v4'
    }
    compile('hoge') {
        execlude module: 'support-v4'
    }
}

備考

ADTモジュールをインポートした場合、jarファイルは自動的にgradleの依存関係に置き換えられるみたい。ただし、最新のバージョンを使用するようです。

インポート時に下記のような警告が出る。

the adt project importer can identify some .jar files and even whole source copies libraries, and replace them with gradle dependencies. However, it cannot figure out which exact version of the library to use, so it will use the latest. if your project needs to be adjusted to compile with the latest library, you can either import the project again and disable the following options, or better yet, update your project.

参考URL