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

React Native构建报错:react-native-safe-area-context相关问题排查

构建React Native项目时的构建失败问题排查

报错信息

BUILD FAILED in 4s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file 'C:\Users\sawan\OneDrive\Desktop\Unsplash\app\node_modules\react-native-safe-area-context\android\build.gradle' line: 28

* What went wrong:
A problem occurred evaluating project ':react-native-safe-area-context'.
> Could not initialize class org.jetbrains.kotlin.gradle.plugin.KotlinGradleBuildServices

* 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.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
A problem occurred configuring project ':react-native-safe-area-context'.
> com.android.builder.errors.EvalIssueException: compileSdkVersion is not specified. Please add it to build.gradle

* 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.
==============================================================================

相关代码片段

1. react-native-safe-area-context模块的build.gradle

//rest of the code ...
android {
compileSdkVersion getExtOrDefault('compileSdkVersion', 30)

// Used to override the NDK path/version on internal CI or by allowing
// users to customize the NDK path/version from their root project (e.g. for M1 support)
if (rootProject.hasProperty("ndkPath")) {
    ndkPath rootProject.ext.ndkPath
}
if (rootProject.hasProperty("ndkVersion")) {
    ndkVersion rootProject.ext.ndkVersion
}

defaultConfig {
    minSdkVersion getExtOrDefault('minSdkVersion', 16)
    targetSdkVersion getExtOrDefault('targetSdkVersion', 28)
    versionCode 1
    versionName "1.0"
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

    ndk {
         abiFilters (*reactNativeArchitectures())
     }
}// rest of the code ....

2. 项目根目录/android/build.gradle

buildscript {
ext {
    buildToolsVersion = "33.0.0"
    minSdkVersion = 21
    compileSdkVersion = 33
    targetSdkVersion = 33

    // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
    ndkVersion = "23.1.7779620"
    // kotlin_version = '1.5.20' //
}
repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath("com.android.tools.build:gradle:7.3.1")
    classpath("com.facebook.react:react-native-gradle-plugin")
    // classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") //
}}

问题分析

两个报错存在关联:

  1. 核心问题是Kotlin Gradle插件未配置,根目录build.gradle中注释掉了kotlin_version的定义和对应的classpath依赖,导致无法初始化Kotlin相关类,触发第一个错误。
  2. 第二个compileSdkVersion未指定的错误是连锁问题:因为Kotlin插件加载失败,影响了gradle脚本的正常执行,导致getExtOrDefault方法无法正确读取根目录ext中定义的compileSdkVersion。

解决方案

  1. 启用Kotlin Gradle插件
    打开项目根目录/android/build.gradle,取消注释以下两行代码:

    kotlin_version = '1.5.20'
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
    

    修改后的buildscript部分如下:

    buildscript {
    ext {
        buildToolsVersion = "33.0.0"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
    
        // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
        ndkVersion = "23.1.7779620"
        kotlin_version = '1.5.20'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.3.1")
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
    }}
    
  2. 清理缓存并重新构建
    在项目根目录执行以下命令清理gradle缓存:

    cd android && gradlew clean
    

    之后回到项目根目录重新运行React Native项目:

    npx react-native run-android
    

内容的提问来源于stack exchange,提问作者Kashi S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 05:05:20