如何用 gradle 解决所有 google play-services 依赖?

How to resolve all google play-services dependencies with gradle?

情况

今天,我的gradle 对我大吼大叫,不编译项目。它无法解析 firebase-iidfirebase-commonplay-services-analytics-implplay-services-auth-api-phoneplay-services-auth-base。周末过后真是一团糟。

所以我检查了不同API的版本,升级了需要升级的,发现Google给[=20=添加了强制依赖,添加了它,然后升级了我的google-services 插件,甚至添加了 play-services-base 依赖项。我还将 firebase-messaging 升级到 17.0.0 版,将 crashlytics 升级到 2.9.3 版。

但是,构建仍然无法编译,出现以下错误消息:

The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[15.0.2,15.0.2], [15.0.4,15.0.4]], but resolves to 15.0.4. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.

Gradle 文件

这是我尝试修复后的 Gradle 文件(仅显示依赖项部分):

apply plugin: 'com.android.application'

...
dependencies {
    implementation project(':library')
    implementation project(':otherlibrary')
    implementation 'com.android.volley:volley:1.1.0'
    implementation('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
    implementation 'com.google.android.gms:play-services-base:15.0.1'
    implementation 'com.google.android.gms:play-services-analytics:15.0.2'
    implementation 'com.google.android.gms:play-services-location:15.0.1'
    implementation('com.google.android.gms:play-services-ads:15.0.1') {
        exclude group: 'com.android.support', module: 'customtabs'
    }
    implementation 'com.google.android.gms:play-services-identity:15.0.1'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'
    implementation 'com.google.android.gms:play-services-drive:15.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.0'
    implementation 'com.google.firebase:firebase-messaging:17.0.0'

    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.nineoldandroids:library:2.4.0'
    implementation 'com.daimajia.slider:library:1.1.5@aar'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:mediarouter-v7:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:exifinterface:27.1.1'
    implementation 'com.android.support:support-v13:27.1.1'
    // https://mvnrepository.com/artifact/com.darwinsys/hirondelle-date4j
    implementation group: 'com.darwinsys', name: 'hirondelle-date4j', version: '1.5.1'
    // For RxAndroid and RxJava
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version)
    implementation 'io.reactivex.rxjava2:rxjava:2.1.12'

    // Crashlytics
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'

    // CSV with outputstream writer
    implementation 'com.opencsv:opencsv:4.1'

    // Gson
    implementation 'com.google.code.gson:gson:2.8.5'

    debugImplementation 'com.android.support.test:runner:1.0.2'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
    androidTestImplementation 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

我的项目范围 gradle 包含这些行:

...
repositories {
    maven {
       url 'https://maven.google.com/'
       name 'Google'
    }
    ...
}
dependencies {
    ...
    classpath 'com.google.gms:google-services:4.0.1'
}
...

官方解决方案无效

我已经尝试根据official firebase documentation, which is more or less the same as 修复多个库的版本号。
正如你在官方文档link中看到的那样,截至2018年5月2日,他们提供了一种解决此类问题的方法,但问题的版本号不一样,我已经尝试过给出了修复(现在有一些降级),但无济于事。

Official documentation: If your app has a dependency on com.google.firebase:firebase-core:15.0.0 as well as any of the following libraries:

com.google.android.gms:play-services-analytics
com.google.android.gms:play-services-appinvite
com.google.android.gms:play-services-tagmanager
com.google.firebase:firebase-analytics
com.google.firebase:firebase-crash
com.google.firebase:firebase-dynamic-links
com.google.firebase:firebase-messaging

You will need to update the version of the latter dependency to 15.0.2. This addresses the issue where version 3.3.0 of the Google Services Gradle plugin reports: The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[15.0.0,15.0.0], [15.0.2,15.0.2]], but resolves to 15.0.2...

那么如何解决这些版本冲突,以便我的构建再次正确编译?

我尝试恢复到旧版本,它似乎有效,我发现 gradle-wrapper.properties 使用的是旧版本 (4.5) 而不是我在较新项目中使用的 4.6。更改它以及从 alpha15 恢复到 classpath 'com.android.tools.build:gradle:3.2.0-alpha08' 似乎可以解决问题。我认为您可以根据需要使用 alpha 15,但请确保您可以使用 classpath 'com.android.tools.build:gradle:3.1.2'。 除了所有这些更改外,firebase-coregoogle-gms-services 应设置为版本 16.0.0.

所以在这个问题上迷茫了一天之后,我发现 play-services-analytics 的最新版本 而不是 他们在 their website 中发布的版本.它是 16.0.0 而不是 15.0.2

Android Studio 将此作为警告告诉同事,但我的同事对此并不关心。所以在这里,改变一切的行:

implementation 'com.google.android.gms:play-services-analytics:16.0.0'

很遗憾google播放服务已停止 此寡妇消息是生成的并且从未 运行 任何 google 服务执行

我将此添加到我的 build.gradle(应用程序版本)并且有效:

googleServices { disableVersionCheck = true }

(请记住,这只是为了禁用版本检查,它实际上并没有修复任何问题)