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

Firebase初始化报错:cannot find symbol .setCredentials()求助

解决Firebase Realtime Database编译错误:error: cannot find symbol .setCredentials(GoogleCredentials.fromStream(serviceAccount))

问题详情

编译Java代码时出现error: cannot find symbol .setCredentials(GoogleCredentials.fromStream(serviceAccount)),已参照Firebase官方文档编写代码并调整Gradle依赖,但问题未解决。

现有代码与配置

Java代码

package Database;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.FileInputStream;
import java.io.IOException;

public class DemoFirebase {
    public static void main(String[] args) throws IOException {
        FileInputStream serviceAccount = new FileInputStream("my_JSon_file");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("my_database_url")
                .build();
    
        FirebaseApp.initializeApp(options);
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Object value = dataSnapshot.getValue();
                System.out.println("Value is: " + value);
            }
    
            @Override
            public void onCancelled(DatabaseError error) {
                System.out.println("Error: " + error.getMessage());
            }
        });
    }
}

Gradle依赖配置

dependencies {
    implementation 'com.google.firebase:firebase-analytics:21.2.2'
    implementation platform('com.google.firebase:firebase-bom:31.5.0')
    implementation 'com.google.cloud:google-cloud-storage:2.14.0'
    implementation 'com.google.firebase:firebase-admin:9.1.1'
    implementation 'com.google.firebase:firebase-analytics'
    implementation('com.google.firebase:firebase-appcheck-debug:16.1.2')
    implementation('com.google.firebase:firebase-inappmessaging:20.3.1')
    implementation('com.google.firebase:firebase-inappmessaging-display:20.3.1')
    implementation 'com.google.auth:google-auth-library-credentials:1.12.1'
    implementation 'com.google.auth:google-auth-library-oauth2-http:1.12.1'
    implementation 'com.google.firebase:firebase-database:20.2.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.3.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

解决方案

1. 区分项目类型,移除冲突依赖

你的依赖中同时包含了Firebase Admin SDK和Android Firebase SDK,这两者不能混用:

  • 如果是后端Java项目:删除所有Android相关依赖(如androidx.appcompat、com.google.android.material等),仅保留firebase-admin和认证相关依赖,确保依赖精简无冲突。
  • 如果是Android项目:删除com.google.firebase:firebase-admin:9.1.1,Android项目无需手动设置Credentials,Firebase会通过设备的Google服务自动完成认证。

2. 修正初始化代码(按项目类型调整)

  • Android项目:无需手动构建FirebaseOptions,简化初始化代码:

    // 直接初始化Firebase App
    FirebaseApp.initializeApp(getApplicationContext());
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
    // 后续监听逻辑保持不变
    

    同时确保app目录下存在google-services.json文件,并在项目根build.gradle中添加Google Services插件:

    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
    }
    

    在app模块的build.gradle末尾添加:

    apply plugin: 'com.google.gms.google-services'
    
  • 后端Java项目:确认firebase-admin版本正确,执行gradle clean和gradle build清除缓存后重新编译,确保GoogleCredentials的导入路径正确。

3. 清理冗余依赖

删除不必要的依赖(如重复的firebase-analytics、firebase-appcheck-debug、firebase-inappmessaging等),仅保留项目必需的库,降低依赖冲突概率。

内容的提问来源于stack exchange,提问作者Hoàng Trần

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 00:47:51