无法在Firebase

时间:2017-11-30 19:55:25

标签: java google-app-engine firebase firebase-realtime-database firebase-admin

我试图从Google App Engine操纵我的Firebase数据库。按照教程,我收到以下错误 -

  

java.lang.NoClassDefFoundError:无法初始化类com.google.firebase.internal.FirebaseThreadManagers

有时我会看到以下内容:

org.slf4j.LoggerFactory is a restricted class. Please see the Google  App Engine developer's guide for more details.

错误发生在FirebaseOptions上,如下面的代码所示。

        FileInputStream serviceAccount = new FileInputStream("WEB-INF/MyApp.json");

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

    FirebaseApp.initializeApp(options);

这里有完整的错误

  

java.lang.NoClassDefFoundError:无法初始化类com.google.firebase.internal.FirebaseThreadManagers       在com.google.firebase.FirebaseOptions $ Builder。(FirebaseOptions.java:147)       在com.example.name.myapplication.backend.MyServlet.doGet(MyServlet.java:55)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:617)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:717)       在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166)       在com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doRedirectedModuleRequest(DevAppServerModulesFilter.java:415)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:128)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)       在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)       在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)       在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)       在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)       在com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)       在org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)       在org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)       在com.google.appengine.tools.development.JettyContainerService.forwardToServer(JettyContainerService.java:458)       在com.google.appengine.tools.development.Modules.forwardToInstance(Modules.java:372)       在com.google.appengine.tools.development.DelegatingModulesFilterHelper.forwardToInstance(DelegatingModulesFilterHelper.java:95)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doRedirect(DevAppServerModulesFilter.java:326)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:119)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)       在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)       在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)       在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)       在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)       在com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在com.google.appengine.tools.development.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:511)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在org.mortbay.jetty.Server.handle(Server.java:326)       在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)       at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:923)       在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)       在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)       在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)       在org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)       在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)

这是我后端的build.gradle

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.google.appengine:gradle-appengine-plugin:1.9.42'
    }
}

repositories {
    jcenter();
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.42'
    compile 'javax.servlet:servlet-api:2.5'
    compile 'com.google.firebase:firebase-admin:5.5.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.google.code.gson:gson:2.6.1'


    appengine {
        downloadSdk = true
        appcfg {
            oauth2 = true
        }
    }
}

顶级构建文件:

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

2 个答案:

答案 0 :(得分:2)

不要在服务器应用中使用firebase-admin依赖项和其他Android客户端库。 firebase-admin单独拥有在JVM运行时中访问Realtime Database所需的一切。 Android客户端库在服务器应用程序中没用,因为它们需要Android基础架构。

答案 1 :(得分:0)

对于以下代码行,我遇到相同的错误 NoClassDefFoundError FirebaseThreadManagers private ThreadManager threadManager = FirebaseThreadManagers.DEFAULT_THREAD_MANAGER;

解决方案

重建 gradle 文件。重建 gradle 文件后,Kotlin应用程序按预期运行。