mirror of
https://github.com/status-im/react-native.git
synced 2025-01-13 11:05:21 +00:00
Additional doc improvements
Summary: Mostly cleaning up running on device, integrating with existing apps guides. Closes https://github.com/facebook/react-native/pull/14165 Differential Revision: D5125590 Pulled By: hramos fbshipit-source-id: 9239b41a67ab92789e6e409f2715c2cf179fa5f6
This commit is contained in:
parent
fe229f8104
commit
9bde0fba70
@ -9,102 +9,176 @@ next: running-on-device
|
||||
previous: testing
|
||||
---
|
||||
|
||||
<div class="integration-toggler">
|
||||
<style>
|
||||
.integration-toggler a {
|
||||
display: inline-block;
|
||||
padding: 10px 5px;
|
||||
margin: 2px;
|
||||
border: 1px solid #05A5D1;
|
||||
border-radius: 3px;
|
||||
text-decoration: none !important;
|
||||
}
|
||||
.display-platform-objc .integration-toggler .button-objc,
|
||||
.display-platform-swift .integration-toggler .button-swift,
|
||||
.display-platform-android .integration-toggler .button-android {
|
||||
background-color: #05A5D1;
|
||||
color: white;
|
||||
}
|
||||
block { display: none; }
|
||||
.display-platform-objc .objc,
|
||||
.display-platform-swift .swift,
|
||||
.display-platform-android .android {
|
||||
display: block;
|
||||
}</style>
|
||||
<span>Platform:</span>
|
||||
<a href="javascript:void(0);" class="button-objc" onclick="display('platform', 'objc')">Objective-C</a>
|
||||
<a href="javascript:void(0);" class="button-swift" onclick="display('platform', 'swift')">Swift</a>
|
||||
<a href="javascript:void(0);" class="button-android" onclick="display('platform', 'android')">Android</a>
|
||||
.toggler li {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
top: 1px;
|
||||
padding: 10px;
|
||||
margin: 0px 2px 0px 2px;
|
||||
border: 1px solid #05A5D1;
|
||||
border-bottom-color: transparent;
|
||||
border-radius: 3px 3px 0px 0px;
|
||||
color: #05A5D1;
|
||||
background-color: transparent;
|
||||
font-size: 0.99em;
|
||||
cursor: pointer;
|
||||
}
|
||||
.toggler li:first-child {
|
||||
margin-left: 0;
|
||||
}
|
||||
.toggler li:last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
.toggler ul {
|
||||
width: 100%;
|
||||
display: inline-block;
|
||||
list-style-type: none;
|
||||
margin: 0;
|
||||
border-bottom: 1px solid #05A5D1;
|
||||
cursor: default;
|
||||
}
|
||||
@media screen and (max-width: 960px) {
|
||||
.toggler li,
|
||||
.toggler li:first-child,
|
||||
.toggler li:last-child {
|
||||
display: block;
|
||||
border-bottom-color: #05A5D1;
|
||||
border-radius: 3px;
|
||||
margin: 2px 0px 2px 0px;
|
||||
}
|
||||
.toggler ul {
|
||||
border-bottom: 0;
|
||||
}
|
||||
}
|
||||
.toggler a {
|
||||
display: inline-block;
|
||||
padding: 10px 5px;
|
||||
margin: 2px;
|
||||
border: 1px solid #05A5D1;
|
||||
border-radius: 3px;
|
||||
text-decoration: none !important;
|
||||
}
|
||||
.display-platform-objc .toggler .button-objc,
|
||||
.display-platform-swift .toggler .button-swift,
|
||||
.display-platform-android .toggler .button-android {
|
||||
background-color: #05A5D1;
|
||||
color: white;
|
||||
}
|
||||
block { display: none; }
|
||||
.display-platform-objc .objc,
|
||||
.display-platform-swift .swift,
|
||||
.display-platform-android .android {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
|
||||
React Native is great when you are starting a new mobile app from scratch. However, it also works well for adding a single view or user flow to existing native applications. With a few steps, you can add new React Native based features, screens, views, etc.
|
||||
|
||||
The specific steps are different depending on what platform you're targeting.
|
||||
|
||||
<div class="toggler">
|
||||
<ul role="tablist" >
|
||||
<li id="objc" class="button-objc" aria-selected="false" role="tab" tabindex="0" aria-controls="objctab" onclick="display('platform', 'objc')">
|
||||
iOS (Objective-C)
|
||||
</li>
|
||||
<li id="swift" class="button-swift" aria-selected="false" role="tab" tabindex="0" aria-controls="swifttab" onclick="display('platform', 'swift')">
|
||||
iOS (Swift)
|
||||
</li>
|
||||
<li id="android" class="button-android" aria-selected="false" role="tab" tabindex="0" aria-controls="androidtab" onclick="display('platform', 'android')">
|
||||
Android (Java)
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<block class="android" />
|
||||
|
||||
> This section will be updated shortly showing an integration into a more real world application such as the 2048 app that was used for Objective-C and Swift.
|
||||
|
||||
<block class="objc swift android" />
|
||||
|
||||
## Key Concepts
|
||||
|
||||
React Native is great when you are starting a new mobile app from scratch. However, it also works well for adding a single view or user flow to existing native applications. With a few steps, you can add new React Native based features, screens, views, etc.
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
The keys to integrating React Native components into your iOS application are to:
|
||||
|
||||
1. Understand what React Native components you want to integrate.
|
||||
2. Create a `Podfile` with `subspec`s for all the React Native components you will need for your integration.
|
||||
3. Create your actual React Native components in JavaScript.
|
||||
4. Add a new event handler that creates a `RCTRootView` that points to your React Native component and its `AppRegistry` name that you defined in `index.ios.js`.
|
||||
5. Start the React Native server and run your native application.
|
||||
6. Optionally add more React Native components.
|
||||
7. [Debug](/react-native/releases/next/docs/debugging.html).
|
||||
8. Prepare for [deployment](docs/running-on-device.html) (e.g., via the `react-native-xcode.sh` script).
|
||||
9. Deploy and Profit!
|
||||
1. Set up React Native dependencies and directory structure.
|
||||
2. Understand what React Native components you will use in your app.
|
||||
3. Add these components as dependencies using CocoaPods.
|
||||
4. Develop your React Native components in JavaScript.
|
||||
5. Add a `RCTRootView` to your iOS app. This view will serve as the container for your React Native component.
|
||||
6. Start the React Native server and run your native application.
|
||||
7. Verify that the React Native aspect of your application works as expected.
|
||||
|
||||
<block class="android" />
|
||||
|
||||
The keys to integrating React Native components into your Android application are to:
|
||||
|
||||
1. Understand what React Native components you want to integrate.
|
||||
2. Install `react-native` in your Android application root directory to create `node_modules/` directory.
|
||||
3. Create your actual React Native components in JavaScript.
|
||||
4. Add `com.facebook.react:react-native:+` and a `maven` pointing to the `react-native` binaries in `node_modules/` to your `build.gradle` file.
|
||||
4. Create a custom React Native specific `Activity` that creates a `ReactRootView`.
|
||||
5. Start the React Native server and run your native application.
|
||||
6. Optionally add more React Native components.
|
||||
7. [Debug](/react-native/releases/next/docs/debugging.html).
|
||||
8. [Prepare](/react-native/releases/next/docs/signed-apk-android.html) for [deployment](docs/running-on-device.html).
|
||||
9. Deploy and Profit!
|
||||
1. Set up React Native dependencies and directory structure.
|
||||
2. Develop your React Native components in JavaScript.
|
||||
3. Add a `ReactRootView` to your Android app. This view will serve as the container for your React Native component.
|
||||
4. Start the React Native server and run your native application.
|
||||
5. Verify that the React Native aspect of your application works as expected.
|
||||
|
||||
<block class="objc swift android" />
|
||||
|
||||
## Prerequisites
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
Follow the instructions for building apps with native code from the [Getting Started guide](docs/getting-started.html) to configure your development environment for building React Native apps for iOS.
|
||||
|
||||
### 1. Set up directory structure
|
||||
|
||||
To ensure a smooth experience, create a new folder for your integrated React Native project, then copy your existing iOS project to a `/ios` subfolder.
|
||||
|
||||
<block class="android" />
|
||||
|
||||
The [Android Getting Started guide](docs/getting-started.html) will install the appropriate prerequisites (e.g., `npm`) for React Native on the Android target platform and your chosen development environment.
|
||||
Follow the instructions for building apps with native code from the [Getting Started guide](docs/getting-started.html) to configure your development environment for building React Native apps for Android.
|
||||
|
||||
> To ensure a smooth experience, make sure your `android` project is under `$root/android`.
|
||||
### 1. Set up directory structure
|
||||
|
||||
To ensure a smooth experience, create a new folder for your integrated React Native project, then copy your existing Android project to a `/android` subfolder.
|
||||
|
||||
<block class="objc swift android" />
|
||||
|
||||
### 2. Install JavaScript dependencies
|
||||
|
||||
Go to the root directory for your project and create a new `package.json` file with the following contents:
|
||||
|
||||
```bash
|
||||
{
|
||||
"name": "MyReactNativeApp",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node node_modules/react-native/local-cli/cli.js start"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Next, you will install the `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:
|
||||
|
||||
```bash
|
||||
$ npm install --save react react-native
|
||||
```
|
||||
|
||||
This will create a new `/node_modules` folder in your project's root directory. This folder stores all the JavaScript dependencies required to build your project.
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
### General
|
||||
### 3. Install CocoaPods
|
||||
|
||||
First, follow the [Getting Started guide](docs/getting-started.html) for your development environment and the iOS target platform to install the prerequisites for React Native.
|
||||
[CocoaPods](http://cocoapods.org) 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.
|
||||
|
||||
> To ensure a smooth experience, make sure your `iOS` project is under `$root/ios`.
|
||||
|
||||
### CocoaPods
|
||||
|
||||
[CocoaPods](http://cocoapods.org) is a package management tool for iOS and Mac development. We use it to add the actual React Native framework code locally into your current project.
|
||||
We recommend installing CocoaPods using [Homebrew](http://brew.sh/).
|
||||
|
||||
```bash
|
||||
$ sudo gem install cocoapods
|
||||
$ brew install cocoapods
|
||||
```
|
||||
|
||||
> It is technically possible not to use CocoaPods, but this requires manual library and linker additions that overly complicates this process.
|
||||
> It is technically possible not to use CocoaPods, but that would require manual library and linker additions that would overly complicate this process.
|
||||
|
||||
## Our Sample App
|
||||
<block class="objc swift" />
|
||||
|
||||
## Adding React Native to your app
|
||||
|
||||
<block class="objc" />
|
||||
|
||||
@ -118,90 +192,19 @@ Assume the [app for integration](https://github.com/JoelMarcey/swift-2048) is a
|
||||
|
||||
![Before RN Integration](img/react-native-existing-app-integration-ios-before.png)
|
||||
|
||||
## Package Dependencies
|
||||
### Configuring CocoaPods dependencies
|
||||
|
||||
React Native integration requires both the React and React Native node modules. The React Native Framework will provide the code to allow your application integration to happen.
|
||||
Before you integrate React Native into your application, you will want to decide what parts of the React Native framework you would like to integrate. We will use CocoaPods to specify which of these "subspecs" your app will depend on.
|
||||
|
||||
The list of supported `subspec`s is available in [`/node_modules/react-native/React.podspec`](https://github.com/facebook/react-native/blob/master/React.podspec). They are generally named by functionality. For example, you will generally always want the `Core` `subspec`. That will get you the `AppRegistry`, `StyleSheet`, `View` and other core React Native libraries. If you want to add the React Native `Text` library (e.g., for `<Text>` elements), then you will need the `RCTText` `subspec`. If you want the `Image` library (e.g., for `<Image>` elements), then you will need the `RCTImage` `subspec`.
|
||||
|
||||
### `package.json`
|
||||
|
||||
We will add the package dependencies to a `package.json` file. Create this file in the root of your project if it does not exist.
|
||||
|
||||
> Normally with React Native projects, you will put files like `package.json`, `index.ios.js`, etc. in the root directory of your project and then have your iOS specific native code in a subdirectory like `ios/` where your Xcode project is located (e.g., `.xcodeproj`).
|
||||
|
||||
Below is an example of what your `package.json` file should minimally contain.
|
||||
|
||||
> Version numbers will vary according to your needs. Normally the latest versions for both [React](https://github.com/facebook/react/releases) and [React Native](https://github.com/facebook/react-native/releases) will be sufficient.
|
||||
|
||||
<block class="objc" />
|
||||
You can specify which `subspec`s your app will depend on in a `Podfile` file. The easiest way to create a `Podfile` is by running the CocoaPods `init` command in the `/ios` subfolder of your project:
|
||||
|
||||
```bash
|
||||
{
|
||||
"name": "NumberTileGame",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node node_modules/react-native/local-cli/cli.js start"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "15.0.2",
|
||||
"react-native": "0.26.1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<block class="swift" />
|
||||
|
||||
```bash
|
||||
{
|
||||
"name": "swift-2048",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "node node_modules/react-native/local-cli/cli.js start"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "15.0.2",
|
||||
"react-native": "0.26.1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
### Packages Installation
|
||||
|
||||
Install the React and React Native modules via the Node package manager. The Node modules will be installed into a `node_modules/` directory in the root of your project.
|
||||
|
||||
```bash
|
||||
# From the directory containing package.json project, install the modules
|
||||
# The modules will be installed in node_modules/
|
||||
$ npm install
|
||||
```
|
||||
|
||||
## React Native Framework
|
||||
|
||||
The React Native Framework was installed as Node module in your project [above](#package-dependencies). We will now install a CocoaPods `Podfile` with the components you want to use from the framework itself.
|
||||
|
||||
### Subspecs
|
||||
|
||||
Before you integrate React Native into your application, you will want to decide what parts of the React Native Framework you would like to integrate. That is where `subspec`s come in. When you create your `Podfile`, you are going to specify React Native library dependencies that you will want installed so that your application can use those libraries. Each library will become a `subspec` in the `Podfile`.
|
||||
|
||||
|
||||
The list of supported `subspec`s are in [`node_modules/react-native/React.podspec`](https://github.com/facebook/react-native/blob/master/React.podspec). They are generally named by functionality. For example, you will generally always want the `Core` `subspec`. That will get you the `AppRegistry`, `StyleSheet`, `View` and other core React Native libraries. If you want to add the React Native `Text` library (e.g., for `<Text>` elements), then you will need the `RCTText` `subspec`. If you want the `Image` library (e.g., for `<Image>` elements), then you will need the `RCTImage` `subspec`.
|
||||
|
||||
#### Podfile
|
||||
|
||||
After you have used Node to install the React and React Native frameworks into the `node_modules` directory, and you have decided on what React Native elements you want to integrate, you are ready to create your `Podfile` so you can install those components for use in your application.
|
||||
|
||||
The easiest way to create a `Podfile` is by using the CocoaPods `init` command in the native iOS code directory of your project:
|
||||
|
||||
```bash
|
||||
## In the directory where your native iOS code is located (e.g., where your `.xcodeproj` file is located)
|
||||
$ pod init
|
||||
```
|
||||
|
||||
The `Podfile` will be created and saved in the *iOS* directory (e.g., `ios/`) of your current project and will contain a boilerplate setup that you will tweak for your integration purposes. In the end, `Podfile` should look something similar to this:
|
||||
The `Podfile` will contain a boilerplate setup that you will tweak for your integration purposes. In the end, `Podfile` should look something similar to this:
|
||||
|
||||
<block class="objc" />
|
||||
|
||||
@ -253,8 +256,6 @@ end
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
#### Pod Installation
|
||||
|
||||
After you have created your `Podfile`, you are ready to install the React Native pod.
|
||||
|
||||
```bash
|
||||
@ -280,26 +281,21 @@ Pod installation complete! There are 3 dependencies from the Podfile and 1 total
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
## Code Integration
|
||||
### Code integration
|
||||
|
||||
Now that we have a package foundation, we will actually modify the native application to integrate React Native into the application. For our 2048 app, we will add a "High Score" screen in React Native.
|
||||
Now we will actually modify the native iOS application to integrate React Native. For our 2048 sample app, we will add a "High Score" screen in React Native.
|
||||
|
||||
### The React Native component
|
||||
#### The React Native component
|
||||
|
||||
The first bit of code we will write is the actual React Native code for the new "High Score" screen that will be integrated into our application.
|
||||
|
||||
#### Create a `index.ios.js` file
|
||||
##### 1. Create a `index.ios.js` file
|
||||
|
||||
First, create an empty `index.ios.js` file. For ease, I am doing this in the root of the project.
|
||||
First, create an empty `index.ios.js` file in the root of your React Native project.
|
||||
|
||||
> `index.ios.js` is the starting point for React Native applications on iOS. And it is always required. It can be a small file that `require`s other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in `index.ios.js`
|
||||
`index.ios.js` is the starting point for React Native applications on iOS, and it is always required. It can be a small file that `require`s other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in `index.ios.js`.
|
||||
|
||||
```bash
|
||||
# In root of your project
|
||||
$ touch index.ios.js
|
||||
```
|
||||
|
||||
#### Add Your React Native Code
|
||||
##### 2. Add your React Native code
|
||||
|
||||
In your `index.ios.js`, create your component. In our sample here, we will add simple `<Text>` component within a styled `<View>`
|
||||
|
||||
@ -357,19 +353,19 @@ AppRegistry.registerComponent('RNHighScores', () => RNHighScores);
|
||||
|
||||
> `RNHighScores` is the name of your module that will be used when you add a view to React Native from within your iOS application.
|
||||
|
||||
## The Magic: `RCTRootView`
|
||||
#### The Magic: `RCTRootView`
|
||||
|
||||
Now that your React Native component is created via `index.ios.js`, you need to add that component to a new or existing `ViewController`. The easiest path to take is to optionally create an event path to your component and then add that component to an existing `ViewController`.
|
||||
|
||||
We will tie our React Native component with a new native view in the `ViewController` that will actually host it called `RCTRootView` .
|
||||
|
||||
### Create an Event Path
|
||||
##### 1. Create an Event Path
|
||||
|
||||
You can add a new link on the main game menu to go to the "High Score" React Native page.
|
||||
|
||||
![Event Path](img/react-native-add-react-native-integration-link.png)
|
||||
|
||||
#### Event Handler
|
||||
##### 2. Event Handler
|
||||
|
||||
We will now add an event handler from the menu link. A method will be added to the main `ViewController` of your application. This is where `RCTRootView` comes into play.
|
||||
|
||||
@ -450,7 +446,7 @@ import React
|
||||
}
|
||||
```
|
||||
|
||||
> Note that `RCTRootView bundleURL` starts up a new JSC VM. To save resources and simplify the communication between RN views in different parts of your native app, you can have multiple views powered by React Native that are associated with a single JS runtime. To do that, instead of using `RCTRootView bundleURL`, use [`RCTBridge initWithBundleURL`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridge.h#L93) to create a bridge and then use `RCTRootView initWithBridge`.
|
||||
> Note that `RCTRootView bundleURL` starts up a new JSC VM. To save resources and simplify the communication between RN views in different parts of your native app, you can have multiple views powered by React Native that are associated with a single JS runtime. To do that, instead of using `RCTRootView bundleURL`, use [`RCTBridge initWithBundleURL`](https://github.com/facebook/react-native/blob/master/React/Base/RCTBridge.h#L89) to create a bridge and then use `RCTRootView initWithBridge`.
|
||||
|
||||
<block class="objc" />
|
||||
|
||||
@ -462,7 +458,7 @@ import React
|
||||
|
||||
<block class="objc swift" />
|
||||
|
||||
#### Wire Up
|
||||
##### 3. Wire Up
|
||||
|
||||
Wire up the new link in the main menu to the newly added event handler method.
|
||||
|
||||
@ -470,11 +466,11 @@ Wire up the new link in the main menu to the newly added event handler method.
|
||||
|
||||
> One of the easier ways to do this is to open the view in the storyboard and right click on the new link. Select something such as the `Touch Up Inside` event, drag that to the storyboard and then select the created method from the list provided.
|
||||
|
||||
## Test Your Integration
|
||||
### Test your integration
|
||||
|
||||
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.ios.bundle` packager and the server running on `localhost` to serve it.
|
||||
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.ios.bundle` package and the server running on `localhost` to serve it.
|
||||
|
||||
### App Transport Security
|
||||
##### 1. Add App Transport Security exception
|
||||
|
||||
Apple has blocked implicit cleartext HTTP resource loading. So we need to add the following our project's `Info.plist` (or equivalent) file.
|
||||
|
||||
@ -492,14 +488,17 @@ Apple has blocked implicit cleartext HTTP resource loading. So we need to add th
|
||||
</dict>
|
||||
```
|
||||
|
||||
### Run the Packager
|
||||
> App Transport Security is good for your users. Make sure to re-enable it prior to releasing your app for production.
|
||||
|
||||
##### 2. Run the packager
|
||||
|
||||
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:
|
||||
|
||||
```bash
|
||||
# From the root of your project, where the `node_modules` directory is located.
|
||||
$ npm start
|
||||
```
|
||||
|
||||
### Run the App
|
||||
##### 3. Run the app
|
||||
|
||||
If you are using Xcode or your favorite editor, build and run your native iOS application as normal. Alternatively, you can run the app from the command line using:
|
||||
|
||||
@ -524,27 +523,75 @@ Here is the *React Native* high score screen:
|
||||
|
||||
<block class="objc" />
|
||||
|
||||
You can examine the code that added the React Native screen on [GitHub](https://github.com/JoelMarcey/iOS-2048/commit/9ae70c7cdd53eb59f5f7c7daab382b0300ed3585).
|
||||
You can examine the code that added the React Native screen to our sample app on [GitHub](https://github.com/JoelMarcey/iOS-2048/commit/9ae70c7cdd53eb59f5f7c7daab382b0300ed3585).
|
||||
|
||||
<block class="swift" />
|
||||
|
||||
You can examine the code that added the React Native screen on [GitHub](https://github.com/JoelMarcey/swift-2048/commit/13272a31ee6dd46dc68b1dcf4eaf16c1a10f5229).
|
||||
You can examine the code that added the React Native screen to our sample app on [GitHub](https://github.com/JoelMarcey/swift-2048/commit/13272a31ee6dd46dc68b1dcf4eaf16c1a10f5229).
|
||||
|
||||
<block class="android" />
|
||||
|
||||
## Add JS to your app
|
||||
## Adding React Native to your app
|
||||
|
||||
In your app's root folder, run:
|
||||
### Configuring maven
|
||||
|
||||
$ npm init
|
||||
$ npm install --save react react-native
|
||||
$ curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig
|
||||
Add the React Native dependency to your app's `build.gradle` file:
|
||||
|
||||
This creates a node module for your app and adds the `react-native` npm dependency. Now open the newly created `package.json` file and add this under `scripts`:
|
||||
```
|
||||
dependencies {
|
||||
...
|
||||
compile "com.facebook.react:react-native:+" // From node_modules.
|
||||
}
|
||||
```
|
||||
|
||||
"start": "node node_modules/react-native/local-cli/cli.js start"
|
||||
> If you want to ensure that you are always using a specific React Native version in your native build, replace `+` with an actual React Native version you've downloaded from `npm`.
|
||||
|
||||
Copy & paste the following code to `index.android.js` in your root folder — it's a barebones React Native app:
|
||||
Add an entry for the local React Native maven directory to `build.gradle`. Be sure to add it to the "allprojects" block:
|
||||
|
||||
```
|
||||
allprojects {
|
||||
repositories {
|
||||
...
|
||||
maven {
|
||||
// All of React Native (JS, Android binaries) is installed from npm
|
||||
url "$rootDir/node_modules/react-native/android"
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
> Make sure that the path is correct! You shouldn’t run into any “Failed to resolve: com.facebook.react:react-native:0.x.x" errors after running Gradle sync in Android Studio.
|
||||
|
||||
### Configuring permissions
|
||||
|
||||
Next, make sure you have the Internet permission in your `AndroidManifest.xml`:
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
If you need to access to the `DevSettingsActivity` add to your `AndroidManifest.xml`:
|
||||
|
||||
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
|
||||
|
||||
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.
|
||||
|
||||
### Code integration
|
||||
|
||||
Now we will actually modify the native Android application to integrate React Native.
|
||||
|
||||
#### The React Native component
|
||||
|
||||
The first bit of code we will write is the actual React Native code for the new "High Score" screen that will be integrated into our application.
|
||||
|
||||
##### 1. Create a `index.android.js` file
|
||||
|
||||
First, create an empty `index.android.js` file in the root of your React Native project.
|
||||
|
||||
`index.android.js` is the starting point for React Native applications on Android, and it is always required. It can be a small file that `require`s other file that are part of your React Native component or application, or it can contain all the code that is needed for it. In our case, we will just put everything in `index.android.js`.
|
||||
|
||||
##### 2. Add your React Native code
|
||||
|
||||
In your `index.android.js`, create your component. In our sample here, we will add simple `<Text>` component within a styled `<View>`:
|
||||
|
||||
```js
|
||||
'use strict';
|
||||
@ -581,46 +628,36 @@ var styles = StyleSheet.create({
|
||||
AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
|
||||
```
|
||||
|
||||
## Prepare your current app
|
||||
##### 3. Configure permissions for development error overlay
|
||||
|
||||
In your app's `build.gradle` file add the React Native dependency:
|
||||
```
|
||||
dependencies {
|
||||
...
|
||||
compile "com.facebook.react:react-native:+" // From node_modules.
|
||||
If your app is targeting the Android `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.
|
||||
|
||||
```java
|
||||
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()));
|
||||
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> If you want to ensure that you are always using a specific React Native version in your native build, replace `+` with an actual React Native version you've downloaded from `npm`.
|
||||
Finally, the `onActivityResult()` method (as shown in the code below) has to be overridden to handle the permission Accepted or Denied cases for consistent UX.
|
||||
|
||||
In your project's `build.gradle` file add an entry for the local React Native maven directory. Be sure to add it to the "allprojects" block:
|
||||
|
||||
```
|
||||
allprojects {
|
||||
repositories {
|
||||
...
|
||||
maven {
|
||||
// All of React Native (JS, Android binaries) is installed from npm
|
||||
url "$rootDir/node_modules/react-native/android"
|
||||
```java
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (!Settings.canDrawOverlays(this)) {
|
||||
// SYSTEM_ALERT_WINDOW permission not granted...
|
||||
}
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
> Make sure that the path is correct! You shouldn’t run into any “Failed to resolve: com.facebook.react:react-native:0.x.x" errors after running Gradle sync in Android Studio.
|
||||
|
||||
Next, make sure you have the Internet permission in your `AndroidManifest.xml`:
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
If you need to access to the `DevSettingsActivity` add to your `AndroidManifest.xml`:
|
||||
|
||||
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
|
||||
|
||||
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.
|
||||
|
||||
## Add native code
|
||||
#### The Magic: `ReactRootView`
|
||||
|
||||
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 `Activity` that creates a `ReactRootView`, starts a React application inside it and sets it as the main content view.
|
||||
|
||||
@ -668,8 +705,6 @@ We need set the theme of `MyReactActivity` to `Theme.AppCompat.Light.NoActionBar
|
||||
</activity>
|
||||
```
|
||||
|
||||
|
||||
|
||||
> A `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.
|
||||
|
||||
Next, we need to pass some activity lifecycle callbacks down to the `ReactInstanceManager`:
|
||||
@ -733,49 +768,42 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
|
||||
|
||||
Now your activity is ready to run some JavaScript code.
|
||||
|
||||
### Configure permissions for development error overlay
|
||||
### Test your integration
|
||||
|
||||
If your app is targeting the Android `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.
|
||||
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.android.bundle` package and the server running on localhost to serve it.
|
||||
|
||||
```java
|
||||
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()));
|
||||
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
|
||||
}
|
||||
}
|
||||
##### 1. Run the packager
|
||||
|
||||
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:
|
||||
|
||||
```bash
|
||||
$ npm start
|
||||
```
|
||||
|
||||
Finally, the `onActivityResult()` method (as shown in the code below) has to be overridden to handle the permission Accepted or Denied cases for consistent UX.
|
||||
##### 2. Run the app
|
||||
|
||||
```java
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (!Settings.canDrawOverlays(this)) {
|
||||
// SYSTEM_ALERT_WINDOW permission not granted...
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Run your app
|
||||
|
||||
To run your app, you need to first start the development server. To do this, simply run the following command in your root folder:
|
||||
|
||||
$ npm start
|
||||
|
||||
Now build and run your Android app as normal (`./gradlew installDebug` from command-line; in Android Studio just create debug build as usual).
|
||||
|
||||
> If you are using Android Studio for your builds and not the Gradle Wrapper directly, make sure you install [watchman](https://facebook.github.io/watchman/) before running `npm start`. It will prevent the packager from crashing due to conflicts between Android Studio and the React Native packager.
|
||||
Now build and run your Android app as normal.
|
||||
|
||||
Once you reach your React-powered activity inside the app, it should load the JavaScript code from the development server and display:
|
||||
|
||||
![Screenshot](img/EmbeddedAppAndroid.png)
|
||||
|
||||
### Creating a release build in Android Studio
|
||||
|
||||
You can use Android Studio to create your release builds too! It’s as easy as creating release builds of your previously-existing native Android app. There’s just one additional step, which you’ll have to do before every release build. You need to execute the following to create a React Native bundle, which’ll be included with your native Android app:
|
||||
|
||||
$ react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/com/your-company-name/app-package-name/src/main/assets/index.android.bundle --assets-dest android/com/your-company-name/app-package-name/src/main/res/
|
||||
|
||||
Don’t forget to replace the paths with correct ones and create the assets folder if it doesn’t exist!
|
||||
|
||||
Now just create a release build of your native app from within Android Studio as usual and you should be good to go!
|
||||
|
||||
<block class="objc swift android" />
|
||||
|
||||
### Now what?
|
||||
|
||||
At this point you can continue developing your app as usual. Refer to our [debugging](/docs/debugging.html) and [deployment](docs/running-on-device.html) docs to learn more about working with React Native.
|
||||
|
||||
<script>
|
||||
// Convert <div>...<span><block /></span>...</div>
|
||||
// Into <div>...<block />...</div>
|
||||
@ -844,15 +872,3 @@ if (!foundHash) {
|
||||
display('platform', isMac ? 'objc' : 'android');
|
||||
}
|
||||
</script>
|
||||
|
||||
<block class="android" />
|
||||
|
||||
## Creating a release build in Android Studio
|
||||
|
||||
You can use Android Studio to create your release builds too! It’s as easy as creating release builds of your previously-existing native Android app. There’s just one additional step, which you’ll have to do before every release build. You need to execute the following to create a React Native bundle, which’ll be included with your native Android app:
|
||||
|
||||
$ react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/com/your-company-name/app-package-name/src/main/assets/index.android.bundle --assets-dest android/com/your-company-name/app-package-name/src/main/res/
|
||||
|
||||
Don’t forget to replace the paths with correct ones and create the assets folder if it doesn’t exist!
|
||||
|
||||
Now just create a release build of your native app from within Android Studio as usual and you should be good to go!
|
||||
|
Loading…
x
Reference in New Issue
Block a user