无法将 Firebase 添加到 Android 项目



我不能添加构建所需的依赖项。Gradle,因为它看起来不对

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
} 

这是我的文件,但要添加irebase依赖,你需要一个像这样的文件

buildscript {

repositories {

// Check that you have the following line (if not, add it):

google()  // Google's Maven repository

}

dependencies {

...

// Add this line

classpath 'com.google.gms:google-services:4.3.12'

}

}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
google()  // Google's Maven repository
...
}
}

为什么我的构建。Gradle看起来有问题吗?

在您的build.gradle (project)。注意插件中的google服务

// Paste this buildscript
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
// Some other needed classpath 
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
}
}
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}

在你的build.gradle (module)

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
// google service 
id 'com.google.gms.google-services'

id 'kotlin-kapt'
id 'androidx.navigation.safeargs'
id 'kotlin-parcelize'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.yourAppName"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
dataBinding true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
// Added newly
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01'
// firebase
implementation platform('com.google.firebase:firebase-bom:30.1.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-database-ktx:20.0.5'
}