首页 > 解决方案 > 如何通过 Android Studio/react native 中的 DependencyResolution 手动设置相同的版本(编译和运行时类路径)

问题描述

我在我的项目中添加了一个 npm 库,从那时起,android 版本不再工作。iOS版本运行良好。我添加的库是https://www.npmjs.com/package/react-native-connectivity-status

当我尝试运行 react-native run-android 时,我收到错误消息 > Android 依赖项 'com.google.android.gms:play-services-basement' 的编译(11.8.0)和运行时(15.0. 1) 类路径。您应该通过 DependencyResolution" 手动设置相同的版本。

在应用程序 build.gradle 我添加了:

implementation project(':react-native-connectivity-status') // to check if BT is turned on

这篇文章表明我需要做的就是添加

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
            && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.0.2"
            }
        }
    }
}

到根级别 build.gradle。我已经做到了。我还将所有编译语句都切换为实现。

所以现在我的根级 build.gradle 现在看起来像:

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

buildscript {
    repositories {

        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        // Add jitpack repository (added by react-native-spinkit)
        maven { url "https://jitpack.io" }
        mavenLocal()

        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        jcenter()
    }
    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'com.android.support'
                && !details.requested.name.contains('multidex') ) {
                    details.useVersion "26.0.2"
                }
            }
        }
    }
}

这是我的 build.gradle 中的依赖项列表

dependencies {
    implementation project(':react-native-i18n')
    implementation project(':react-native-svg')
    implementation project(':react-native-extra-dimensions-android')
    implementation project(':react-native-google-analytics-bridge')
    implementation project(':react-native-splash-screen')
    implementation project(':react-native-image-picker')
    implementation project(':react-native-spinkit')
    implementation project(':react-native-fbsdk')
    implementation project(':react-native-picker')
    implementation 'cn.aigestudio.wheelpicker:WheelPicker:1.1.2'
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.facebook.react:react-native:+'
    // From node_modules
    implementation 'com.facebook.fresco:fresco:1.3.0'
    implementation 'com.facebook.fresco:animated-gif:1.3.0'
    implementation project(':blelibrary')
    implementation project(':gaialibrary')
    implementation project(':vmupgradelibrary')
    implementation 'com.google.code.gson:gson:2.8.4'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation project(':react-native-connectivity-status') // to check if BT is turned on
}

和一些附加信息

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId "com.someapp"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 28
        versionName "1.2.4"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        packagingOptions {
            exclude "lib/arm64-v8a/libgnustl_shared.so"
        }
        externalNativeBuild {
            cmake {
                arguments '-DANDROID_STL=c++_static'
            }
        }
    }

我对错误消息感到非常困惑,因为我在 build.gradle 的任何地方都看不到 com.google.android.gms:play-services-basement。

在发布这个问题之前,我已经查看了很多答案: hereherehere

我意识到我没有在默认配置中添加 multiDexEnabled true。添加后,运行 ./gradlew clean 然后 react-native run-android,我得到一个新的错误: > Android dependency 'com.google.android.gms:play-services-tasks-license:11.8.0' is set只编译/提供不支持的

标签: androidreact-nativegradlereact-native-android

解决方案


通过在应用级别的 build.gradle 中添加以下依赖项,我能够构建它(带有新的和改进的错误):

implementation("com.google.android.gms:play-services-basement:15.0.1")
implementation("com.google.android.gms:play-services-base:15.0.1") 

推荐阅读