0

I'm using react-native-device-info library in react-native. I've used it for about a year without problems, but since the last two weeks the app on Android crashes because of this library.

I get an error as follows:

enter image description here

They discussed about this issue here. And they explanation is as follows:

Unable to merge dex / Multiple dex files / Problems with com.google.android.gms react-native-device-info uses com.google.android.gms:play-services-gcm to provide [getInstance()][#getinstance]. This can lead to conflicts when building the Android application.

If you're using a different version of com.google.android.gms:play-services-gcm in your app, you can define the googlePlayServicesVersion gradle variable in your build.gradle file to tell react-native-device-info what version it should require.

If you're using a different library that conflicts with com.google.android.gms:play-services-gcm, you can simply ignore this dependency in your gradle file:

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

I've tried to add that code to build.gradle but nothing happens, the sam problem.

I'm not sure how to solve it. Any idea?

2
  • have you done this ? react-native link react-native-device-info Commented Jun 26, 2018 at 7:36
  • @Pramod Of course. Commented Jun 26, 2018 at 8:41

1 Answer 1

3

Change your app/build.gradle dependencies to look like this:

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

If 11.8.0 doesn't work try to replace it with +. The react-native-device-info uses + for it's com.google.android.gms:play-services-gcm dependency.

UPDATE

If that doesn't work, do as suggested in the error message --> try adding def googlePlayServicesVersion = "11.8.0" to your app/build.gradle to force the version on react-native-device-info. (You can change the version as you like)

Then change your dependencies to look like this:

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

If you take a look at their build.gradle file, they handle their dependencies like this:

...
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"
}

UPDATE

(This is the solution that worked)

If that doesn't work, try changing your project level build.gradle file to look like this (The repositories order is important):

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"
    }
  }
}

The order of the repositories is important because if, for example, jcenter() is first, and it will find the com.google.android.gms package in there, it will retrieve those sources, and they might be the wrong ones. In your case, the maven {Google...} element pointed to the sources you need, and this is why it had to be called before jcenter() was used.

Sign up to request clarification or add additional context in comments.

6 Comments

If I use 11.8.0 I get the same error. If I replace it with + I get an error A problem occurred evaluating project ':app'. > Failed to apply plugin [id 'com.google.gms.google-services'] > For input string: "+"
Updated my answer
If I use 11.8.0 I get an error as follows > Could not find com.google.android.gms:play-services-gcm:11.8.0.. But If I go to cd ~/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services I see that I do not have 11.8.0 there. The newest is 11.0.4. But when I try with 11.0.4 I get the same error as in the question.
Updated my answer... I'm using the react-native-device-info lib and everything is working fine for me.. So I'm trying to understand what is done right on my side :) BTW, have you enabled multidex ?
The modifying build.gradle did the trick. multidex is not enabled. Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.