Search code examples
javaandroidfirebasefirebase-authenticationfirebase-admin

error: cannot find symbol .setCredentials(GoogleCredentials.fromStream(serviceAccount))


Please help me with this code, I still don't understand why I'm like this, I edited the gradle file, and edited the java file but it still doesn't work, please help me fix it. p/s: this is the first time I post help so I'm so grateful and appreciative

Java file:

In the Java file, I tried to declare it like on the Firebase page of Google but still got the error

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 file: In this Gradle file, I have added libraries that may be needed but it still fails in the Java file

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'

}

Solution

  • The FirebaseOptions.Builder class doesn't contain any .setCredentials() methods. So there is no way you can call such a method on an object of type Builder, hence the error.

    On the other hand, Firebase Admin SDK indeed contains such a method, but unfortunately, you cannot use the Admin SDK and the regular Android SDK for Firebase in the same project.

    If you want to use some functionality that exists in the Firebase Admin SDK, then you should consider creating a Cloud Function and calling it from your Android application's code.