使用 react-native 添加依赖项时出错

Errors adding dependency with react-native

我正在使用 react-native 创建一个应用程序。到目前为止,我已经实现了一些模块,例如 react-native-camerareact-native-maps.

一切正常,直到我尝试实施 react-native-push-notification

我使用 this tutorial 遵循了他们的实现,但现在,即使应用成功构建,它在启动时崩溃。

这是我的logcat(它实际上被裁剪了,因为它放不下)

11-19 18:29:15.419 29371 29456 I art     : Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.maps.model.Cap>: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/internal/safeparcel/zza;
11-19 18:29:15.419 29371 29456 I art     :   at java.util.List com.airbnb.android.react.maps.MapsPackage.createViewManagers(com.facebook.react.bridge.ReactApplicationContext) (MapsPackage.java:36)
11-19 18:29:15.419 29371 29456 I art     :   at java.util.List com.facebook.react.ReactInstanceManager.getOrCreateViewManagers(com.facebook.react.bridge.ReactApplicationContext) (ReactInstanceManager.java:753)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.uimanager.UIManagerModule com.facebook.react.CoreModulesPackage.createUIManager(com.facebook.react.bridge.ReactApplicationContext) (CoreModulesPackage.java:168)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.uimanager.UIManagerModule com.facebook.react.CoreModulesPackage.access0(com.facebook.react.CoreModulesPackage, com.facebook.react.bridge.ReactApplicationContext) (CoreModulesPackage.java:52)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.NativeModule com.facebook.react.CoreModulesPackage.get() (CoreModulesPackage.java:126)
11-19 18:29:15.419 29371 29456 I art     :   at java.lang.Object com.facebook.react.CoreModulesPackage.get() (CoreModulesPackage.java:123)
11-19 18:29:15.419 29371 29456 I art     :   at void com.facebook.react.NativeModuleRegistryBuilder.processPackage(com.facebook.react.ReactPackage) (NativeModuleRegistryBuilder.java:61)
11-19 18:29:15.419 29371 29456 I art     :   at void com.facebook.react.ReactInstanceManager.processPackage(com.facebook.react.ReactPackage, com.facebook.react.NativeModuleRegistryBuilder) (ReactInstanceManager.java:1171)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.NativeModuleRegistry com.facebook.react.ReactInstanceManager.processPackages(com.facebook.react.bridge.ReactApplicationContext, java.util.List, boolean) (ReactInstanceManager.java:1141)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.ReactApplicationContext com.facebook.react.ReactInstanceManager.createReactContext(com.facebook.react.bridge.JavaScriptExecutor, com.facebook.react.bridge.JSBundleLoader) (ReactInstanceManager.java:1083)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.ReactApplicationContext com.facebook.react.ReactInstanceManager.access0(com.facebook.react.ReactInstanceManager, com.facebook.react.bridge.JavaScriptExecutor, com.facebook.react.bridge.JSBundleLoader) (ReactInstanceManager.java:116)
11-19 18:29:15.419 29371 29456 I art     :   at void com.facebook.react.ReactInstanceManager.run() (ReactInstanceManager.java:913)
11-19 18:29:15.419 29371 29456 I art     :   at void java.lang.Thread.run() (Thread.java:762)
11-19 18:29:15.419 29371 29456 I art     : Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.internal.safeparcel.zza" on path: DexPathList[[zip file "/data/app/com.dpassapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.dpassapp-2/lib/arm, /data/app/com.dpassapp-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
11-19 18:29:15.419 29371 29456 I art     :   at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
11-19 18:29:15.419 29371 29456 I art     :   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
11-19 18:29:15.419 29371 29456 I art     :   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
11-19 18:29:15.419 29371 29456 I art     :   at java.util.List com.airbnb.android.react.maps.MapsPackage.createViewManagers(com.facebook.react.bridge.ReactApplicationContext) (MapsPackage.java:36)
11-19 18:29:15.419 29371 29456 I art     :   at java.util.List com.facebook.react.ReactInstanceManager.getOrCreateViewManagers(com.facebook.react.bridge.ReactApplicationContext) (ReactInstanceManager.java:753)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.uimanager.UIManagerModule com.facebook.react.CoreModulesPackage.createUIManager(com.facebook.react.bridge.ReactApplicationContext) (CoreModulesPackage.java:168)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.uimanager.UIManagerModule com.facebook.react.CoreModulesPackage.access0(com.facebook.react.CoreModulesPackage, com.facebook.react.bridge.ReactApplicationContext) (CoreModulesPackage.java:52)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.NativeModule com.facebook.react.CoreModulesPackage.get() (CoreModulesPackage.java:126)
11-19 18:29:15.419 29371 29456 I art     :   at java.lang.Object com.facebook.react.CoreModulesPackage.get() (CoreModulesPackage.java:123)
11-19 18:29:15.419 29371 29456 I art     :   at void com.facebook.react.NativeModuleRegistryBuilder.processPackage(com.facebook.react.ReactPackage) (NativeModuleRegistryBuilder.java:61)
11-19 18:29:15.419 29371 29456 I art     :   at void com.facebook.react.ReactInstanceManager.processPackage(com.facebook.react.ReactPackage, com.facebook.react.NativeModuleRegistryBuilder) (ReactInstanceManager.java:1171)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.NativeModuleRegistry com.facebook.react.ReactInstanceManager.processPackages(com.facebook.react.bridge.ReactApplicationContext, java.util.List, boolean) (ReactInstanceManager.java:1141)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.ReactApplicationContext com.facebook.react.ReactInstanceManager.createReactContext(com.facebook.react.bridge.JavaScriptExecutor, com.facebook.react.bridge.JSBundleLoader) (ReactInstanceManager.java:1083)
11-19 18:29:15.419 29371 29456 I art     :   at com.facebook.react.bridge.ReactApplicationContext com.facebook.react.ReactInstanceManager.access0(com.facebook.react.ReactInstanceManager, com.facebook.react.bridge.JavaScriptExecutor, com.facebook.react.bridge.JSBundleLoader) (ReactInstanceManager.java:116)
11-19 18:29:15.419 29371 29456 I art     :   at void com.facebook.react.ReactInstanceManager.run() (ReactInstanceManager.java:913)
11-19 18:29:15.419 29371 29456 I art     :   at void java.lang.Thread.run() (Thread.java:762)

