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

集成react-native-safe-area-context时Android构建失败求助

构建失败:react-native-safe-area-context集成报错

报错详情

构建过程出现两个核心错误:

  1. 任务执行异常
    • 位置:node_modules/react-native-safe-area-context/android/build.gradle 第35行
    • 错误内容:

      Could not initialize class org.jetbrains.kotlin.gradle.plugin.KotlinGradleBuildServices

  2. 任务配置异常
    • 错误内容:

      compileSdkVersion is not specified. Please add it to build.gradle

相关配置

库的build.gradle代码

buildscript {
    def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['RNSAC_kotlinVersion']

    repositories {
        mavenCentral()
        google()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
        classpath("com.android.tools.build:gradle:7.3.1")
        classpath("com.diffplug.spotless:spotless-plugin-gradle:6.11.0")
    }
}

if (project == rootProject) {
    apply from: "spotless.gradle"
    return
}


def getExtOrDefault(name, defaultValue) {
    return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
}

def isNewArchitectureEnabled() {
    // To opt-in for the New Architecture, you can either:
    // - Set `newArchEnabled` to true inside the `gradle.properties` file
    // - Invoke gradle with `-newArchEnabled=true`
    // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
    return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

if (isNewArchitectureEnabled()) {
    apply plugin: "com.facebook.react"
}

android {
    def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
    if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
        namespace "com.th3rdwave.safeareacontext"
    }

    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())
        }
    }

    lintOptions{
        abortOnError false
    }

    packagingOptions {
        // For some reason gradle only complains about the duplicated version of libreact_render libraries
        // while there are more libraries copied in intermediates folder of the lib build directory, we exclude
        // only the ones that make the build fail (ideally we should only include libsafeareacontext_modules but we
        // are only allowed to specify exclude patterns)
        exclude "**/libreact_render*.so"
    }
    sourceSets.main {
        java {
            if (isNewArchitectureEnabled()) {
                srcDirs += [
                    "src/fabric/java",
                    "${project.buildDir}/generated/source/codegen/java"
                ]
            } else {
                srcDirs += [
                    "src/paper/java"
                ]
            }
        }
    }
}

def reactNativeArchitectures() {
    def value = project.getProperties().get("reactNativeArchitectures")
    return value ? value.split(",") : [
        "armeabi-v7a",
        "x86",
        "x86_64",
        "arm64-v8a"
    ]
}

repositories {
    google()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    mavenCentral()
}

def kotlin_version = getExtOrDefault('kotlinVersion', project.properties['RNSAC_kotlinVersion'])

dependencies {
    implementation 'com.facebook.react:react-native:+'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

项目package.json

{
  "name": "UserManager",
  "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.7",
    "@react-navigation/native-stack": "^6.9.13",
    "react": "18.2.0",
    "react-native": "0.72.4",
    "react-native-safe-area-context": "^4.7.1",
    "react-native-screens": "^3.24.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/eslint-config": "^0.72.2",
    "@react-native/metro-config": "^0.72.11",
    "@tsconfig/react-native": "^3.0.0",
    "@types/react": "^18.0.24",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.2.1",
    "eslint": "^8.19.0",
    "jest": "^29.2.1",
    "metro-react-native-babel-preset": "0.76.8",
    "prettier": "^2.4.1",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  },
  "engines": {
    "node": ">=16"
  }
}

已尝试操作

  • 执行./gradlew cleanBuildCache清理构建缓存
  • 重新安装项目依赖
  • 重新执行react-native run-android构建

解决方案

1. 明确配置项目根Gradle的扩展参数

打开项目根目录的android/build.gradle,在buildscript块中添加或完善ext配置,确保包含compileSdkVersion、kotlinVersion等参数,适配React Native 0.72.4的要求:

buildscript {
    ext {
        buildToolsVersion = "33.0.0"
        minSdkVersion = 21
        compileSdkVersion = 33
        targetSdkVersion = 33
        kotlinVersion = "1.8.0"
    }
    // ... 其他原有内容(repositories、dependencies等)
}

该配置会被react-native-safe-area-context的build.gradle读取,解决compileSdkVersion未指定的问题,同时统一Kotlin版本。

2. 修复Kotlin与Gradle插件兼容性

  • React Native 0.72.4对应的Android Gradle插件版本为7.4.2,确保根项目android/build.gradle中的com.android.tools.build:gradle版本与之一致,避免版本冲突:
    dependencies {
        classpath("com.android.tools.build:gradle:7.4.2")
        // ... 其他依赖
    }
    
  • 确保本地安装的Kotlin版本与配置的kotlinVersion一致,或允许Gradle自动下载对应版本(需网络正常)。

3. 彻底清理缓存并重构

  1. 删除以下目录:
    • 项目根目录的node_modules
    • android/.gradle、android/build、android/app/build
  2. 执行npm install重装依赖
  3. 进入android目录执行./gradlew clean
  4. 重新执行react-native run-android

内容的提问来源于stack exchange,提问作者Pawan yadav

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 00:28:09