react-native-mapsを追加したらandroidでコンパイルエラー

  • react-native run-ios は問題なし
  • react-native run-androidを実行すると実行時にコンパイルエラーが発生
> Task :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED
D8: Program type already present: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
  • 以下のような理由らしい
    • ライブラリが依存しているライブラリがないとか
    • ライブラリが依存しているライブラリが、重複しているとか
    • 古いバージョンのサポートライブラリに依存しているとか
  • とりあえず依存関係を調査

  • android studioを起動してターミナルで./gradlew :app:dependenciesで依存関係のツリーみたいなのが確認できる

  • やっぱりreact-native-mapsが怪しい

 project :react-native-maps
|    +--- com.google.android.gms:play-services-base:10.2.4
|    |    +--- com.google.android.gms:play-services-basement:[10.2.4] -> 10.2.4
|    |    |    \--- com.android.support:support-v4:24.0.0
|    |    |         \--- com.android.support:support-annotations:24.0.0 -> 27.1.1
|    |    \--- com.google.android.gms:play-services-tasks:[10.2.4] -> 10.2.4
|    |         \--- com.google.android.gms:play-services-basement:[10.2.4] -> 10.2.4 (*)
|    +--- com.google.android.gms:play-services-maps:10.2.4
|    |    +--- com.google.android.gms:play-services-base:[10.2.4] -> 10.2.4 (*)
|    |    \--- com.google.android.gms:play-services-basement:[10.2.4] -> 10.2.4 (*)
|    \--- com.google.maps.android:android-maps-utils:0.5+ -> 0.5


  • /react-native-project/android/app/build.gradleを確認してみる。

support-v4がなかったので、追加したらなんか動いた。

  • 24.0.0に依存してるが、こんかい27.1.1のsupport-v4でも大丈夫だった。
  • /react-native-project/android/build.gradle

buildscript { ext { buildToolsVersion = "27.0.3" minSdkVersion = 21 compileSdkVersion = 27 targetSdkVersion = 26 supportLibVersion = "27.1.1" } repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenLocal() jcenter() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } google() } } task wrapper(type: Wrapper) { gradleVersion = '4.4' distributionUrl = distributionUrl.replace("bin", "all") }
  • /react-native-project/android/app/build.gradle
// 省略

dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.android.support:support-v4:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation project(':react-native-maps')
    implementation project(':react-native-vector-icons')
}

// 省略