【发布时间】:2016-01-08 21:26:00
【问题描述】:
我的 android-appengine-endpoint 项目完全由 android studio 设置。因此我什么都不做:我只是让 android studio 将我的 AppEngine 模块添加到我的 android 项目中。现在我收到了java.lang.NoClassDefFoundError。我是否必须在 gradle 中设置一些东西或在 android studio 中配置一些东西才能摆脱这个错误?基本上,当我尝试测试我的应用程序时,我得到了异常。
代码:
注意:UserDao 不是在我的端点类中调用ofy,而是进行所有ofy 数据访问调用的地方。您可以将我的代码与this tutorial 进行比较 p>
public class UserDao extends OfyService {
public static void persist(User user) {
ofy().save().entities(user).now();
}
public static User getById(long id) {
return ofy().load().type(User.class).id(id).now();
}
public static List<User> getByTwitterId(Long twitterId) {
return ofy().load().type(User.class).filter("twitterId", twitterId).list();
}
}
public class OfyService {
public OfyService() {}
static {
ObjectifyService.register(User.class);
ObjectifyService.register(Food.class);
ObjectifyService.register(Pet.class);
}
public static Objectify ofy() { return ObjectifyService.ofy(); }
public static ObjectifyFactory factory() {
return ObjectifyService.factory();
}
}
从 App Engine 控制台看到的实际错误:
com.google.api.server.spi.SystemService invokeServiceMethod: exception occurred while calling backed method
java.lang.NoClassDefFoundError: Could not initialize class com.mycompany.server.data.dao.UserDao
at com.mycompany.server.utils.Authenticator.verifyAccount(Authenticator.java:22)
at com.mycompany.server.endpoint.server.getFood(server.java:105)
at sun.reflect.GeneratedMethodAccessor22.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:44)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:359)
at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:160)
at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:118)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:230)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at java.lang.Thread.run(Thread.java:745)
我在这里看到的每个帖子似乎都在谈论WEB-INF\lib。好吧,Android Studio 没有在我的 WEB-INF 中创建任何 lib 目录。所以 Android Studio 和 App Engine 都来自谷歌。所以我在那里迷路了。
我的webapp/WEB-INF 里面只有三个文件:
web.xmllogging.propertiesappengine-web.xml
这是我的依赖项的图像,以防有帮助:
【问题讨论】:
-
我编写了实际的应用引擎源代码:实体、端点、ofy。我只是不知道
WEB-INF里面有什么。至于 UserDao,它是 OfyService 的子类,而不是实体。所以我不知道那里发生了什么。 -
与 UserDao(与其他 OfyService 子类相比)的唯一区别是它的一个静态方法抛出异常:
IllegalStateException -
UserDao 不是一个实体。它没有
@Entity注释。我的子类是指public class UserDao extends OfyService,其中OfyService 只是ObjectifyService的包装,我只是遵循推荐的最佳实践结构。 -
我重构了 UserDao 类并删除了异常。做到了。在某种情况下,我有一种 satic 方法会引发异常。这就是问题的原因。
-
在堆栈跟踪中我看到
com.googlecode.objectify.ObjectifyFilter,我得出结论认为Gradle 已经下载了Objectify 没有问题。那么缺什么课呢?
标签: android google-app-engine google-cloud-endpoints objectify