You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native构建报错:Could not find com.facebook.react:react-native:0.74.4

React Native 构建报错 Could not find com.facebook.react:react-native:0.74.4 解决方案

问题概述

使用Metro初始化React Native项目,添加react-native-safe-area-context和react-native-screens依赖后,执行./gradlew build时触发依赖找不到的错误:Could not find com.facebook.react:react-native:0.74.4,尝试添加exclusiveContent配置后问题未解决。

项目配置参考

package.json

{
  "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": "yarn@3.6.4"
}

app/build.gradle

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.
            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)

项目级build.gradle

//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"

分步修复方案

1. 对齐Gradle插件与RN版本

项目级build.gradle中react-native-gradle-plugin版本为0.71.19,与RN版本0.74.4不匹配,导致依赖解析逻辑异常。修改为对应版本:

classpath("com.facebook.react:react-native-gradle-plugin:0.74.4")

2. 删除冗余依赖声明

React Native 0.70+版本仅需react-android依赖,app/build.gradle中额外添加的react-native依赖属于冗余配置,会引发冲突。删除该行:

implementation 'com.facebook.react:react-native:0.74.4'

3. 修正本地Maven仓库路径

检查$rootDir/../node_modules/react-native/android路径是否正确,若项目根目录直接包含node_modules,应改为:

maven { url "$rootDir/node_modules/react-native/android" }

4. 启用官方推荐的exclusiveContent配置

启用注释掉的exclusiveContent配置(这是RN官方推荐的依赖解析方式),同时移除单独的maven声明,避免重复:

exclusiveContent {
    filter {
        includeGroup "com.facebook.react"
    }
    forRepository {
        maven {
            url "$rootDir/node_modules/react-native/android" // 确保路径与实际项目结构匹配
        }
    }
}

5. 清理缓存后重新构建

执行以下命令清理构建缓存并重新安装依赖:

./gradlew clean
yarn install
./gradlew build

内容的提问来源于stack exchange,提问作者Somnath Pal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 05:13:12