react-native-navigation的迁移库

Installing.mdx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. + component: {
  33. + name: 'com.myApp.WelcomeScreen'
  34. + }
  35. + }
  36. + }
  37. + });
  38. +});
  39. ```
  40. ⚠️ we use the layout type `component` here, which renders a React component but does not allow you to navigate to others. See [Usage](docs/Usage.md) and [LayoutTypes](docs/layout-types.md) for more options.
  41. ___
  42. ## Manual Installation
  43. If installation with react-native link did not work, follow the manual installation steps below.
  44. ### iOS
  45. > Make sure your Xcode is updated. We recommend editing `.h` and `.m` files in Xcode as the IDE will usually point out common errors.
  46. #### Installation with CocoaPods
  47. 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.
  48. 1. Update your `Podfile`:
  49. **If you're upgrading to v5 from a previous RNN version**, make sure to remove manual linking of RNN
  50. ```diff
  51. platform :ios, '9.0'
  52. require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
  53. target 'YourApp' do
  54. # Pods for YourApp
  55. pod 'React', :path => '../node_modules/react-native/'
  56. pod 'React-Core', :path => '../node_modules/react-native/React'
  57. pod 'React-DevSupport', :path => '../node_modules/react-native/React'
  58. pod 'React-fishhook', :path => '../node_modules/react-native/Libraries/fishhook'
  59. pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
  60. pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
  61. pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
  62. pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
  63. pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
  64. pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
  65. pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
  66. pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
  67. pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
  68. pod 'React-RCTWebSocket', :path => '../node_modules/react-native/Libraries/WebSocket'
  69. pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
  70. pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
  71. pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
  72. pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
  73. pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
  74. pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  75. pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
  76. pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
  77. - pod 'ReactNativeNavigation', :podspec => '../node_modules/react-native-navigation/ReactNativeNavigation.podspec'
  78. use_native_modules!
  79. end
  80. ```
  81. 2. `cd ios && pod install`
  82. #### Native Installation
  83. If all else fails, we can always try and install the hardcore way:
  84. 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)).
  85. 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)).
  86. a. If you're seeing an error message in Xcode such as:
  87. ```
  88. 'ReactNativeNavigation/ReactNativeNavigation.h' file not found.
  89. ```
  90. You may also need to add a Header Search Path: ([screenshots](https://facebook.github.io/react-native/docs/linking-libraries-ios.html#step-3)).
  91. ```objectivec
  92. $(SRCROOT)/../node_modules/react-native-navigation/lib/ios
  93. ```
  94. 3. In Xcode, you will need to edit this file: `AppDelegate.m`. This function is the main entry point for your app:
  95. ```objectivec
  96. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... }
  97. ```
  98. Its content should look like this:
  99. ```objectivec
  100. #import "AppDelegate.h"
  101. #import <React/RCTBundleURLProvider.h>
  102. #import <React/RCTRootView.h>
  103. #import <ReactNativeNavigation/ReactNativeNavigation.h>
  104. @implementation AppDelegate
  105. -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  106. {
  107. NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  108. [ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];
  109. return YES;
  110. }
  111. @end
  112. ```
  113. a. If, in Xcode, you see the following error message in `AppDelegate.m` next to `#import "RCTBundleURLProvider.h"`:
  114. ```
  115. ! 'RCTBundleURLProvider.h' file not found
  116. ```
  117. This is because the `React` scheme is missing from your project. You can verify this by opening the `Product` menu and the `Scheme` submenu.
  118. 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.
  119. b. If, in Xcode, you see the following warning message in `AppDelegate.m` next to `#import "@implementation AppDelegate"`:
  120. ```
  121. Class 'AppDelegate' does not conform to protocol 'RCTBridgeDelegate'
  122. ```
  123. You can remove `RCTBridgeDelegate` from this file: `AppDelegate.h`:
  124. ```diff
  125. - #import <React/RCTBridgeDelegate.h>
  126. - @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
  127. + @interface AppDelegate : UIResponder <UIApplicationDelegate>
  128. ...
  129. ```
  130. ### Android
  131. > 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.
  132. #### 1 Update `android/build.gradle`:
  133. ```diff
  134. buildscript {
  135. ext {
  136. - minSdkVersion = 16
  137. + minSdkVersion = 19 // Or higher
  138. compileSdkVersion = 26
  139. targetSdkVersion = 26
  140. supportLibVersion = "26.1.0"
  141. + RNNKotlinVersion = "1.3.61" // Or any version above 1.3.x
  142. + RNNKotlinStdlib = "kotlin-stdlib-jdk8"
  143. }
  144. repositories {
  145. google()
  146. jcenter()
  147. + mavenLocal()
  148. + mavenCentral()
  149. }
  150. dependencies {
  151. + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61" // Or whatever Kotlin version you've specified above
  152. + classpath 'com.android.tools.build:gradle:3.5.3' // Or higher
  153. - classpath 'com.android.tools.build:gradle:2.2.3'
  154. }
  155. }
  156. allprojects {
  157. repositories {
  158. + google()
  159. mavenLocal()
  160. jcenter()
  161. maven {
  162. // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
  163. url "$rootDir/../node_modules/react-native/android"
  164. }
  165. - maven {
  166. - url 'https://maven.google.com/'
  167. - name 'Google'
  168. - }
  169. + maven { url 'https://jitpack.io' }
  170. }
  171. }
  172. ```
  173. #### 2 Update `MainActivity.java`
  174. `MainActivity.java` should extend `com.reactnativenavigation.NavigationActivity` instead of `ReactActivity`.
  175. This file is located in `android/app/src/main/java/com/<yourproject>/MainActivity.java`.
  176. ```diff
  177. -import com.facebook.react.ReactActivity;
  178. +import com.reactnativenavigation.NavigationActivity;
  179. -public class MainActivity extends ReactActivity {
  180. +public class MainActivity extends NavigationActivity {
  181. - @Override
  182. - protected String getMainComponentName() {
  183. - return "yourproject";
  184. - }
  185. }
  186. ```
  187. If you have any **react-native** related methods, you can safely delete them.
  188. #### 3 Update `MainApplication.java`
  189. This file is located in `android/app/src/main/java/com/<yourproject>/MainApplication.java`.
  190. ```diff
  191. ...
  192. import android.app.Application;
  193. import com.facebook.react.ReactApplication;
  194. import com.facebook.react.ReactNativeHost;
  195. import com.facebook.react.ReactPackage;
  196. import com.facebook.react.shell.MainReactPackage;
  197. import com.facebook.soloader.SoLoader;
  198. +import com.reactnativenavigation.NavigationApplication;
  199. +import com.reactnativenavigation.react.NavigationReactNativeHost;
  200. -public class MainApplication extends Application implements ReactApplication {
  201. +public class MainApplication extends NavigationApplication {
  202. private final ReactNativeHost mReactNativeHost =
  203. - new ReactNativeHost(this) {
  204. + new NavigationReactNativeHost(this) {
  205. @Override
  206. public boolean getUseDeveloperSupport() {
  207. return BuildConfig.DEBUG;
  208. }
  209. @Override
  210. protected List<ReactPackage> getPackages() {
  211. @SuppressWarnings("UnnecessaryLocalVariable")
  212. List<ReactPackage> packages = new PackageList(this).getPackages();
  213. // Packages that cannot be autolinked yet can be added manually here, for example:
  214. // packages.add(new MyReactNativePackage());
  215. return packages;
  216. }
  217. @Override
  218. protected String getJSMainModuleName() {
  219. return "index";
  220. }
  221. };
  222. @Override
  223. public ReactNativeHost getReactNativeHost() {
  224. return mReactNativeHost;
  225. }
  226. @Override
  227. public void onCreate() {
  228. super.onCreate();
  229. - SoLoader.init(this, /* native exopackage */ false);
  230. initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  231. }
  232. }
  233. ```
  234. ### App root
  235. Now that you're done, don't forget to update the `index.js` file, as [shown above](#update-indexjs-file).
  236. ## Troubleshooting
  237. ### Build app with gradle command
  238. **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 .
  239. ```
  240. "scripts": {
  241. ...
  242. "android": "cd ./android && ./gradlew app:assembleDebug && ./gradlew installDebug"
  243. }
  244. ```
  245. Now run `npm run android` to build your application
  246. ### Ignore other RNN flavors
  247. If you don't want to run `npm run android` and want to keep the default `react-native run-android` command, you need to instruct gradle to ignore the other flavors RNN provides.
  248. To do so edit `android/build.gradle` and add:
  249. ```diff
  250. +subprojects { subproject ->
  251. + afterEvaluate {
  252. + if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
  253. + android {
  254. + variantFilter { variant ->
  255. + def names = variant.flavors*.name
  256. + if (names.contains("reactNative51") || names.contains("reactNative55")) {
  257. + setIgnore(true)
  258. + }
  259. + }
  260. + }
  261. + }
  262. + }
  263. +}
  264. ```
  265. **Note**: As more build variants become available in the future, you will need to adjust the list (`names.contains("reactNative51") || names.contains("reactNative55")`). This is why we recommend the first solution.
  266. ### Force the same support library version across all dependencies
  267. 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:
  268. ```
  269. FAILURE: Build failed with an exception.
  270. * What went wrong:
  271. Execution failed for task ':app:preDebugBuild'.
  272. > 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
  273. ```
  274. To resolve these conflicts, add the following to your `app/build.gradle`:
  275. ```groovy
  276. android {
  277. ...
  278. }
  279. configurations.all {
  280. resolutionStrategy.eachDependency { DependencyResolveDetails details ->
  281. def requested = details.requested
  282. if (requested.group == 'com.android.support' && requested.name != 'multidex') {
  283. details.useVersion "${rootProject.ext.supportLibVersion}"
  284. }
  285. }
  286. }
  287. dependencies {
  288. ...
  289. implementation 'com.android.support:design:25.4.0'
  290. implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
  291. }
  292. ```