react-native-safe-area-context build.gradle 文件出现错误且未指定compileSdkVersion

问题描述 投票:0回答:2

我在构建我的 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.
==============================================================================

它的错误在这个文件中: C:\Users\sawan\OneDrive\Desktop\Unsplash pp 颂歌模块 eact-native-safe-area-context ndroid uild.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 ....

这是我来自 ndroid 的 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") //
}}

不知道错误是在这个路径还是build.gradle文件中

react-native kotlin gradle build.gradle react-native-safe-area-view
2个回答
1
投票

“gradle 缓存”有关。

只需清理 gradle 缓存,例如:

rm -rf ~/.gradle

然后

yarn android

npx react-native run-android

最初由me(@Pranav-yadav)在这里发布:https://github.com/th3rdwave/react-native-safe-area-context/issues/391#issuecomment-1565925679

P.S.:清理 gradle 缓存后,第一次构建时,它将下载所需的 gradle 文件,因此,第一次构建将消耗“更多”带宽。之后它将像往常一样构建。


0
投票

在您的项目中

android>gradle>包装器>gradle-wrapper.properties 降低你的 gradle 版本(例如 8.6 到 8.2.1) 比运行你的项目 npx react-native start --reset-cache

© www.soinside.com 2019 - 2024. All rights reserved.