Firebase引用版本沖突解決:Android dependency 'com.google.android.gms:play-services-basement' has different...

  • 前兩天在RN項(xiàng)目中集成原生的firebase,之后報(bào)錯(cuò)插件版本沖突悍手,報(bào)錯(cuò)信息如下:

What went wrong:
Execution failed for task ':app:preDebugBuild'.
Android dependency 'com.google.android.gms:play-services-basement' has different version for the compile (16.0.1) and runtime (16.1.0) classpath. You should manually set the same version via DependencyResolution
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org

嘗試

  • 這個(gè)問題可是折磨了我大半天逢慌,按照網(wǎng)上搜索的解決方式各種嘗試

1. 方法一:

  • 在項(xiàng)目的build.gradle中加入
allprojects {
    subprojects {
            project.configurations.all {
                resolutionStrategy.eachDependency { details ->
                    if (details.requested.group == 'com.android.support'
                            && !details.requested.name.contains('multidex') ) {
                        details.useVersion "16.1.0"
                    }
                }
            }
        }
}

2. 方法二:

classpath 'com.google.gms:google-services:4.0.2' // Just updated the version here.

3. 方法三:

  • 各個(gè)module的dependencies里的compile改為implementation
    然而悠轩,并沒有什么卵用

解決方法

  • 之后靜下心來好好思考了下,依賴版本沖突攻泼,肯定是存在重復(fù)依賴的問題火架。從這個(gè)思路著手:

第一步:

第二步:

  • 去除依賴沖突 參考https://blog.csdn.net/yyo201/article/details/80570741
    在Teminal 里面輸入 gradlew app:dependencies 回車之后等一會(huì)兒就可以查看到整個(gè)項(xiàng)目的依賴樹結(jié)構(gòu)了何鸡。

    image.png

    image.png

  • 在依賴樹結(jié)構(gòu)里搜索出現(xiàn)版本沖突的module,即play-services-basement只搁,
    上面應(yīng)該可以看出來 271行到273行firebase-core引用的play-services-basement版本是16.0.1音比,
    432行-436行看到react-native-device-info引用的play-services-basement版本變成了16.1.0,
    才導(dǎo)致了play-services-basement引用版本沖突氢惋。

  • 那我們?nèi)コ貜?fù)引用play-services-basement不就可以了么洞翩。
    在引用firebase-core的moudle的build.gradle文件中exclude 掉play-services-basement,

implementation ('com.google.firebase:firebase-core:16.0.6') { // 所加的第三方框架
        exclude group: 'com.google.android.gms',module: 'play-services-basement'     // 加載時(shí)排除框架中的design包
    }
  • 現(xiàn)在編譯焰望,就能成功通過了骚亿。

其他可行方法

版本沖突

在同一個(gè)配置下(例如androidTestCompile),某個(gè)模塊的不同版本同時(shí)被依賴時(shí)熊赖,默認(rèn)使用最新版来屠,gradle同步時(shí)不會(huì)報(bào)錯(cuò)。例如下面的hamcrest-core和runner震鹉。

