Browse Source

Update Installing.md (#3727)

Target react-native 0.56 version
Fix `MainActivity.java` and `MainApplication.java` with a working solution
Guillaume Vincent 6 years ago
parent
commit
c0d3b1709c
1 changed files with 203 additions and 105 deletions
  1. 203
    105
      docs/docs/Installing.md

+ 203
- 105
docs/docs/Installing.md View File

@@ -53,23 +53,19 @@
53 53
 include ':react-native-navigation'
54 54
 project(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/lib/android/app/')
55 55
 ```
56
-## 2. Updating Gradle build files.
57
-
58
-> react-native-navigation supports multiple React Native versions. Target the React Native version required by your project by specifying the RNN build flavor you require.
59
-><br><br>Available options:
60
->
61
->* `reactNative51`: Support for React Native 0.51-0.54
62
->* `reactNative55`: Support for React Native 0.55
63
->* `reactNative56`: Support for React Native 0.56 and above
64
->
65
-><br>For example: To target React Native 0.56
66
-> <br><br>Replace the following line in step 2.2:<br>
67
->`missingDimensionStrategy "RNN.reactNativeVersion", "reactNative51"`
68
-><br>with:<br>
69
->`missingDimensionStrategy "RNN.reactNativeVersion", "reactNative56"`
70
-><br><br>Make a choice in step 2.3 to alter your Android run-command (**prefered**) or ignore the unused buildvariants in your root build.gradle file.
71
-
72
-### 2.1 Update `android/build.gradle`:
56
+
57
+### 2. Make sure you're using the new gradle plugin, edit `android/gradle/wrapper/gradle-wrapper.properties`
58
+
59
+```diff
60
+distributionBase=GRADLE_USER_HOME
61
+distributionPath=wrapper/dists
62
+zipStoreBase=GRADLE_USER_HOME
63
+zipStorePath=wrapper/dists
64
++distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
65
+-distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
66
+```
67
+
68
+### 3 Update `android/build.gradle`:
73 69
 
74 70
 ```diff
75 71
 buildscript {
@@ -78,6 +74,10 @@ buildscript {
78 74
 +        mavenLocal()
79 75
 +        mavenCentral()
80 76
 +        jcenter()
77
+-        maven {
78
+-            url 'https://maven.google.com/'
79
+-            name 'Google'
80
+-        }
81 81
 	}
82 82
 	dependencies {
83 83
 +        classpath 'com.android.tools.build:gradle:3.0.1'
@@ -89,142 +89,216 @@ allprojects {
89 89
 	repositories {
90 90
 +		google()
91 91
 +		mavenCentral()
92
-+		mavenLocal()
92
+		mavenLocal()
93 93
 		jcenter()
94 94
 		maven {
95 95
 			// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
96 96
 			url "$rootDir/../node_modules/react-native/android"
97 97
 		}
98
+-        maven {
99
+-            url 'https://maven.google.com/'
100
+-            name 'Google'
101
+-        }
98 102
 +		maven { url 'https://jitpack.io' }
99 103
 	}
100 104
 }
101 105
 
106
+ext {
107
+-    buildToolsVersion = "26.0.3"
108
++    buildToolsVersion = "27.0.3"
109
+-    minSdkVersion = 16
110
++    minSdkVersion = 19
111
+    compileSdkVersion = 26
112
+    targetSdkVersion = 26
113
+    supportLibVersion = "26.1.0"
114
+}
115
+
102 116
 
103 117
 ```
104 118
 
105
-### 2.2 Update project dependencies in `android/app/build.gradle`.
119
+### 4 Update project dependencies in `android/app/build.gradle`.
106 120
 
107
-```groovy
121
+```diff
108 122
 android {
109
-	compileSdkVersion 25
110
-	buildToolsVersion "27.0.3"
111
-	
112
-	defaultConfig {
113
-		minSdkVersion 19
114
-		targetSdkVersion 25
115
-		missingDimensionStrategy "RNN.reactNativeVersion", "reactNative51" // <- See note above for further instruction regarding compatibility with other React Native versions
116
-	...
117
-	}
118
-
119
-	compileOptions {
120
-		sourceCompatibility JavaVersion.VERSION_1_8
121
-		targetCompatibility JavaVersion.VERSION_1_8
122
-	}
123
+    compileSdkVersion rootProject.ext.compileSdkVersion
124
+    buildToolsVersion rootProject.ext.buildToolsVersion
125
+
126
+    defaultConfig {
127
+        applicationId "com.yourproject"
128
+        minSdkVersion rootProject.ext.minSdkVersion
129
+        targetSdkVersion rootProject.ext.targetSdkVersion
130
+        versionCode 1
131
+        versionName "1.0"
132
+        ndk {
133
+            abiFilters "armeabi-v7a", "x86"
134
+        }
135
+    }
136
++	compileOptions {
137
++		sourceCompatibility JavaVersion.VERSION_1_8
138
++		targetCompatibility JavaVersion.VERSION_1_8
139
++	}
123 140
 	...
124 141
 }
125 142
 
126 143
 dependencies {
127
-	implementation fileTree(dir: "libs", include: ["*.jar"])
128
-	implementation "com.android.support:appcompat-v7:25.4.0"
129
-	implementation "com.facebook.react:react-native:+"
130
-	implementation project(':react-native-navigation')
144
+-    compile fileTree(dir: "libs", include: ["*.jar"])
145
+-    compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
146
+-    compile "com.facebook.react:react-native:+"  // From node_modules
147
++    implementation fileTree(dir: "libs", include: ["*.jar"])
148
++    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
149
++    implementation "com.facebook.react:react-native:+"  // From node_modules
150
++    implementation project(':react-native-navigation')
131 151
 }
132 152
 ```
133 153
 
134
-### 2.3 Compiling the correct build flavor
135
-Now that you've specified the RNN flavor you need to compile acording to the installed React Native version, you need to ignore the other flavors RNN provides.
154
+### 5. Update `gradle.properties` and disable incremental resource processing
136 155
 
137
-1. **The preferable option**: Use the configuration specified in the `app` module. 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:asembleDebug`. The easiest way is to add a package.json command to build and install your debug Android APK since the RN-CLI run-android command will no longer be of use.
156
+```diff
157
++# Disable incremental resource processing as it broke release build
158
++android.enableAapt2=false
138 159
 ```
139
-"scripts": {
140
-  "run-android": "cd ./android && ./gradlew app:assembleDebug && ./gradlew installDebug"
160
+
161
+### 6. Update `MainActivity.java`
162
+
163
+`MainActivity.java` should extend `com.reactnativenavigation.NavigationActivity` instead of `ReactActivity`.
164
+
165
+This file is located in `android/app/src/main/java/com/<yourproject>/MainActivity.java`.
166
+
167
+```diff
168
+-import com.facebook.react.ReactActivity;
169
++import com.reactnativenavigation.NavigationActivity;
170
+
171
+-public class MainActivity extends ReactActivity { 
172
++public class MainActivity extends NavigationActivity {
173
+-    @Override
174
+-    protected String getMainComponentName() {
175
+-        return "yourproject";
176
+-    }
141 177
 }
142 178
 ```
143 179
 
180
+If you have any **react-native** related methods, you can safely delete them.
181
+
182
+### 7. Update `MainApplication.java`
144 183
 
145
-2. Alternatively, explicitly ignore unwanted flavors in your project's root `build.gradle`. (Note: As more build variants come available in the future, you will need to adjust this list. While this option lets you keep the CLI command ```react-native run-android``` it comes at a cost of future upkeep.)
184
+This file is located in `android/app/src/main/java/com/<yourproject>/MainApplication.java`.
185
+	
146 186
 ```diff
147
-+subprojects { subproject ->
148
-+    afterEvaluate {
149
-+        if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
150
-+            android {
151
-+                variantFilter { variant ->
152
-+                    def names = variant.flavors*.name
153
-+                    if (names.contains("reactNative51") || names.contains("reactNative55")) { // <- See note above for further instruction regarding compatibility with other React Native versions
154
-+                        // Gradle ignores any variants that satisfy the conditions above.
155
-+                        setIgnore(true)
156
-+                    }
157
-+                }
187
+...
188
+import android.app.Application;
189
+
190
+import com.facebook.react.ReactApplication;
191
+import com.facebook.react.ReactNativeHost;
192
+import com.facebook.react.ReactPackage;
193
+import com.facebook.react.shell.MainReactPackage;
194
+import com.facebook.soloader.SoLoader;
195
+
196
++import com.reactnativenavigation.NavigationApplication;
197
++import com.reactnativenavigation.react.NavigationReactNativeHost;
198
++import com.reactnativenavigation.react.ReactGateway;
199
+
200
+import java.util.Arrays;
201
+import java.util.List;
202
+
203
+-public class MainApplication extends Application implements ReactApplication {
204
++public class MainApplication extends NavigationApplication {
205
++    
206
++    @Override
207
++    protected ReactGateway createReactGateway() {
208
++        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
209
++            @Override
210
++            protected String getJSMainModuleName() {
211
++                return "index";
158 212
 +            }
159
-+        }
213
++        };
214
++        return new ReactGateway(this, isDebug(), host);
215
++    }
216
++
217
++    @Override
218
++    public boolean isDebug() {
219
++        return BuildConfig.DEBUG;
160 220
 +    }
221
++
222
++    protected List<ReactPackage> getPackages() {
223
++        // Add additional packages you require here
224
++        // No need to add RnnPackage and MainReactPackage
225
++        return Arrays.<ReactPackage>asList(
226
++            // eg. new VectorIconsPackage()
227
++        );
228
++    }
229
++  
230
++    @Override
231
++    public List<ReactPackage> createAdditionalReactPackages() {
232
++        return getPackages();
233
++    }
234
+- ...
161 235
 +}
162
-```
163 236
 
164
-### 3. Make sure you're using the new gradle plugin, edit `android/gradle/wrapper/gradle-wrapper.properties`
165
-
166
-```diff
167
-distributionBase=GRADLE_USER_HOME
168
-distributionPath=wrapper/dists
169
-zipStoreBase=GRADLE_USER_HOME
170
-zipStorePath=wrapper/dists
171
-+distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
172
--distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
173 237
 ```
174 238
 
175
-### 4. Update `gradle.properties` and disable incremental resource processing
239
+### 8 RNN and React Native version
240
+
241
+react-native-navigation supports multiple React Native versions. Target the React Native version required by your project by specifying the RNN build flavor in `app/build.gradle`.
176 242
 
177 243
 ```diff
178
-+# Disable incremental resource processing as it broke release build
179
-+android.enableAapt2=false
244
+android {
245
+    ...
246
+    defaultConfig {
247
+        applicationId "com.yourproject"
248
+        minSdkVersion rootProject.ext.minSdkVersion
249
+        targetSdkVersion rootProject.ext.targetSdkVersion
250
++        missingDimensionStrategy "RNN.reactNativeVersion", "reactNative56"
251
+        versionCode 1
252
+        versionName "1.0"
253
+        ...
254
+    }
255
+    ...
256
+}
180 257
 ```
181 258
 
182
-### 5. Update `MainActivity.java`
259
+RNN only support react-native 0.51 (`"reactNative51"`), 0.55 (`"reactNative55"`), and O.56 (`"reactNative56"`),
183 260
 
184
-`MainActivity.java` should extend `com.reactnativenavigation.NavigationActivity` instead of `ReactActivity`.
185
-
186
-This file can be located in `android/app/src/main/java/com/yourproject/`.
261
+Now we need to instruct gradle how to build that flavor. To do so here two solutions :
187 262
 
188
-```java
189
-import com.reactnativenavigation.NavigationActivity;
263
+#### 8.1 Build app with gradle command 
190 264
 
191
-public class MainActivity extends NavigationActivity {
265
+**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:asembleDebug`. The easiest way is to add a package.json command to build and install your debug Android APK .
192 266
 
267
+```
268
+"scripts": {
269
+  ...
270
+  "android": "cd ./android && ./gradlew app:assembleDebug && ./gradlew installDebug"
193 271
 }
194 272
 ```
195 273
 
196
-If you have any **react-native** related methods, you can safely delete them.
274
+No run `npm run android` to build your application
197 275
 
198
-### 6. In `MainApplication.java`, add the following
199
-	
200
-```java
201
-import com.reactnativenavigation.NavigationApplication;
276
+#### 8.2 Ignore other RNN flavors
202 277
 
203
-public class MainApplication extends NavigationApplication {
204
-	@Override
205
-	public boolean isDebug() {
206
-		return BuildConfig.DEBUG;
207
-	}
278
+If you don't want to run `npm run android` and want to keep the default `react-native run-android` command, you need to specify to graddle to ignore the other flavors RNN provides.
208 279
 
209
-	@Override
210
-	public List<ReactPackage> createAdditionalReactPackages() {
211
-		return Arrays.<ReactPackage>asList(
212
-			// eg. new VectorIconsPackage()
213
-		);
214
-	}
215
-}
280
+To do so edit `android/build.gradle` and add:
281
+
282
+```diff
283
++subprojects { subproject ->
284
++    afterEvaluate {
285
++        if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
286
++            android {
287
++                variantFilter { variant ->
288
++                    def names = variant.flavors*.name
289
++                    if (names.contains("reactNative51") || names.contains("reactNative55")) {
290
++                        setIgnore(true)
291
++                    }
292
++                }
293
++            }
294
++        }
295
++    }
296
++}
216 297
 ```
217
-Make sure that `isDebug` method is implemented.
218 298
 
219
-### 7. Update `AndroidManifest.xml` and set `application` **android:name** value to `.MainApplication`
299
+**Note**: As more build variants come 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.
220 300
 
221
-```xml
222
-<application
223
-    android:name=".MainApplication"
224
-    ...
225
-/>
226
-```
227
-### 8. Force the same support library version across all dependencies
301
+### 9. Force the same support library version across all dependencies (optional)
228 302
 
229 303
 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:
230 304
 
@@ -247,7 +321,7 @@ configurations.all {
247 321
     resolutionStrategy.eachDependency { DependencyResolveDetails details ->
248 322
         def requested = details.requested
249 323
         if (requested.group == 'com.android.support' && requested.name != 'multidex') {
250
-            details.useVersion "25.4.0" // <- Change this to whatever version you're using
324
+            details.useVersion "${rootProject.ext.supportLibVersion}"
251 325
         }
252 326
     }
253 327
 }
@@ -255,7 +329,31 @@ configurations.all {
255 329
 dependencies {
256 330
     ...
257 331
     implementation 'com.android.support:design:25.4.0'
258
-    implementation 'com.android.support:appcompat-v7:25.4.0'
332
+    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
259 333
 }
260 334
 
261
-```
335
+```
336
+
337
+## You can use react-native-navigation \o/
338
+
339
+Update `index.js` file
340
+
341
+
342
+```diff
343
++import { Navigation } from "react-native-navigation";
344
+-import {AppRegistry} from 'react-native';
345
+import App from "./App";
346
+-import {name as appName} from './app.json';
347
+
348
+-AppRegistry.registerComponent(appName, () => App);
349
++Navigation.registerComponent(`navigation.playground.WelcomeScreen`, () => App);
350
+
351
++Navigation.events().registerAppLaunchedListener(() => {
352
++  Navigation.setRoot({
353
++    root: {
354
++      component: {
355
++        name: "navigation.playground.WelcomeScreen"
356
++      }
357
++    }
358
++  });
359
++});