com.google.android.gms:play-services-base 在 react native 中的编译和运行时冲突

com.google.android.gms:play-services-base compile and runtime collision in react native

我的 React Native 项目有问题。当我尝试构建项目时(在 android、windows 10、android 模拟器中),我遇到了有关编译时间版本和 运行 播放时间版本之间冲突的错误-服务基础。

我正在使用:

"react": "16.3.1"
"react-native": "0.55.3"
"react-native-firebase": "^4.3.8"

android/build.gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven {
               url 'https://maven.fabric.io/public'
            }
        maven {
            url 'https://google.bintray.com/exoplayer/'
        }
        google()
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath 'com.google.gms:google-services:4.0.1'
        classpath 'io.fabric.tools:gradle:1.25.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            url 'https://jitpack.io'
        }
        maven {
            url 'https://google.bintray.com/exoplayer/'
        }
        maven {
            url 'https://maven.google.com'
        }
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        jcenter()
        configurations.all {
                        resolutionStrategy.force 'com.google.android.gms:play-services-base:17.0.0'
                    }
    }
}

subprojects {

project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.google.android.gms'
                    && (details.requested.name.contains('play-services-core')
                    || details.requested.name.contains('play-services-tasks')
                    || details.requested.name.contains('play-services-basement')
                    || details.requested.name.contains('play-services-stats'))) {
                details.useVersion "17.0.0"
            }
        }
        afterEvaluate {project ->
    if (project.name.contains('react-native-fetch-blob') ||
            project.name.contains('react-native-image-picker') ||
            project.name.contains('react-native-vector-icons')) {
        buildscript {
            repositories {
                maven { url "https://dl.bintray.com/android/android-tools/"  }
            }
        }
    }
}
}
}

android/app/build.gradle

    apply plugin: "com.android.application"
apply plugin: 'io.fabric'

import com.android.build.OutputFile

project.ext.react = [
        entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = true
def Properties localproperties = new Properties()
localproperties.load(project.rootProject.file("local.properties").newDataInputStream())

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        applicationId "ir.brogrammers.tamin"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 32
        versionName "1.0.8"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        multiDexEnabled true
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    /* signingConfigs {
        release {
                if (localproperties.getProperty('MYAPP_RELEASE_STORE_FILE', null)) {
                storeFile file(localproperties['MYAPP_RELEASE_STORE_FILE'])
                storePassword localproperties['MYAPP_RELEASE_STORE_PASSWORD']
                keyAlias localproperties['MYAPP_RELEASE_KEY_ALIAS']
                keyPassword localproperties['MYAPP_RELEASE_KEY_PASSWORD']
            }
        }
    } */
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
            ext.enableCrashlytics = true
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation project(':react-native-firebase')

    // Firebase dependencies
    implementation "com.google.android.gms:play-services-base:17.0.0"
    implementation "com.google.firebase:firebase-core:17.0.0"
    implementation "com.google.firebase:firebase-messaging:19.0.0"
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:27.1.0"
    implementation("com.facebook.react:react-native:0.55.3") { force = true }
    implementation project(':react-native-document-picker')
    implementation project(':react-native-image-picker')
    implementation project(':react-native-fs')
    implementation project(':react-native-device-info')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-video')
    implementation project(':react-native-file-viewer')
    implementation project(':react-native-splash-screen')
    implementation 'com.facebook.fresco:animated-base-support:1.3.0'
    implementation 'com.facebook.fresco:animated-gif:1.3.0'
    implementation 'com.facebook.fresco:fresco:1.3.0'
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.+'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.1'
    implementation project(':react-native-fetch-blob')
    implementation project(':react-native-account-manager')
    compile project(':react-native-shortcut-badge')
    compile project(':react-native-wheel-picker-android')
    implementation 'com.android.support:multidex:1.0.3'
    implementation project(':react-native-i18n')

//    configurations.all {
//        resolutionStrategy {
//            force 'com.google.android.gms:play-services-gcm:17.0.0'
//            force 'com.google.android.gms:play-services-base:17.0.0'
//            force 'com.google.firebase:firebase-core:17.0.0'
//            force 'com.google.firebase:firebase-messaging:19.0.0'
//        }
//    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

当我想用 react-native 运行-android 构建项目时,它失败并出现此错误

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:preDebugBuild'.
> Android dependency 'com.google.android.gms:play-services-base' has different version for the compile (16.0.1) and runtime (17.0.0) classpath. You should manually set the same version via DependencyResolution

我尝试将 play-services-base 版本更改为 17.0.0 并强制其使用版本 17.0.0

    resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.google.android.gms'
                && (details.requested.name.contains('play-services-core')
                || details.requested.name.contains('play-services-tasks')
                || details.requested.name.contains('play-services-basement')
                || details.requested.name.contains('play-services-stats'))) {
            details.useVersion "17.0.0"
        }
    }

github 上的这条评论提供了您问题的答案

一个依赖项(在本例中为 react-native-device-info)正在使用其依赖项之一的最新版本,而不是 fixed/pinned 版本。昨天发布新版本的 google 服务时,导致构建为 device-info 引入新版本,从而导致与正确固定所需版本的其他依赖项发生冲突。

implementation(project(":react-native-device-info"),  {
  exclude group: "com.google.android.gms"
})

实施"com.google.android.gms:play-services-gcm:16.0.0" 并可能将 react-native-device-info 替换为可能具有相同问题的任何其他依赖项(它们将包含类似 implementation "com.google.android.gms:play-services-gcm:+" 的行,这取决于 google gcm 的最新版本)。