dependencies {
   androidTestCompile('com.android.support.test:runner:0.4')
   androidTestCompile('com.android.support.test:rules:0.2')
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
+--- com.android.support.test:runner:0.4
|    +--- com.android.support:support-annotations:23.0.1
|    +--- junit:junit:4.12
|    |    \--- org.hamcrest:hamcrest-core:1.3
|    \--- com.android.support.test:exposed-instrumentation-api-publish:0.4
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 -> 0.4 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
     +--- com.android.support.test:rules:0.2 (*)
     +--- com.squareup:javawriter:2.1.1
     +--- org.hamcrest:hamcrest-integration:1.1
     |    \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     +--- com.android.support.test.espresso:espresso-idling-resource:2.1
     +--- org.hamcrest:hamcrest-library:1.1
     |    \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     +--- javax.inject:javax.inject:1
     +--- com.google.code.findbugs:jsr305:2.0.1
     +--- com.android.support.test:runner:0.2 -> 0.4 (*)
     +--- javax.annotation:javax.annotation-api:1.2
     \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
1俱笛、Force

force強(qiáng)制設(shè)置某個(gè)模塊的版本。

 configurations.all {
       resolutionStrategy {
           force 'org.hamcrest:hamcrest-core:1.3'
       }
    }
    dependencies {
       androidTestCompile('com.android.support.test:runner:0.2')
       androidTestCompile('com.android.support.test:rules:0.2')
       androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
    }

可以看到传趾,原本對(duì)hamcrest-core 1.1的依賴迎膜,全部變成了1.3。

+--- com.android.support.test:runner:0.2
|    +--- junit:junit-dep:4.10
|    |    \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
     +--- com.android.support.test:rules:0.2 (*)
     +--- com.squareup:javawriter:2.1.1
     +--- org.hamcrest:hamcrest-integration:1.1
     |    \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     +--- com.android.support.test.espresso:espresso-idling-resource:2.1
     +--- org.hamcrest:hamcrest-library:1.1
     |    \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     +--- javax.inject:javax.inject:1
     +--- com.google.code.findbugs:jsr305:2.0.1
     +--- com.android.support.test:runner:0.2 (*)
     +--- javax.annotation:javax.annotation-api:1.2
     \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
2浆兰、Exclude

Exclude可以設(shè)置不編譯指定的模塊

configurations {
   all*.exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
dependencies {
   androidTestCompile('com.android.support.test:runner:0.2')
   androidTestCompile('com.android.support.test:rules:0.2')
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}

+--- com.android.support.test:runner:0.2
|    +--- junit:junit-dep:4.10
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
     +--- com.android.support.test:rules:0.2 (*)
     +--- com.squareup:javawriter:2.1.1
     +--- org.hamcrest:hamcrest-integration:1.1
     +--- com.android.support.test.espresso:espresso-idling-resource:2.1
     +--- org.hamcrest:hamcrest-library:1.1
     +--- javax.inject:javax.inject:1
     +--- com.google.code.findbugs:jsr305:2.0.1
     +--- com.android.support.test:runner:0.2 (*)
     \--- javax.annotation:javax.annotation-api:1.2
3磕仅、單獨(dú)使用group或module參數(shù)

exclude后的參數(shù)有g(shù)roup和module,可以分別單獨(dú)使用簸呈,會(huì)排除所有匹配項(xiàng)榕订。例如下面的腳本匹配了所有的group為’com.android.support.test’的模塊。

configurations {
   all*.exclude group: 'com.android.support.test'
}
dependencies {
   androidTestCompile('com.android.support.test:runner:0.2')
   androidTestCompile('com.android.support.test:rules:0.2')
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}

  -- com.android.support.test.espresso:espresso-core:2.1
         +--- com.squareup:javawriter:2.1.1
         +--- org.hamcrest:hamcrest-integration:1.1
         |    \--- org.hamcrest:hamcrest-core:1.1
         +--- com.android.support.test.espresso:espresso-idling-resource:2.1
         +--- org.hamcrest:hamcrest-library:1.1
         |    \--- org.hamcrest:hamcrest-core:1.1
         +--- javax.inject:javax.inject:1
         +--- com.google.code.findbugs:jsr305:2.0.1
         +--- javax.annotation:javax.annotation-api:1.2
         \--- org.hamcrest:hamcrest-core:1.1

單獨(dú)給某個(gè)模塊指定exclude

dependencies {
   androidTestCompile('com.android.support.test:runner:0.2')
   androidTestCompile('com.android.support.test:rules:0.2')
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
       exclude group: 'org.hamcrest'
   }
}
+--- com.android.support.test:runner:0.2
|    +--- junit:junit-dep:4.10
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
     +--- com.android.support.test:rules:0.2 (*)
     +--- com.squareup:javawriter:2.1.1
     +--- com.android.support.test.espresso:espresso-idling-resource:2.1
     +--- javax.inject:javax.inject:1
     +--- com.google.code.findbugs:jsr305:2.0.1
     +--- com.android.support.test:runner:0.2 (*)
     \--- javax.annotation:javax.annotation-api:1.2

不同配置下的版本沖突
同樣的配置下的版本沖突蜕便,會(huì)自動(dòng)使用最新版劫恒;而不同配置下的版本沖突,gradle同步時(shí)會(huì)直接報(bào)錯(cuò)玩裙〖婷常可使用exclude段直、force解決沖突。

例如compile 'com.android.support:appcompat-v7:23.1.1'溶诞,和androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'鸯檬,所依賴的com.android.support:support-annotations版本不同,就會(huì)導(dǎo)致沖突螺垢。

dependencies {
   compile 'com.android.support:appcompat-v7:23.1.1'
   androidTestCompile('com.android.support.test:runner:0.2')
   androidTestCompile('com.android.support.test:rules:0.2')
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}

gradle同步時(shí)會(huì)提示

Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.

執(zhí)行dependencies會(huì)提示

FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
4喧务、不兼容

雖然可以通過force、exclude等方式避免依賴項(xiàng)版本沖突枉圃,使得grade同步成功功茴,但是并不能代表編譯時(shí)沒有問題。由于不同版本可能不完全兼容孽亲,于是會(huì)出現(xiàn)各種奇怪的報(bào)錯(cuò)坎穿。已知的解決思路是更改包的版本、嘗試強(qiáng)制使用不同版本的依賴項(xiàng)返劲,找到可兼容的依賴組合玲昧。

參考:http://www.paincker.com/gradle-dependencies

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市篮绿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尘应,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件官辈,死亡現(xiàn)場(chǎng)離奇詭異拳亿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鹅经,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門贷痪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人舱沧,你說我怎么就攤上這事∏K拢” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵典阵,是天一觀的道長嫉鲸。 經(jīng)常有香客問我,道長藤树,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任品嚣,我火速辦了婚禮翰撑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘册养。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布芬为。 她就那樣靜靜地躺著,像睡著了一般孙乖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上份氧,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天唯袄,我揣著相機(jī)與錄音,去河邊找鬼蜗帜。 笑死越妈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的钮糖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼店归!你這毒婦竟也來了阎抒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤消痛,失蹤者是張志新(化名)和其女友劉穎且叁,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秩伞,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡逞带,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了纱新。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片展氓。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖脸爱,靈堂內(nèi)的尸體忽然破棺而出遇汞,到底是詐尸還是另有隱情,我是刑警寧澤簿废,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布空入,位于F島的核電站,受9級(jí)特大地震影響族檬,放射性物質(zhì)發(fā)生泄漏歪赢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一单料、第九天 我趴在偏房一處隱蔽的房頂上張望埋凯。 院中可真熱鬧,春花似錦看尼、人聲如沸递鹉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽躏结。三九已至,卻和暖如春狰域,著一層夾襖步出監(jiān)牢的瞬間媳拴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來泰國打工兆览, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留屈溉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓抬探,卻偏偏與公主長得像子巾,于是被迫代替她去往敵國和親帆赢。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355