我使用此答案将Android Studio配置为纯java https://stackoverflow.com/a/26196451。然后我开始将Firebase Admin SDK添加到我的项目中我遵循了本教程https://firebase.google.com/docs/admin/setup但是当我运行它时,我得到了这个例外:
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
来自:
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://clix-clix6596f.firebaseio.com").build();
然后我转到gradle的缓存文件,找到FirebaseOptions $ Builder.class文件。我将其复制到我应用的classes>java>main>com>xwekta>lib
文件夹中。除非我将其名称重命名为不包含$符号的内容,否则Android Studio无法识别它。 (顺便说一下包名也是lib)
这是我的代码:
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());
}
}
}
这是文件层次结构:
| .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
这是build.gradle文件:
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"