java - Firebase 错误 - 无法在 Android Studio 中计算属性 'extraGeneratedResDir' 的值

标签 java android firebase android-studio

我正在 Android Studio 中使用 Firebase 数据库运行一个简单的应用 - 已在 Firebase 仪表板上设置项目并连接到我的 Android Studio 应用。构建项目时出现此错误:

Execution failed for task ':app:mapDebugSourceSetPaths'.
> Error while evaluating property 'extraGeneratedResDir' of task ':app:mapDebugSourceSetPaths'
   > Failed to calculate the value of task ':app:mapDebugSourceSetPaths' property 'extraGeneratedResDir'.
      > Querying the mapped value of provider(java.util.Set) before task ':app:processDebugGoogleServices' has completed is not supported

这是堆栈跟踪:

Executing tasks: [:app:assembleDebug] in project D:\work\Android Studio\FirebaseExample2

    Download https://plugins.gradle.org/m2/com/gradle/gradle-enterprise-gradle-plugin/3.10.2/gradle-enterprise-gradle-plugin-3.10.2.jar, took 1 s 705 ms (2.21 MB)
    > Task :app:createDebugVariantModel
    > Task :app:preBuild UP-TO-DATE
    > Task :app:preDebugBuild UP-TO-DATE
    > Task :app:mergeDebugNativeDebugMetadata NO-SOURCE
    > Task :app:compileDebugAidl NO-SOURCE
    > Task :app:compileDebugRenderscript NO-SOURCE
    > Task :app:generateDebugBuildConfig
    > Task :app:javaPreCompileDebug
    > Task :app:checkDebugAarMetadata
    > Task :app:generateDebugResValues
    > Task :app:mapDebugSourceSetPaths FAILED
    > Task :app:generateDebugResources
    > Task :app:mergeDebugResources
    > Task :app:processDebugGoogleServices
    Execution optimizations have been disabled for task ':app:processDebugGoogleServices' to ensure correctness due to the following reasons:
      - Gradle detected a problem with the following location: 'D:\work\Android Studio\FirebaseExample2\app\build\generated\res\google-services\debug'. Reason: Task ':app:mergeDebugResources' uses this output of task ':app:processDebugGoogleServices' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.5/userguide/validation_problems.html#implicit_dependency for more details about this problem.
    > Task :app:packageDebugResources
    > Task :app:createDebugCompatibleScreenManifests
    > Task :app:extractDeepLinksDebug
    > Task :app:parseDebugLocalResources
    > Task :app:processDebugMainManifest
    > Task :app:processDebugManifest
    > Task :app:mergeDebugShaders
    > Task :app:compileDebugShaders NO-SOURCE
    > Task :app:generateDebugAssets UP-TO-DATE
    > Task :app:mergeDebugAssets
    > Task :app:compressDebugAssets
    > Task :app:processDebugJavaRes NO-SOURCE
    > Task :app:desugarDebugFileDependencies
    > Task :app:checkDebugDuplicateClasses
    > Task :app:mergeDebugJniLibFolders
    > Task :app:processDebugManifestForPackage
    > Task :app:mergeDebugJavaResource
    > Task :app:mergeExtDexDebug

这里是 google-services.json(从 Firebase 控制台下载并粘贴到此处的应用目录 (D:\work\Android Studio\FirebaseExample2\app): directory location for google-services.json file

{
  "project_info": {
    "project_number": "102836682558",
    "firebase_url": "https://example-d3da6-default-rtdb.firebaseio.com",
    "project_id": "example-d3da6",
    "storage_bucket": "example-d3da6.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:102836682558:android:b20556c6763de16c5c766f",
        "android_client_info": {
          "package_name": "com.enetapplications.firebaseexample"
        }
      },
      "oauth_client": [
        {
          "client_id": "102836682558-j851bq7699ovchhcufcn1o60jul1fpm3.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "com.enetapplications.firebaseexample",
            "certificate_hash": "1dad8469cb2080d8e92693b394b0791456170041"
          }
        },
        {
          "client_id": "102836682558-9rgt5a9e3s1305joulfhpugrjhdrpm7v.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyCKFtgpzJ7ITDeC8LBIysH1B5PyKjOrXJc"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "102836682558-9rgt5a9e3s1305joulfhpugrjhdrpm7v.apps.googleusercontent.com",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

这是build.gradle:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    namespace 'com.enetapplications.firebaseexample'
    compileSdkVersion 33

    defaultConfig {
        applicationId "com.enetapplications.firebaseexample"
        minSdkVersion 27
        targetSdkVersion 33
        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
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.firebase:firebase-database:20.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

最后是MainActivity.java:

package com.enetapplications.firebaseexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    Button send;
    EditText name;

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference reference = database.getReference().child("users");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.editText);
        send = findViewById(R.id.button);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = name.getText().toString();
                reference.child("userName").setValue(userName);
            }
        });

    }
}

我已经尝试过使缓存无效并重新启动 Android Studio,但问题仍然存在。

谢谢!!!

最佳答案

发现问题出在 build.gradle (Project: ) 文件中,具体是这一行:

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

需要更改为:

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

所以现在整个 gradle 文件是:

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.0' apply false
    id 'com.android.library' version '7.4.0' apply false
}

随着新版本的发布,这种情况无疑会发生变化,但截至 2023 年 6 月 28 日,我的程序正在完美编译!

关于java - Firebase 错误 - 无法在 Android Studio 中计算属性 'extraGeneratedResDir' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76567383/

相关文章:

Android 构建因 cordova-whitelist-plugin 而失败

android - 在 apk 之间共享原始资源

android - maven 无法从原型(prototype)创建项目

android - 聊天应用中的 Firebase 实时数据库结构

ios - 用于 Twitter iOS 的 OAuthProvider 使用 Firebase 登录有错误的构造函数

java - 使按钮在我们触摸的确切位置触摸

java - JSESSIONID 的值在 session 失效时不会更改

java - applicationContext.xml 正在从 src/main/resources 复制到/WEB-INF/classes

java - 如何找到与特定值匹配的 int 数组的索引

java - 如何确保多次单击单个按钮不会重复结果?