1

I configure the Android Studio use as pure java https://stackoverflow.com/a/26196451 using this answer. Then I started adding the Firebase Admin SDK to my project I followed this tutorial https://firebase.google.com/docs/admin/setup However when I run it I get this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/firebase/FirebaseOptions$Builder
    at com.xwekta.lib.myClass.main(myClass.java:24)
Caused by: java.lang.ClassNotFoundException: com.google.firebase.FirebaseOptions$Builder
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

from the line of :

FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://clix-clix6596f.firebaseio.com").build();

Then I go to gradle's cache file and found FirebaseOptions$Builder.class file. I copied it to my app's classes>java>main>com>xwekta>lib folder. Android Studio didn't recognize it unless I rename its name to something which doesn't include $ symbol. (By the way package name is also lib)

Here is my code:

package com.xwekta.lib;

import com.google.api.core.ApiFuture;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.cloud.FirestoreClient;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.ExecutionException;


public class myClass {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        String hi = "Hello World";
        System.out.println(hi);
        FileInputStream serviceAccount = new FileInputStream("D:\\Cem\\Firebasecokonemli\\sirebasea_dmin.json");
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://clix-clix-6596f.firebaseio.com").build();
        FirebaseApp.initializeApp(options);
        Firestore fstr = FirestoreClient.getFirestore();
        CollectionReference colref = fstr.collection("users");
        Query query = colref.whereEqualTo("First Name", "Ali");
        ApiFuture<QuerySnapshot> querySnapshot = query.get();
        for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
            System.out.println(document.getId());
        }

}


}

Here is the file hierarchy:

|   .gitignore
|   build.gradle
|   lib.iml
|   
+---build
|   +---classes
|   |   \---java
|   |       \---main
|   |           \---com
|   |               \---xwekta
|   |                   \---lib
|   |                           FirebaseOptions$Builder.class // This where I put the file for only troubleshooting.
|   |                           myClass.class
|   |                           
|   +---libs
|   |       lib.jar
|   |       
|   \---tmp
|       +---compileJava
|       \---jar
|               MANIFEST.MF
|               
+---libs
|       
\---src
    \---main
        \---java
            \---com
                \---xwekta
                    \---lib
                            myClass.java

Here is the build.gradle file :

apply plugin: 'java-library'

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    api 'com.google.firebase:firebase-admin:5.8.0'
    testCompileOnly 'com.android.support:multidex:1.0.2'
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
Cem
  • 29
  • 4
  • 2
    You can't just copy files from build caches into another module's build directory. That's never going to work. That build folder is derived from your project's source code and can be removed at any time. – Doug Stevenson Feb 04 '18 at 22:04
  • 2
    Also, why are you adding firebase-admin jars to your project at the same time as using the maven coordinates? That makes no sense. Just use the maven coordinates. – Doug Stevenson Feb 04 '18 at 22:05
  • @DougStevenson ah, these jar files for just troubleshooting I am indeed using maven coordinates. And for the other question I know it is delivered from my code but isn't it interesting that Android Studio only sees .class files without $ on it. So other thing was for the troubleshooting as well. – Cem Feb 04 '18 at 22:27
  • Did you find a resolution for this? This is happening to me as well... – user1163234 Jun 28 '20 at 12:13
  • @user1163234 Actually no, I have learned little Python and use that to access Firebase. – Cem Jul 03 '20 at 21:01

0 Answers0