react-native-navigation的迁移库

docs-Installing.mdx 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. ---
  2. id: installing
  3. title: Installation
  4. sidebar_label: Installation
  5. ---
  6. ## Requirements
  7. * node >= 8
  8. * react-native >= 0.51
  9. ## npm
  10. * `npm install --save react-native-navigation`
  11. ## Installing with `react-native link`
  12. If you're using RN 0.60 or higher, you can link RNN automatically with react-native link.
  13. Unlike most other libraries, react-native-navigation requires you to make a few changes to native files. To make all the necessary changes, run
  14. ```react-native link react-native-navigation```
  15. in your project's root folder. Make sure to commit the changes introduced by the link script.
  16. If the link script completed successfully, you're good to go! If one of the steps failed, you'll need to complete the relevant step in the manual installation steps bellow.
  17. ## Displaying the app root
  18. ### Update index.js file
  19. `index.js` is typically used as an entry point into the app. It's first parsed and executed by the JS engine, therefore we'll want to show our UI from there.
  20. The following diff demonstrates changes needed to be made to `index.js`, initialized by `react-native init`.
  21. ```diff
  22. +import { Navigation } from "react-native-navigation";
  23. -import {AppRegistry} from 'react-native';
  24. import App from "./App";
  25. -import {name as appName} from './app.json';
  26. -AppRegistry.registerComponent(appName, () => App);
  27. +Navigation.registerComponent('com.myApp.WelcomeScreen', () => App);
  28. +Navigation.events().registerAppLaunchedListener(() => {
  29. + Navigation.setRoot({
  30. + root: {
  31. + stack: {
  32. + children: [
  33. + component: {
  34. + name: 'com.myApp.WelcomeScreen'
  35. + }
  36. + ]
  37. + }
  38. + }
  39. + });
  40. +});
  41. ```
  42. ___
  43. ## Manual Installation
  44. If installation with react-native link did not work, follow the manual installation steps below.
  45. ### iOS
  46. > Make sure your Xcode is updated. We recommend editing `.h` and `.m` files in Xcode as the IDE will usually point out common errors.
  47. #### Installation with CocoaPods
  48. Projects generated using newer versions of react-native use CocoaPods by default. In that case it's easier to install react-native-navigation using CocoaPods.
  49. 1. Update your `Podfile`:
  50. **If you're upgrading to v5 from a previous RNN version**, make sure to remove manual linking of RNN
  51. ```diff
  52. platform :ios, '9.0'
  53. require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
  54. target 'YourApp' do
  55. # Pods for YourApp
  56. pod 'React', :path => '../node_modules/react-native/'
  57. pod 'React-Core', :path => '../node_modules/react-native/React'
  58. pod 'React-DevSupport', :path => '../node_modules/react-native/React'
  59. pod 'React-fishhook', :path => '../node_modules/react-native/Libraries/fishhook'
  60. pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
  61. pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
  62. pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
  63. pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
  64. pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
  65. pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
  66. pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
  67. pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
  68. pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
  69. pod 'React-RCTWebSocket', :path => '../node_modules/react-native/Libraries/WebSocket'
  70. pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
  71. pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
  72. pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
  73. pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
  74. pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
  75. pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  76. pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
  77. pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
  78. - pod 'ReactNativeNavigation', :podspec => '../node_modules/react-native-navigation/ReactNativeNavigation.podspec'
  79. use_native_modules!
  80. end
  81. ```
  82. 2. `cd ios && pod install`
  83. #### Native Installation
  84. If all else fails, we can always try and install the hardcore way:
  85. 1. In Xcode, in Project Navigator (left pane), right-click on the `Libraries` > `Add files to [project name]`. Add `node_modules/react-native-navigation/lib/ios/ReactNativeNavigation.xcodeproj` ([screenshots](https://facebook.github.io/react-native/docs/linking-libraries-ios.html#manual-linking)).
  86. 2. In Xcode, in Project Navigator (left pane), click on your project (top), then click on your *target* row (on the "project and targets list", which is on the left column of the right pane) and select the `Build Phases` tab (right pane). In the `Link Binary With Libraries` section add `libReactNativeNavigation.a` ([screenshots](https://facebook.github.io/react-native/docs/linking-libraries-ios.html#step-2)).
  87. a. If you're seeing an error message in Xcode such as:
  88. ```
  89. 'ReactNativeNavigation/ReactNativeNavigation.h' file not found.
  90. ```
  91. You may also need to add a Header Search Path: ([screenshots](https://facebook.github.io/react-native/docs/linking-libraries-ios.html#step-3)).
  92. ```objectivec
  93. $(SRCROOT)/../node_modules/react-native-navigation/lib/ios
  94. ```
  95. 3. In Xcode, you will need to edit this file: `AppDelegate.m`. This function is the main entry point for your app:
  96. ```objectivec
  97. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... }
  98. ```
  99. Its content should look like this:
  100. ```objectivec
  101. #import "AppDelegate.h"
  102. #import <React/RCTBundleURLProvider.h>
  103. #import <React/RCTRootView.h>
  104. #import <ReactNativeNavigation/ReactNativeNavigation.h>
  105. @implementation AppDelegate
  106. -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  107. {
  108. NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  109. [ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];
  110. return YES;
  111. }
  112. @end
  113. ```
  114. a. If, in Xcode, you see the following error message in `AppDelegate.m` next to `#import "RCTBundleURLProvider.h"`:
  115. ```
  116. ! 'RCTBundleURLProvider.h' file not found
  117. ```
  118. This is because the `React` scheme is missing from your project. You can verify this by opening the `Product` menu and the `Scheme` submenu.
  119. To make the `React` scheme available to your project, run `npm install -g react-native-git-upgrade` followed by `react-native-git-upgrade`. Once this is done, you can click back to the menu in Xcode: `Product -> Scheme -> Manage Schemes`, then click '+' to add a new scheme. From the `Target` menu, select "React", and click the checkbox to make the scheme `shared`. This should make the error disappear.
  120. b. If, in Xcode, you see the following warning message in `AppDelegate.m` next to `#import "@implementation AppDelegate"`:
  121. ```
  122. Class 'AppDelegate' does not conform to protocol 'RCTBridgeDelegate'
  123. ```
  124. You can remove `RCTBridgeDelegate` from this file: `AppDelegate.h`:
  125. ```diff
  126. - #import <React/RCTBridgeDelegate.h>
  127. - @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
  128. + @interface AppDelegate : UIResponder <UIApplicationDelegate>
  129. ...
  130. ```
  131. ### Android
  132. > Make sure your Android Studio installation is up to date. We recommend editing `gradle` and `java` files in Android Studio as the IDE will suggest fixes and point out errors, this way you avoid most common pitfalls.
  133. #### 1 Update `android/build.gradle`:
  134. ```diff
  135. buildscript {
  136. ext {
  137. - minSdkVersion = 16
  138. + minSdkVersion = 19 // Or higher
  139. compileSdkVersion = 26
  140. targetSdkVersion = 26
  141. supportLibVersion = "26.1.0"
  142. + RNNKotlinVersion = "1.3.61" // Or any version above 1.3.x
  143. + RNNKotlinStdlib = "kotlin-stdlib-jdk8"
  144. }
  145. repositories {
  146. google()
  147. jcenter()
  148. + mavenLocal()
  149. + mavenCentral()
  150. }
  151. dependencies {
  152. + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61" // Or whatever Kotlin version you've specified above
  153. + classpath 'com.android.tools.build:gradle:3.5.3' // Or higher
  154. - classpath 'com.android.tools.build:gradle:2.2.3'
  155. }
  156. }
  157. allprojects {
  158. repositories {
  159. + google()
  160. mavenLocal()
  161. jcenter()
  162. maven {
  163. // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
  164. url "$rootDir/../node_modules/react-native/android"
  165. }
  166. - maven {
  167. - url 'https://maven.google.com/'
  168. - name 'Google'
  169. - }
  170. + maven { url 'https://jitpack.io' }
  171. }
  172. }
  173. ```
  174. #### 2 Update `MainActivity.java`
  175. `MainActivity.java` should extend `com.reactnativenavigation.NavigationActivity` instead of `ReactActivity`.
  176. This file is located in `android/app/src/main/java/com/<yourproject>/MainActivity.java`.
  177. ```diff
  178. -import com.facebook.react.ReactActivity;
  179. +import com.reactnativenavigation.NavigationActivity;
  180. -public class MainActivity extends ReactActivity {
  181. +public class MainActivity extends NavigationActivity {
  182. - @Override
  183. - protected String getMainComponentName() {
  184. - return "yourproject";
  185. - }
  186. }
  187. ```
  188. If you have any **react-native** related methods, you can safely delete them.
  189. #### 3 Update `MainApplication.java`
  190. This file is located in `android/app/src/main/java/com/<yourproject>/MainApplication.java`.
  191. ```diff
  192. ...
  193. import android.app.Application;
  194. import com.facebook.react.ReactApplication;
  195. import com.facebook.react.ReactNativeHost;
  196. import com.facebook.react.ReactPackage;
  197. import com.facebook.react.shell.MainReactPackage;
  198. import com.facebook.soloader.SoLoader;
  199. +import com.reactnativenavigation.NavigationApplication;
  200. +import com.reactnativenavigation.react.NavigationReactNativeHost;
  201. -public class MainApplication extends Application implements ReactApplication {
  202. +public class MainApplication extends NavigationApplication {
  203. private final ReactNativeHost mReactNativeHost =
  204. - new ReactNativeHost(this) {
  205. + new NavigationReactNativeHost(this) {
  206. @Override
  207. public boolean getUseDeveloperSupport() {
  208. return BuildConfig.DEBUG;
  209. }
  210. @Override
  211. protected List<ReactPackage> getPackages() {
  212. @SuppressWarnings("UnnecessaryLocalVariable")
  213. List<ReactPackage> packages = new PackageList(this).getPackages();
  214. // Packages that cannot be autolinked yet can be added manually here, for example:
  215. // packages.add(new MyReactNativePackage());
  216. return packages;
  217. }
  218. @Override
  219. protected String getJSMainModuleName() {
  220. return "index";
  221. }
  222. };
  223. @Override
  224. public ReactNativeHost getReactNativeHost() {
  225. return mReactNativeHost;
  226. }
  227. @Override
  228. public void onCreate() {
  229. super.onCreate();
  230. - SoLoader.init(this, /* native exopackage */ false);
  231. initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  232. }
  233. }
  234. ```
  235. ### App root
  236. Now that you're done, don't forget to update the `index.js` file, as [shown above](#update-indexjs-file).
  237. ## Troubleshooting
  238. ### Build app with gradle command
  239. **prefered solution** - The RNN flavor you would like to build is specified in `app/build.gradle`. Therefore in order to compile only that flavor, instead of building your entire project using `./gradlew assembleDebug`, you should instruct gradle to build the app module: `./gradlew app:assembleDebug`. The easiest way is to add a package.json command to build and install your debug Android APK .
  240. ```
  241. "scripts": {
  242. ...
  243. "android": "cd ./android && ./gradlew app:assembleDebug && ./gradlew installDebug"
  244. }
  245. ```
  246. Now run `npm run android` to build your application
  247. ### Force the same support library version across all dependencies
  248. Some of your dependencies might require a different version of one of Google's support library packages. This results in compilation errors similar to this:
  249. ```
  250. FAILURE: Build failed with an exception.
  251. * What went wrong:
  252. Execution failed for task ':app:preDebugBuild'.
  253. > Android dependency 'com.android.support:design' has different version for the compile (25.4.0) and runtime (26.1.0) classpath. You should manually set the same version via DependencyResolution
  254. ```
  255. To resolve these conflicts, add the following to your `app/build.gradle`:
  256. ```groovy
  257. android {
  258. ...
  259. }
  260. configurations.all {
  261. resolutionStrategy.eachDependency { DependencyResolveDetails details ->
  262. def requested = details.requested
  263. if (requested.group == 'com.android.support' && requested.name != 'multidex') {
  264. details.useVersion "${rootProject.ext.supportLibVersion}"
  265. }
  266. }
  267. }
  268. dependencies {
  269. ...
  270. implementation 'com.android.support:design:25.4.0'
  271. implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
  272. }
  273. ```