React Native 尝试构建、清理或同步 gradle 会导致此错误。
错误: `> 配置项目:react-native-firebase_app :react-native-firebase_app package.json 位于 /Users/{name}/Documents/GitHub/{project}/node_modules/@react-native-firebase/app/package.json
FAILURE:构建失败并出现异常。
地点: 构建文件'/Users/{name}/Documents/GitHub/{project}/node_modules/@react-native-firebase/app/android/build.gradle'行:66
出了什么问题: 评估项目“:react-native-firebase_app”时出现问题。
设置命名空间为时已晚 已经阅读了它来配置该项目。 考虑将此调用移至评估期间, 或使用变体 API。`
@react-native-firebase/app/android/build.gradle':
import io.invertase.gradle.common.PackageJson
buildscript {
// The Android Gradle plugin is only required when opening the android folder stand-alone.
// This avoids unnecessary downloads and potential conflicts when the library is included as a
// module dependency in an application project.
if (project == rootProject) {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:8.1.2")
}
}
}
plugins {
id "io.invertase.gradle.build" version "1.5"
}
def packageJson = PackageJson.getForProject(project)
def firebaseBomVersion = packageJson['sdkVersions']['android']['firebase']
def playServicesAuthVersion = packageJson['sdkVersions']['android']['playServicesAuth']
def jsonMinSdk = packageJson['sdkVersions']['android']['minSdk']
def jsonTargetSdk = packageJson['sdkVersions']['android']['targetSdk']
def jsonCompileSdk = packageJson['sdkVersions']['android']['compileSdk']
project.ext {
set('react-native', [
versions: [
android: [
minSdk: jsonMinSdk,
targetSdk: jsonTargetSdk,
compileSdk: jsonCompileSdk,
],
firebase: [
bom: firebaseBomVersion,
],
play: [
"play-services-auth": playServicesAuthVersion,
],
],
])
}
apply from: file('./firebase-json.gradle')
// If data collection isn't specifically disabled, default is enabled
String dataCollectionDefaultEnabled = 'true'
if (rootProject.ext && rootProject.ext.firebaseJson) {
if (rootProject.ext.firebaseJson.isFlagEnabled('app_data_collection_default_enabled', true) == false) {
dataCollectionDefaultEnabled = 'false'
}
}
android {
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
// Set namespace here
if (agpVersion >= 7) {
namespace 'io.invertase.firebase'
}
defaultConfig {
multiDexEnabled true
manifestPlaceholders = [
firebaseJsonDataCollectionDefaultEnabled: dataCollectionDefaultEnabled
]
}
buildFeatures {
// AGP 8 no longer builds config by default
buildConfig true
}
lintOptions {
disable 'GradleCompatible'
abortOnError false
}
if (agpVersion < 8) {
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/reactnative/java']
}
}
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation platform("com.google.firebase:firebase-bom:${ReactNative.ext.getVersion("firebase", "bom")}")
implementation "com.google.firebase:firebase-common"
implementation "com.google.android.gms:play-services-auth:${ReactNative.ext.getVersion("play", "play-services-auth")}"
}
ReactNative.shared.applyPackageVersion()
ReactNative.shared.applyDefaultExcludes()
ReactNative.module.applyAndroidVersions()
ReactNative.module.applyReactNativeDependency("api")
build.gradle:
buildscript {
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 21
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "25.1.8937393"
kotlinVersion = "1.8.0" // Make sure this matches the version you wish to use
}
ext.kotlin_version = "1.8.0"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:8.4.1"
classpath "com.google.gms:google-services:4.4.0"
classpath "com.facebook.react:react-native-gradle-plugin:+"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'com.facebook.react.rootproject'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
namespace {namespace}
defaultConfig {
applicationId {same as namespace}
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
implementation project(':react-native-firebase_app')
}
gradle.wrapper.properties:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
我删除了节点模块并重新安装。我升级了gradle版本。我尝试升级android studio。我尝试修改@react-native-firebase/app/android/build.gradle'。除非我修改了 @react-native-firebase/app/android/build.gradle' 文件并注释掉了很多,然后我才能让它同步但不能构建,否则这些都不起作用。这就是我对该结果的评论:
if (agpVersion >= 7) {
namespace 'io.invertase.firebase'
}
lintOptions {
disable 'GradleCompatible'
abortOnError false
}
dependencies {
implementation platform("com.google.firebase:firebase-bom:${ReactNative.ext.getVersion("firebase", "bom")}")
implementation "com.google.firebase:firebase-common"
implementation "com.google.android.gms:play-services-auth:${ReactNative.ext.getVersion("play", "play-services-auth")}"
}
ReactNative.shared.applyPackageVersion()
ReactNative.shared.applyDefaultExcludes()
ReactNative.module.applyAndroidVersions()
ReactNative.module.applyReactNativeDependency("api")
主要问题来自于版本不兼容。以下是我用来解决问题的版本:
科特林:1.8.0 Gradle:gradle-8.2-all.zip Java:17
还在我的 android/buil.gradle 中将插件依赖项设置为不是动态的,所以
classpath "com.facebook.react:react-native-gradle-plugin:8.2"
最后在我的 android/app/build.gradle 中我将其设置为我的依赖项:
dependencies {
implementation 'com.facebook.react:react-native:+'
// Exclude Flipper integration if not used
implementation('com.facebook.react:react-native:+') {
exclude group: 'com.facebook.flipper'
}
implementation 'com.facebook.soloader:soloader:0.10.1+'
}