上网查了一下,可能是依赖版本冲突。所以我检查了我的依赖关系树并注意到我正在使用的另一个模块也使用 com.google.android.gms:play-services-gcm,但版本不同。所以我更改了应用程序级别 build.gradle 中的版本以匹配其他版本,但我仍然遇到相同的错误。

这是我项目的依赖树:

releaseRuntimeClasspath - Resolved configuration for runtime for variant: release
+--- com.android.support:appcompat-v7:26.1.0 -> 27.1.1
|    +--- com.android.support:support-annotations:27.1.1
|    +--- com.android.support:support-core-utils:27.1.1
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    \--- com.android.support:support-compat:27.1.1
|    |         +--- com.android.support:support-annotations:27.1.1
|    |         \--- android.arch.lifecycle:runtime:1.1.0
|    |              +--- android.arch.lifecycle:common:1.1.0
|    |              \--- android.arch.core:common:1.1.0
|    +--- com.android.support:support-fragment:27.1.1
|    |    +--- com.android.support:support-compat:27.1.1 (*)
|    |    +--- com.android.support:support-core-ui:27.1.1
|    |    |    +--- com.android.support:support-annotations:27.1.1
|    |    |    +--- com.android.support:support-compat:27.1.1 (*)
|    |    |    \--- com.android.support:support-core-utils:27.1.1 (*)
|    |    +--- com.android.support:support-core-utils:27.1.1 (*)
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    +--- android.arch.lifecycle:livedata-core:1.1.0
|    |    |    +--- android.arch.lifecycle:common:1.1.0
|    |    |    +--- android.arch.core:common:1.1.0
|    |    |    \--- android.arch.core:runtime:1.1.0
|    |    |         \--- android.arch.core:common:1.1.0
|    |    \--- android.arch.lifecycle:viewmodel:1.1.0
|    +--- com.android.support:support-vector-drawable:27.1.1
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    \--- com.android.support:support-compat:27.1.1 (*)
|    \--- com.android.support:animated-vector-drawable:27.1.1
|         +--- com.android.support:support-vector-drawable:27.1.1 (*)
|         \--- com.android.support:support-core-ui:27.1.1 (*)
+--- com.facebook.react:react-native:+ -> 0.57.4
|    +--- com.facebook.infer.annotation:infer-annotation:0.11.2
|    |    \--- com.google.code.findbugs:jsr305:3.0.1 -> 3.0.2
|    +--- javax.inject:javax.inject:1
|    +--- com.android.support:appcompat-v7:27.1.1 (*)
|    +--- com.facebook.fresco:fresco:1.10.0
|    |    +--- com.facebook.fresco:fbcore:1.10.0
|    |    +--- com.facebook.fresco:drawee:1.10.0
|    |    |    +--- com.facebook.fresco:fbcore:1.10.0
|    |    |    \--- com.facebook.fresco:imagepipeline:1.10.0
|    |    |         +--- com.facebook.fresco:imagepipeline-base:1.10.0
|    |    |         |    +--- com.facebook.soloader:soloader:0.5.0 -> 0.5.1
|    |    |         |    +--- com.parse.bolts:bolts-tasks:1.4.0
|    |    |         |    \--- com.facebook.fresco:fbcore:1.10.0
|    |    |         +--- com.facebook.soloader:soloader:0.5.0 -> 0.5.1
|    |    |         +--- com.parse.bolts:bolts-tasks:1.4.0
|    |    |         \--- com.facebook.fresco:fbcore:1.10.0
|    |    +--- com.facebook.fresco:imagepipeline:1.10.0 (*)
|    |    \--- com.facebook.soloader:soloader:0.5.0 -> 0.5.1
|    +--- com.facebook.fresco:imagepipeline-okhttp3:1.10.0
|    |    +--- com.squareup.okhttp3:okhttp:3.10.0 -> 3.11.0
|    |    |    \--- com.squareup.okio:okio:1.14.0
|    |    +--- com.facebook.fresco:fbcore:1.10.0
|    |    \--- com.facebook.fresco:imagepipeline:1.10.0 (*)
|    +--- com.facebook.soloader:soloader:0.5.1
|    +--- com.google.code.findbugs:jsr305:3.0.2
|    +--- com.squareup.okhttp3:okhttp:3.11.0 (*)
|    +--- com.squareup.okhttp3:okhttp-urlconnection:3.11.0
|    |    \--- com.squareup.okhttp3:okhttp:3.11.0 (*)
|    +--- com.squareup.okio:okio:1.14.0
|    \--- org.webkit:android-jsc:r174650
+--- project :react-native-ble-manager
|    \--- com.facebook.react:react-native:[0.30.0,) -> 0.57.4 (*)
+--- project :react-native-camera
|    +--- com.google.zxing:core:3.3.0
|    +--- com.drewnoakes:metadata-extractor:2.9.1
|    |    \--- com.adobe.xmp:xmpcore:5.1.2
|    +--- com.google.android.gms:play-services-vision:11.0.4
|    |    +--- com.google.android.gms:play-services-base:[11.0.4] -> 16.0.1
|    |    |    +--- com.google.android.gms:play-services-basement:16.0.1
|    |    |    |    \--- com.android.support:support-v4:26.1.0
|    |    |    |         +--- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
|    |    |    |         +--- com.android.support:support-media-compat:26.1.0
|    |    |    |         |    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    |    |    |         |    \--- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
|    |    |    |         +--- com.android.support:support-core-utils:26.1.0 -> 27.1.1 (*)
|    |    |    |         +--- com.android.support:support-core-ui:26.1.0 -> 27.1.1 (*)
|    |    |    |         \--- com.android.support:support-fragment:26.1.0 -> 27.1.1 (*)
|    |    |    \--- com.google.android.gms:play-services-tasks:16.0.1
|    |    |         \--- com.google.android.gms:play-services-basement:16.0.1 (*)
|    |    +--- com.google.android.gms:play-services-basement:[11.0.4] -> 16.0.1 (*)
|    |    \--- com.google.android.gms:play-services-vision-common:[11.0.4] -> 11.0.4
|    |         +--- com.google.android.gms:play-services-base:[11.0.4] -> 16.0.1 (*)
|    |         \--- com.google.android.gms:play-services-basement:[11.0.4] -> 16.0.1 (*)
|    +--- com.android.support:exifinterface:26.1.0
|    |    \--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    \--- com.android.support:support-v4:26.1.0 (*)
+--- project :react-native-vector-icons
|    \--- com.facebook.react:react-native:+ -> 0.57.4 (*)
+--- project :react-native-maps
|    +--- com.google.android.gms:play-services-base:11.0.4 -> 16.0.1 (*)
|    +--- com.google.android.gms:play-services-maps:11.0.4
|    |    +--- com.google.android.gms:play-services-base:[11.0.4] -> 16.0.1 (*)
|    |    \--- com.google.android.gms:play-services-basement:[11.0.4] -> 16.0.1 (*)
|    \--- com.google.maps.android:android-maps-utils:0.5+ -> 0.5
+--- project :react-native-push-notification
|    +--- com.android.support:appcompat-v7:26.1.0 -> 27.1.1 (*)
|    +--- com.facebook.react:react-native:+ -> 0.57.4 (*)
|    +--- com.google.android.gms:play-services-gcm:11.0.4
|    |    +--- com.google.android.gms:play-services-base:[11.0.4] -> 16.0.1 (*)
|    |    +--- com.google.android.gms:play-services-basement:[11.0.4] -> 16.0.1 (*)
|    |    \--- com.google.android.gms:play-services-iid:[11.0.4] -> 11.0.4
|    |         +--- com.google.android.gms:play-services-base:[11.0.4] -> 16.0.1 (*)
|    |         \--- com.google.android.gms:play-services-basement:[11.0.4] -> 16.0.1 (*)
|    +--- me.leolin:ShortcutBadger:1.1.8
|    \--- com.google.firebase:firebase-messaging:+ -> 17.3.4
|         +--- com.google.android.gms:play-services-basement:16.0.1 (*)
|         +--- com.google.android.gms:play-services-tasks:16.0.1 (*)
|         +--- com.google.firebase:firebase-common:16.0.3
|         |    +--- com.google.android.gms:play-services-basement:16.0.1 (*)
|         |    \--- com.google.android.gms:play-services-tasks:16.0.1 (*)
|         +--- com.google.firebase:firebase-iid:[17.0.4] -> 17.0.4
|         |    +--- com.google.android.gms:play-services-basement:16.0.1 (*)
|         |    +--- com.google.android.gms:play-services-stats:16.0.1
|         |    |    \--- com.google.android.gms:play-services-basement:16.0.1 (*)
|         |    +--- com.google.android.gms:play-services-tasks:16.0.1 (*)
|         |    +--- com.google.firebase:firebase-common:16.0.3 (*)
|         |    \--- com.google.firebase:firebase-iid-interop:16.0.1
|         |         +--- com.google.android.gms:play-services-base:16.0.1 (*)
|         |         \--- com.google.android.gms:play-services-basement:16.0.1 (*)
|         \--- com.google.firebase:firebase-measurement-connector:17.0.1
|              \--- com.google.android.gms:play-services-basement:16.0.1 (*)
+--- com.google.android.gms:play-services-gcm:11.0.4 (*)
\--- project :react-native-barcode-scanner-google
     +--- com.google.android.gms:play-services-vision:11.0.4 (*)
     \--- com.facebook.react:react-native:0.20.+ -> 0.57.4 (*)

