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"