Commit 2ec30aa7 authored by Rebecca Hughes's avatar Rebecca Hughes

Add android support

parent f9501fb8
......@@ -2,7 +2,7 @@
Device Information for react-native
## Usage
## Installation
First you need to install react-native-device-info:
......@@ -10,6 +10,8 @@ First you need to install react-native-device-info:
npm install react-native-device-info --save
```
### Installation (iOS)
In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name] Go to node_modules ➜ react-native-device-info and add the .xcodeproj file
In XCode, in the project navigator, select your project. Add the lib*.a from the deviceinfo project to your project's Build Phases ➜ Link Binary With Libraries Click .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths and make sure it contains both $(SRCROOT)/../react-native/React and $(SRCROOT)/../../React - mark both as recursive.
......@@ -18,6 +20,61 @@ Run your project (Cmd+R)
(Thanks to @brysgo for writing the instructions)
### Installation (Android)
* In `android/setting.gradle`
```gradle
...
include ':RNDeviceInfo', ':app'
project(':RNDeviceInfo').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')
```
* In `android/app/build.gradle`
```gradle
...
dependencies {
...
compile project(':RNDeviceInfo')
}
```
* register module (in MainActivity.java)
```java
import com.learnium.RNDeviceInfo.*; // <--- import
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new RNDeviceInfo()) // <------ add here
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);
setContentView(mReactRootView);
}
......
}
```
(Thanks to @chirag04 for writing the instructions)
## Example
```js
......
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
}
dependencies {
compile 'com.facebook.react:react-native:0.11.+'
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnium.RNDeviceInfo">
</manifest>
package com.learnium.RNDeviceInfo;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
public class RNDeviceInfo implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new RNDeviceModule(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(
ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
package com.learnium.RNDeviceInfo;
import javax.annotation.Nullable;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;
import android.os.Build;
import android.provider.Settings.Secure;
import java.util.HashMap;
import java.util.Map;
public class RNDeviceModule extends ReactContextBaseJavaModule {
ReactApplicationContext reactContext;
public RNDeviceModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNDeviceInfo";
}
@Override
public @Nullable Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<String, Object>();
constants.put("systemVersion", Build.VERSION.RELEASE);
constants.put("model", Build.MODEL);
constants.put("deviceId", Secure.getString(this.reactContext.getContentResolver(), Secure.ANDROID_ID));
constants.put("systemManufacturer", Build.MANUFACTURER);
return constants;
}
}
/**
* @providesModule react-native-device-info
*/
var { RNDeviceInfo } = require('react-native').NativeModules;
module.exports = {
getUniqueID: function () {
return RNDeviceInfo.deviceId;
},
getManufacturer: function () {
return RNDeviceInfo.systemManufacturer;
},
getModel: function () {
return RNDeviceInfo.model;
},
getSystemName: function () {
return "Android";
},
getSystemVersion: function () {
return RNDeviceInfo.systemVersion;
}
};
......@@ -4,24 +4,20 @@
var { RNDeviceInfo } = require('react-native').NativeModules;
export default class CurrentDeviceIOS {
static getUniqueID() {
return RNDeviceInfo.deviceId;
}
static getManufacturer() {
return "Apple";
}
static getModel() {
return RNDeviceInfo.model;
}
static getSystemName() {
return RNDeviceInfo.systemName;
}
static getSystemVersion() {
return RNDeviceInfo.systemVersion;
}
}
module.exports = {
getUniqueID: function () {
return RNDeviceInfo.deviceId;
},
getManufacturer: function () {
return "Apple";
},
getModel: function () {
return RNDeviceInfo.model;
},
getSystemName: function () {
return RNDeviceInfo.systemName;
},
getSystemVersion: function () {
return RNDeviceInfo.systemVersion;
}
};
{
"name": "react-native-device-info",
"version": "0.2.1",
"version": "0.3.0",
"description": "Get device information using react-native",
"main": "deviceinfo.ios.js",
"repository": {
......@@ -11,6 +11,7 @@
"react-component",
"react-native",
"ios",
"android",
"device",
"events"
],
......
node_modules/react-native-device-info
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment