From d79fc4205e1a8f19e6d243a015e8ec983da1aa1b Mon Sep 17 00:00:00 2001
From: Website Deployment Script Follow the instructions for building apps with native code from the Getting Started guide to configure your development environment for building React Native apps for Android. To ensure a smooth experience, create a new folder for your integrated React Native project, then copy your existing Android project to a To ensure a smooth experience, create a new folder for your integrated React Native project, then copy your existing Android project to an Go to the root directory for your project and create a new Next, you will install the Next, make sure you have installed the yarn package manager. Install the This will print something like: Make sure you use the same React version as specified in the React Native package.json for your release. This will only be necessary as long as React Native depends on a pre-release version of React. warning "react-native@0.52.2" has unmet peer dependency "react@16.2.0". This will create a new This is OK, it means we also need to install react: This will create a new Add CocoaPods is a package management tool for iOS and macOS development. We use it to add the actual React Native framework code locally into your current project. If you need to access to the This is only really used in dev mode when reloading JavaScript from the development server, so you can strip this in release builds if you need to. This is only used in dev mode when reloading JavaScript from the development server, so you can strip this in release builds if you need to. Now we will actually modify the native Android application to integrate React Native. If your app is targeting the Android If your app is targeting the Android You need to add some native code in order to start the React Native runtime and get it to render something. To do this, we're going to create an Let's add some native code in order to start the React Native runtime and tell it to render our JS component. To do this, we're going to create an If you are targetting Android version <5, use the If you are using a starter kit for React Native, replace the "HelloWorld" string with the one in your index.js file (it’s the first argument to the If you are using Android Studio, use We need set the theme of If you are using Android Studio, use We need set the theme of A A Next, we need to pass some activity lifecycle callbacks down to the Next, we need to pass some activity lifecycle callbacks to the This allows JavaScript to control what happens when the user presses the hardware back button (e.g. to implement navigation). When JavaScript doesn't handle a back press, your This allows JavaScript to control what happens when the user presses the hardware back button (e.g. to implement navigation). When JavaScript doesn't handle the back button press, your Finally, we need to hook up the dev menu. By default, this is activated by (rage) shaking the device, but this is not very useful in emulators. So we make it show when you press the hardware menu button (use 1. Set up directory structure
-/android subfolder./android subfolder.2. Install JavaScript dependencies
package.json file with the following contents:react and react-native packages. Open a terminal or command prompt, then navigate to the root directory for your project and type the following commands:
+$ npm install --save react@16.0.0-beta.5 react-native
-react and react-native packages. Open a terminal or command prompt, then navigate to the directory with your package.json file and run:
+$ yarn add react-native
+
-
-/node_modules folder in your project's root directory. This folder stores all the JavaScript dependencies required to build your project.
+$ yarn add react@16.2.0 # Make sure you use the same react version as printed by yarn when installing react-native!
+/node_modules folder. This folder stores all the JavaScript dependencies required to build your project.node_modules/ to your .gitignore file.3. Install CocoaPods
dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
...
- compile "com.facebook.react:react-native:+" // From node_modules.
+ compile "com.facebook.react:react-native:+" // From node_modules
}
@@ -454,7 +460,7 @@ $ react-native run-ios
-DevSettingsActivity add to your AndroidManifest.xml:
-<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
Code integration
The React Native component
@@ -491,8 +497,12 @@ $ react-native run-ios
AppRegistry.registerComponent('MyReactNativeApp', () => HelloWorld);
3. Configure permissions for development error overlay
-API level 23 or greater, make sure you have the overlay permission enabled for the development build. You can check it with Settings.canDrawOverlays(this);. This is required in dev builds because react native development errors must be displayed above all the other windows. Due to the new permissions system introduced in the API level 23, the user needs to approve it. This can be achieved by adding the following code to the Activity file in the onCreate() method. OVERLAY_PERMISSION_REQ_CODE is a field of the class which would be responsible for passing the result back to the Activity. (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
- // SYSTEM_ALERT_WINDOW permission not granted...
+ // SYSTEM_ALERT_WINDOW permission not granted
}
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+API level 23 or greater, make sure you have the permission android.permission.SYSTEM_ALERT_WINDOW enabled for the development build. You can check this with Settings.canDrawOverlays(this);. This is required in dev builds because React Native development errors must be displayed above all the other windows. Due to the new permissions system introduced in the API level 23 (Android M), the user needs to approve it. This can be achieved by adding the following code to your Activity's in onCreate() method.private final int OVERLAY_PERMISSION_REQ_CODE = 1; // Choose any value
+
+...
+
+if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
@@ -506,14 +516,14 @@ AppRegistry.registerComponent('MyReactNativeApp'ifThe Magic:
-ReactRootViewActivity that creates a ReactRootView, starts a React application inside it and sets it as the main content view.Activity that creates a ReactRootView, starts a React application inside it and sets it as the main content view.
@@ -534,6 +544,8 @@ AppRegistry.registerComponent('MyReactNativeApp'// The string here (e.g. "MyReactNativeApp") has to match
+ // the string in AppRegistry.registerComponent() in index.js
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
setContentView(mReactRootView);
@@ -548,8 +560,8 @@ AppRegistry.registerComponent('MyReactNativeApp'
AppCompatActivity class from the com.android.support:appcompat package instead of Activity.AppRegistry.registerComponent() method).Alt + Enter to add all missing imports in your MyReactActivity class. Be careful to use your package’s BuildConfig and not the one from the ...facebook... package.MyReactActivity to Theme.AppCompat.Light.NoActionBar because some components rely on this theme.Alt + Enter to add all missing imports in your MyReactActivity class. Be careful to use your package’s BuildConfig and not the one from the facebook package.MyReactActivity to Theme.AppCompat.Light.NoActionBar because some React Native UI components rely on this theme.<activity
android:name=".MyReactActivity"
android:label="@string/app_name"
@@ -557,9 +569,9 @@ AppRegistry.registerComponent('MyReactNativeApp'</activity>
-
-ReactInstanceManager can be shared amongst multiple activities and/or fragments. You will want to make your own ReactFragment or ReactActivity and have a singleton holder that holds a ReactInstanceManager. When you need the ReactInstanceManager (e.g., to hook up the ReactInstanceManager to the lifecycle of those Activities or Fragments) use the one provided by the singleton.ReactInstanceManager can be shared by multiple activities and/or fragments. You will want to make your own ReactFragment or ReactActivity and have a singleton holder that holds a ReactInstanceManager. When you need the ReactInstanceManager (e.g., to hook up the ReactInstanceManager to the lifecycle of those Activities or Fragments) use the one provided by the singleton.ReactInstanceManager:ReactInstanceManager:
-@Override
protected void onPause() {
super.onPause();
@@ -597,7 +609,7 @@ AppRegistry.registerComponent('MyReactNativeApp'invokeDefaultOnBackPressed method will be called. By default this simply finishes your Activity.invokeDefaultOnBackPressed method will be called. By default this simply finishes your Activity.Ctrl + M if you're using Android Studio emulator):@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
@@ -613,7 +625,7 @@ AppRegistry.registerComponent('MyReactNativeApp'You have now done all the basic steps to integrate React Native with your current application. Now we will start the React Native packager to build the index.bundle package and the server running on localhost to serve it.
To run your app, you need to first start the development server. To do this, simply run the following command in the root directory of your React Native project:
-$ npm start
+$ yarn start
2. Run the app
Now build and run your Android app as normal.