我看到有一些不匹配的版本,但我真的不知道如何正确跟踪或更正它们。

这是我的 build.gradle:

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

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 19
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
    }
}

allprojects {
    repositories {
        google()
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

ext {
    compileSdkVersion   = 26
    targetSdkVersion    = 26
    buildToolsVersion   = "26.0.2"
    supportLibVersion   = "26.1.0"
    googlePlayServicesVersion = "11.0.4"
    androidMapsUtilsVersion = "0.5+"
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

// Application level
apply plugin: "com.android.application"

import com.android.build.OutputFile

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

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

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "com.dpassapp"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    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"
        }
    }
    // 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 fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation project(':react-native-ble-manager')
    implementation project(':react-native-camera')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-maps')

    implementation project(':react-native-push-notification')
    implementation ('com.google.android.gms:play-services-gcm:11.0.4') {
        force = true;
    }

    implementation project(':react-native-barcode-scanner-google')
}

// 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'
}

有谁知道我可能遗漏了什么?或者如何正确调试依赖版本冲突?

这样做可能对你有帮助:

react-native link

react-native link (your dependency name)

像这样:

react-native link react-native-push-notification

我遇到了和你一样的问题,这对我有帮助

祝你好运

在依赖版本冲突(例如 Google Play 服务)中,最好的办法是修复顶级 gradle 中的依赖,然后从冲突的库中排除依赖。

