【问题标题】:react-native-device-info crash the app on androidreact-native-device-info 在 android 上使应用程序崩溃
【发布时间】:2018-06-26 07:02:58
【问题描述】:

我在 react-native 中使用 react-native-device-info 库。我已经使用它大约一年没有问题,但自从过去两周以来,Android 上的应用程序由于这个库而崩溃。

我收到如下错误:

他们讨论了这个问题here。他们的解释如下:

无法合并 dex / 多个 dex 文件 / 出现问题 com.google.android.gms react-native-device-info 使用 com.google.android.gms:play-services-gcm 提供 [getInstance()][#getinstance]。这可能会导致冲突 构建 Android 应用程序。

如果您使用的是不同版本的 com.google.android.gms:play-services-gcm 在你的应用程序中,你可以定义 build.gradle 中的 googlePlayServicesVersion gradle 变量 文件告诉 react-native-device-info 它应该需要什么版本。

如果您使用的其他库与 com.google.android.gms:play-services-gcm,你可以忽略这个 gradle 文件中的依赖项:

compile(project(':react-native-device-info')) {
    exclude group: 'com.google.android.gms'
}

我已尝试将该代码添加到 build.gradle,但没有任何反应,sam 问题。

我不知道如何解决它。有什么想法吗?

【问题讨论】:

  • 你这样做了吗? react-native 链接 react-native-device-info
  • @Pramod 当然可以。

标签: android reactjs react-native


【解决方案1】:

将您的 app/build.gradle 依赖项更改为如下所示:

dependencies {
  ...
  compile project(':react-native-device-info')
  compile('com.google.android.gms:play-services-gcm:11.8.0') {
      force = true
  }
}

如果11.8.0 不起作用,请尝试将其替换为+。 react-native-device-info 使用 + 作为 com.google.android.gms:play-services-gcm 依赖项。

更新

如果这不起作用,请按照错误消息中的建议进行操作 --> 尝试将 def googlePlayServicesVersion = "11.8.0" 添加到您的 app/build.gradle 以强制使用 react-native-device-info 上的版本。 (您可以根据需要更改版本)

然后将依赖项更改为如下所示:

dependencies {
  ...
  compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"
} 

如果您查看他们的 build.gradle 文件,他们会像这样处理它们的依赖关系:

...
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION    = "+"     
...
dependencies {
  def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION

  compile 'com.facebook.react:react-native:+'
  compile "com.google.android.gms:play-services-gcm:$googlePlayServicesVersion"
}

更新

(这是有效的解决方案)

如果这不起作用,请尝试将您的项目级别 build.gradle 文件更改为如下所示(存储库顺序很重要):

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

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

allprojects {
  repositories {
    maven {
        url 'https://maven.google.com/'
        name '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"
    }
  }
}

存储库的顺序很重要,因为例如,如果 jcenter() 排在第一位,并且它会在其中找到 com.google.android.gms 包,它将检索这些源,它们可能是错误的。在您的情况下,maven {Google...} 元素指向您需要的来源,这就是为什么必须在使用 jcenter() 之前调用它。

【讨论】:

  • 如果我使用 11.8.0 我会得到同样的错误。如果我用+ 替换它,我会收到错误A problem occurred evaluating project ':app'. > Failed to apply plugin [id 'com.google.gms.google-services'] > For input string: "+"
  • 更新了我的答案
  • 如果我使用11.8.0,我会收到如下错误> Could not find com.google.android.gms:play-services-gcm:11.8.0.。但是如果我去cd ~/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services,我会发现那里没有11.8.0。最新的是11.0.4。但是当我尝试使用11.0.4 时,我得到了与问题中相同的错误。
  • 更新了我的答案...我正在使用react-native-device-info lib,一切对我来说都很好..所以我试图了解我这边做了什么:)顺便说一句,你启用multidex了吗?
  • 修改build.gradle 成功了。 multidex 未启用。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 2022-01-02
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
相关资源
最近更新 更多