I configure it according to official documents to use react-navigation3.0.2
my package.json
{
"name": "mpx",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-gesture-handler": "^1.0.10",
"react-native-scrollable-tab-view": "^0.10.0",
"react-native-vector-icons": "^6.1.0",
"react-navigation": "^3.0.2",
"teaset": "^0.5.10"
},
"devDependencies": {
"babel-jest": "23.6.0",
"babel-preset-react-native": "4.0.1",
"jest": "23.6.0",
"react-test-renderer": "16.3.1"
},
"jest": {
"preset": "react-native"
}
}
and MainActivity.java
package com.mpx;
import com.facebook.react.ReactActivity;
// add start (react-native-gesture-handler)
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
// add end
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "mpx";
}
// add start (react-native-gesture-handler)
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
// add end
}
then react-native run-android
find error
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/mao/Desktop/lzbk/mpx/node_modules/react-native-gesture-handler/android/build.gradle' line: 32
* What went wrong:
A problem occurred evaluating project ':react-native-gesture-handler'.
> Could not find method compileOnly() for arguments [com.facebook.react:react-native:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
I want to know how to solve it.
In the Build file '/Users/mao/Desktop/lzbk/mpx/node_modules/react-native-gesture-handler/android/build.gradle', in the dependencies section change "compileOnly" to "compile". I'm new to React Native so I'm not sure why, but I just looked at other packages in node_modules and found that they use "compile". Good luck.
I've solved this problem by below changes:
- change configuration in
android/build/gradle
. This is part of my code:buildscript { repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { mavenLocal() jcenter() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } google() } }
add
google()
tobuildscript
andallprojects
, and change the gradle version in classpath. Open...\node_modules\react-native-vector-icons\android\build.gradle
, you will find the gradle version in classpath is 3.1.4, that's why we need to make this change.
- In your android project, open
android/gradle/wrapper/gradle-wrapper.properties
, modify the distributionUrl:distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Since this library depends on gradle version above 3.1.4, we should set a new version than that.
Rebuild the project, it finally works.
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 28
}
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
classpath 'com.google.gms:google-services:4.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}
google()
jcenter()
maven { url 'https://jitpack.io' }
maven { url "https://maven.google.com" }
}
}
solved by make build.gradle file like this.
Most helpful comment
In the Build file '/Users/mao/Desktop/lzbk/mpx/node_modules/react-native-gesture-handler/android/build.gradle', in the dependencies section change "compileOnly" to "compile". I'm new to React Native so I'm not sure why, but I just looked at other packages in node_modules and found that they use "compile". Good luck.