compile(project(":react-native-push-notification")){ exclude group: "com.google.android.gms" }

但是,请确保与所有依赖项兼容的正确版本已固定在顶层 gradle。

我发现我的应用崩溃有 2 个(相似的)原因。

  1. 我的依赖项需要不同版本的 android 支持库
  2. 我的依赖项需要不同版本的 google 服务库

第一个更容易修复。但我真的很难弄清楚为什么我不能将所有 google 服务强制为同一版本,我不断收到错误消息,指出在存储库中找不到库。问题是并非所有依赖项都具有相同的 minor/patch 版本(如所列 here)。

例如:com.google.android.gms:play-services-base 最新版本是 16.0.1(甚至没有 16.0.0),而 com.google.android.gms:play.services.maps 最新版本是16.0.0。因此,如果我对这些版本中的任何一个进行硬编码,另一个版本就会抛出一个未找到的错误。

为了解决这个问题,我必须强制所有 google 服务库使用相同的主要版本,这样它才能为每个服务库获取正确的版本。

将这些行添加到我的顶层 build.gradle 解决了它:

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->

            // force all android support dependencies to 27.1.1
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "27.1.1"
            }

            // force all google services libraries to the same latest major version
            if(details.requested.group == 'com.google.android.gms') {
                details.useVersion '16.+'
            }
        }
    }
}