我在这里停留了将近3天,尝试了许多解决方案,但无济于事。我的母语很新。我的代码库如下。
我的settings.gradle看起来像
rootProject.name = 'rnn6'
include ':app'
include ':react-native-navigation'
project(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/lib/android/app/')
我的build.gradle看起来像
buildscript {
ext {
buildToolsVersion = "28.0.2"
minSdkVersion = 19
compileSdkVersion = 28
targetSdkVersion = 27
supportLibVersion = "28.0.0"
}
repositories {
google()
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url 'https://jitpack.io'
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '4.7'
distributionUrl = distributionUrl.replace("bin", "all")
}
subprojects { subproject ->
afterEvaluate {
if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
variantFilter { variant ->
def names = variant.flavors*.name
if (names.contains("reactNative51") || names.contains("reactNative55")) {
setIgnore(true)
}
}
}
}
}
}
我的app / build.gradle
apply plugin: "com.android.application"
import com.android.build.OutputFile
project.ext.react = [
entryFile: "index.js"
]
apply from: "../../node_modules/react-native/react.gradle"
def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.rnn6"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
missingDimensionStrategy "RNN.reactNativeVersion", "reactNative57_5"
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a"
}
}
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
}
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support' && requested.name != 'multidex') {
details.useVersion "${rootProject.ext.supportLibVersion}"
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'com.android.support:design:25.4.0'
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
implementation project(':react-native-navigation')
}
// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}
我的MainActivity
package com.rnn6;
import com.reactnativenavigation.NavigationActivity;
public class MainActivity extends ReactActivity {
}
我的MainApplication
package com.rnn6;
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends NavigationApplication {
@Override
protected ReactGateway createReactGateway() {
ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
@Override
protected String getJSMainModuleName() {
return "index";
}
};
return new ReactGateway(this, isDebug(), host);
}
@Override
public boolean isDebug() {
return BuildConfig.DEBUG;
}
protected List<ReactPackage> getPackages() {
// Add additional packages you require here
// No need to add RnnPackage and MainReactPackage
return Arrays.<ReactPackage>asList(
// eg. new VectorIconsPackage()
);
}
@Override
public List<ReactPackage> createAdditionalReactPackages() {
return getPackages();
}
}
我的package.json
{
"name": "rnn6",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.6.3",
"react-native": "0.58"
},
"devDependencies": {
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.5.0",
"jest": "24.5.0",
"metro-react-native-babel-preset": "0.53.0",
"react-test-renderer": "16.6.3"
},
"jest": {
"preset": "react-native"
}
}
最后,当我运行react-native run-android时,出现以下错误
> $ react-native run-android JS server already running. Building and
> installing the app on the device (cd android && gradlew.bat
> installDebug)...
>
> > Configure project :app WARNING: The specified Android SDK Build Tools version (28.0.2) is ignored, as it is below the minimum
> supported version (28.0.3) for Android Gradle Plugin 3.2.1. Android
> SDK Build Tools 28.0.3 will be used. To suppress this warning, remove
> "buildToolsVersion '28.0.2'" from your build.gradle file, as each
> version of the Android Gradle Plugin now has a default version of the
> build tools.
>
> FAILURE: Build failed with an exception.
>
> * What went wrong: Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> > Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
> > Could not resolve project :react-native-navigation.
> Required by:
> project :app
> > Unable to find a matching configuration of project :react-native-navigation: None of the consumable configurations have
> attributes.
>
> * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
>
> * Get more help at https://help.gradle.org
>
> BUILD FAILED in 1s Could not install the app on the device, read the
> error above for details. Make sure you have an Android emulator
> running or a device connected and have set up your Android development
> environment:
> https://facebook.github.io/react-native/docs/getting-started.html
>
> Command failed: gradlew.bat installDebug
>
> Error: Command failed: gradlew.bat installDebug
> at checkExecSyncError (child_process.js:637:11)
> at Object.execFileSync (child_process.js:655:13)
> at runOnAllDevices (D:\react-native\rnn6\node_modules\react-native\local-cli\runAndroid\runAndroid.js:299:19)
> at buildAndRun (D:\react-native\rnn6\node_modules\react-native\local-cli\runAndroid\runAndroid.js:135:12)
> at isPackagerRunning.then.result (D:\react-native\rnn6\node_modules\react-native\local-cli\runAndroid\runAndroid.js:65:12)
> at processTicksAndRejections (internal/process/next_tick.js:81:5)