LoginSignup
4
3

More than 5 years have passed since last update.

Data binding ライブラリを有効にしたときにビルドに失敗することがある

Posted at

Android Studio で新規プロジェクトを作って Kotlin を有効にして Data Binding ライブラリも有効にしたところ、ビルドがこけるようになった。

~\Documents\projects\android\playground > .\gradlew build

> Configure project :app

> Task :app:compileDebugKotlin
Using Kotlin incremental compilation


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
22 actionable tasks: 9 executed, 13 up-to-date

↑ のエラーメッセージだとよくわかんなかったけど、multidex を有効にしたら以下のような失敗の仕方に変化。

~\Documents\projects\android\playground > .\gradlew build

> Configure project :app
app: 'androidProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.android.databinding:compiler:3.0.0' and apply the kapt plugin: "apply plugin: 'kotlin-kapt'".


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> java.io.IOException: Can't write [C:\Users\nobuoka\Documents\projects\android\playground\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can't read [C:\Users\nobuoka\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.0.0.aar\0730346c91683ff4bcc7167ddb128661\jars\classes.jar(;;;;;;**.class)] (Duplicate zip entry [classes.jar:android/support/v4/view/ViewPager$1.class]))

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
22 actionable tasks: 1 executed, 21 up-to-dat

これを見るとどうやら別の依存先ライブラリに完全名が同じクラスが含まれている様子。

ということで依存関係を見てみる。

~\Documents\projects\android\playground > .\gradlew :app:dependencies

> Configure project :app

> Task :app:dependencies

------------------------------------------------------------
Project :app
------------------------------------------------------------

(略)

debugCompileClasspath - Resolved configuration for compilation for variant: debug
+--- com.android.databinding:library:1.3.1
|    +--- com.android.support:support-v4:21.0.3
|    |    \--- com.android.support:support-annotations:21.0.3 -> 27.0.0
|    \--- com.android.databinding:baseLibrary:2.3.0-dev -> 3.0.0
+--- com.android.databinding:baseLibrary:3.0.0
+--- com.android.databinding:adapters:1.3.1
|    +--- com.android.databinding:library:1.3 -> 1.3.1 (*)
|    \--- com.android.databinding:baseLibrary:2.3.0-dev -> 3.0.0
+--- org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.51
|    \--- org.jetbrains.kotlin:kotlin-stdlib:1.1.51
|         \--- org.jetbrains:annotations:13.0
+--- com.android.support:appcompat-v7:27.0.0
|    +--- com.android.support:support-annotations:27.0.0
|    +--- com.android.support:support-core-utils:27.0.0
|    |    +--- com.android.support:support-annotations:27.0.0
|    |    \--- com.android.support:support-compat:27.0.0
|    |         +--- com.android.support:support-annotations:27.0.0
|    |         \--- android.arch.lifecycle:runtime:1.0.0
|    |              +--- android.arch.lifecycle:common:1.0.0
|    |              \--- android.arch.core:common:1.0.0
|    +--- com.android.support:support-fragment:27.0.0
|    |    +--- com.android.support:support-compat:27.0.0 (*)
|    |    +--- com.android.support:support-core-ui:27.0.0
|    |    |    +--- com.android.support:support-annotations:27.0.0
|    |    |    \--- com.android.support:support-compat:27.0.0 (*)
|    |    +--- com.android.support:support-core-utils:27.0.0 (*)
|    |    \--- com.android.support:support-annotations:27.0.0
|    +--- com.android.support:support-vector-drawable:27.0.0
|    |    +--- com.android.support:support-annotations:27.0.0
|    |    \--- com.android.support:support-compat:27.0.0 (*)
|    \--- com.android.support:animated-vector-drawable:27.0.0
|         +--- com.android.support:support-vector-drawable:27.0.0 (*)
|         \--- com.android.support:support-core-ui:27.0.0 (*)
\--- com.android.support.constraint:constraint-layout:1.0.2
     \--- com.android.support.constraint:constraint-layout-solver:1.0.2

(略)

プロジェクト全体としては Support Library 27.0.0 を使うようにしているが、com.android.databinding:library:1.3.1 の依存先が com.android.support:support-v4:21.0.3 になっていて、ここだけ古いものが使われてしまっていた。 多分、バージョン 21.0.3 のときには v4 compat ライブラリに ViewPager が含まれていたが、Support Library のモジュール分けが変化して 27.0.0 では別のモジュールに含まれていて、衝突してしまった感じだと思われる。

ちなみに、こういう感じでサポートライブラリのモジュールのバージョンが揃ってない場合には、Android Studio 上で下記のような警告が表示される。

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.0.0, 21.0.3. Examples include com.android.support:animated-vector-drawable:27.0.0 and com.android.support:support-v4:21.0.3 less... (Ctrl+F1)
There are some combinations of libraries, or tools and libraries, that are incompatible, or can lead to bugs. One such incompatibility is compiling with a version of the Android support libraries that is not the latest version (or in particular, a version lower than your targetSdkVersion.)

こういう警告が出たら、依存関係を見て、古いバージョンのサポートライブラリに依存しているものがあればバージョン更新すると良い。 (ただ、古いバージョンのサポートライブラリのモジュールに依存しているライブラリがあったとして、依存先モジュールのバージョンを今回のように書き換えてやると (サポートライブラリのモジュール分けの方針が変わっていた場合などに) クラッシュする可能性もあるので難しいところ。 気を付ける必要がある。)

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3