简体   繁体   中英

undefined is not object (evaluating 'RNDeviceInfo.deviceId') - error in react-native-device-info

I am using react-native-device-info library, and it works fine for iOS simulator. But for Android I receive an error:

undefined is not object (evaluating 'RNDeviceInfo.deviceId')

What can be the problem? I installed it like is described in the guide on the library's page (using rnpm). Then in componentWillMount() I am trying to get the device id:

import DeviceInfo from 'react-native-device-info';
... 
componentWillMount() {
    let clientId = DeviceInfo.getUniqueID();
  }

In addition to rnpm install react-native-device-info and instruction from here , I had to manually add some lines to the following files:

  1. <YourReactProject>/android/settings.gradle :

     rootProject.name = 'YourReactProject' include ':app', ':react-native-device-info' project(':react-native-device-info').projectDir = newFile(rootProject.projectDir, '../node_modules/react-native-device-info/android') 
  2. <YourReactProject>/android/app/build.gradle :

     ... dependencies { compile fileTree(dir: "libs", include: ["*.jar"]) compile "com.android.support:appcompat-v7:23.0.1" compile "com.facebook.react:react-native:+" // From node_modules compile project(':react-native-device-info') } ... 
  3. Rebuild by running react-native run-android

Firstly, make sure that react-native-device-info is linked in your project if you are using react-native version < 0.60.

If not linked, link it using react-native link react-native-device-info and then run react-native run-android command.

If using react-native version >= 0.60 skip this link command as auto-linking should work but make sure to run react-native run-android command.

and then use it using,

import DeviceInfo from 'react-native-device-info';

DeviceInfo.getUniqueId()

There is a small typo. You are using DeviceInfo.getUniqueID(); instead of DeviceInfo.getUniqueId()

Review in the documentation here

First check is it added to your package.json or not? Then link the package with react native using following command

react-native link react-native-device-info

It manually links the package with react-native.

I am facing this issue in IOS.

Even I link the package it showing the same issue.

open your project in Xcode, then under build phases add a binary file of react-native-device-info by clicking on the + button.

Rerun the project, It worked for me.

在此输入图像描述

It looks like this package now uses Cocoapods. Run react-native link then check your Podfile for something like this:

pod 'RNDeviceInfo', :path => '../node_modules/react-native-device-info'

If that's in there, then you just need to install / update your pods.

cd ios
pod install

or

cd ios
pod update

Make sure you restart the metro server and re-run react-native run-ios

use the manual installation and change

your project\\node_modules\\react-native-device-info\\android\\build.gradle'

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

in here implementation as a compile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM