我正在尝试学习 React Native。我有一个使用 Metro 的 React Native 项目。执行 ./gradlew build 但出现错误
Could not find com.facebook.react:react-native:0.74.4
。我尝试按照一篇 SO 文章中的建议添加 exclusiveContent
但没有帮助。添加 react-native-safe-area-context
和 react-native-screens
后,我收到此错误。任何修复此错误的帮助将不胜感激。
{
"name": "myreactnative",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"lint": "eslint .",
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"@react-navigation/native": "^6.1.18",
"@react-navigation/native-stack": "^6.11.0",
"react": "18.2.0",
"react-native": "0.74.4",
"react-native-safe-area-context": "^4.10.8",
"react-native-screens": "^3.33.0"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@react-native/babel-preset": "0.74.86",
"@react-native/eslint-config": "0.74.86",
"@react-native/metro-config": "0.74.86",
"@react-native/typescript-config": "0.74.86",
"@types/react": "^18.2.6",
"@types/react-test-renderer": "^18.0.0",
"babel-jest": "^29.6.3",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"prettier": "2.8.8",
"react-test-renderer": "18.2.0",
"typescript": "5.0.4"
},
"engines": {
"node": ">=18"
},
"packageManager": "[email protected]"
}
apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"
react {
}
def enableProguardInReleaseBuilds = false
def jscFlavor = 'org.webkit:android-jsc:+'
android {
ndkVersion rootProject.ext.ndkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion
namespace "com.myreactnative"
defaultConfig {
applicationId "com.myreactnative"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}
}
dependencies {
implementation("com.facebook.react:react-android:0.74.4")
implementation("com.android.support:appcompat-v7:28.0.0")
implementation("com.android.tools.lint:lint-gradle:31.5.1")
implementation 'com.facebook.react:react-native:0.74.4'
if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android:0.74.4")
} else {
implementation jscFlavor
}
}
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
//project level build.gradle
buildscript {
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 23
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "26.1.10909125"
kotlinVersion = "1.9.22"
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:8.5.1")
classpath("com.facebook.react:react-native-gradle-plugin:0.71.19")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.0")
}
}
allprojects {
repositories {
google()
mavenCentral()
// exclusiveContent {
// filter {
// includeGroup "com.facebook.react"
// }
// forRepository {
// maven {
// url "$rootDir/../node_modules/react-native/android"
// }
// }
// }
maven {
url "$rootDir/../node_modules/react-native/android"
}
}
}
apply plugin: "com.facebook.react.rootproject"
为什么要在
build.gradle
中添加react-native?从那里删除它。
dependencies {
implementation("com.facebook.react:react-android:0.74.4")
implementation("com.android.support:appcompat-v7:28.0.0")
implementation("com.android.tools.lint:lint-gradle:31.5.1")
if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android:0.74.4")
} else {
implementation jscFlavor
}
React 本机依赖项会自动添加到
package.json
。
"dependencies": {
"@react-navigation/native": "^6.1.18",
"@react-navigation/native-stack": "^6.11.0",
"react": "18.2.0",
//Here it is added automatically hence you do not need to add it in `build.gradle`.
"react-native": "0.74.4",
"react-native-safe-area-context": "^4.10.8",
"react-native-screens": "^3.33.0"
},