无法使用导入的库。Android Studio。Google云端服务jar
背景
我创建了一个基本的 Python 应用程序,里面有一些接口注解。我使用 Python 控制台工具导出了一个 Java 客户端库(jar 文件),这个过程是参考了一个示例。然后我用“添加为库”的方式把它加到了我的项目中,并确认了 gradle 文件的设置。
问题
现在我想把这个 jar 文件导入到 Android Studio 0.5.2 中。我按照示例中的步骤操作,但似乎无法访问 jar 文件中的模型。根据示例中的说明,我应该能够像使用其他类一样导入我的库并使用这些模型。
当我添加分号“;”时,Android Studio 会删除我的导入行,并显示一个错误信息:

只能从事件分发线程访问。
详细信息:当前线程:Thread[JobScheduler FJ pool 3/4,6,main] 570719363
我们的分发线程:Thread[AWT-EventQueue-0 0.5.2#AI-135.1078000, eap:true,6,main] 12400241
系统事件队列线程:Thread[AWT-EventQueue-0 0.5.2#AI-135.1078000, eap:true,6,main] 12400241
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:124)
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1085)
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1073)
at com.intellij.openapi.wm.impl.FocusManagerImpl.assertDispatchThread(FocusManagerImpl.java:1159)
at com.intellij.openapi.wm.impl.FocusManagerImpl.getFocusOwner(FocusManagerImpl.java:888)
at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.getFocusOwner(IdeFocusManagerImpl.java:119)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl$2.run(FileEditorManagerImpl.java:202)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.getActiveSplitters(FileEditorManagerImpl.java:230)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.getSplitters(FileEditorManagerImpl.java:1180)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.getCurrentEditorWithProviderComposite(FileEditorManagerImpl.java:1258)
at com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.getEditors(FileEditorManagerImpl.java:1230)
at com.intellij.codeInsight.daemon.impl.DaemonListeners.canUndo(DaemonListeners.java:390)
at com.intellij.codeInsight.daemon.impl.DaemonListeners.canChangeFileSilently(DaemonListeners.java:386)
at com.intellij.codeInsight.intention.impl.config.QuickFixFactoryImpl.timeToOptimizeImports(QuickFixFactoryImpl.java:741)
at com.intellij.codeInsight.intention.impl.config.QuickFixFactoryImpl.access$000(QuickFixFactoryImpl.java:70)
at com.intellij.codeInsight.intention.impl.config.QuickFixFactoryImpl$1.isAvailable(QuickFixFactoryImpl.java:621)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.addAvailableActionsForGroups(ShowIntentionsPass.java:129)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.access$000(ShowIntentionsPass.java:69)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass$1.process(ShowIntentionsPass.java:88)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass$1.process(ShowIntentionsPass.java:85)
at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl$6.process(DaemonCodeAnalyzerImpl.java:545)
at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl$6.process(DaemonCodeAnalyzerImpl.java:539)
at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx$1.process(DaemonCodeAnalyzerEx.java:59)
at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx$1.process(DaemonCodeAnalyzerEx.java:53)
at com.intellij.openapi.editor.impl.IntervalTreeImpl$IntervalNode.processAliveKeys(IntervalTreeImpl.java:97)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:468)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:463)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:476)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:476)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:463)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:463)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:463)
at com.intellij.openapi.editor.impl.IntervalTreeImpl.processOverlappingWith(IntervalTreeImpl.java:440)
at com.intellij.openapi.editor.impl.MarkupModelImpl.processRangeHighlightersOverlappingWith(MarkupModelImpl.java:264)
at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx.processHighlights(DaemonCodeAnalyzerEx.java:53)
at com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl.processHighlightsNearOffset(DaemonCodeAnalyzerImpl.java:539)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.getAvailableActions(ShowIntentionsPass.java:85)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.getActionsToShow(ShowIntentionsPass.java:260)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.getIntentionActionsToShow(ShowIntentionsPass.java:212)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.doCollectInformation(ShowIntentionsPass.java:186)
at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:61)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass$1$1.run(PassExecutorService.java:380)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1153)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass$1.run(PassExecutorService.java:371)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.doRun(PassExecutorService.java:368)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:344)
at com.intellij.concurrency.JobLauncherImpl$VoidForkJoinTask.exec(JobLauncherImpl.java:193)
at jsr166e.ForkJoinTask.doExec(ForkJoinTask.java:260)
at jsr166e.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:858)
at jsr166e.ForkJoinPool.scan(ForkJoinPool.java:1687)
at jsr166e.ForkJoinPool.runWorker(ForkJoinPool.java:1642)
at jsr166e.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:109)
这个错误信息中有四个部分,看起来都很相似。
我到处搜索过,但还是没有找到解决办法。使用这个库对我的项目非常重要,我已经花了超过 10 个小时试图自己解决这个问题。任何建议或指导都非常感谢。
更新
感谢 @Scott 的反馈,解决了导入消失的问题,但我仍然无法访问 jar 文件中的对象。

有人知道这可能是 jar、gradle 还是 Android Studio 的问题吗?
1 个回答
这个错误其实是Android Studio里的一个小 bug,跟你遇到的问题没关系。你在设置里有打开 Preferences > Editor > Auto import > Optimize imports on the fly 这个选项。当你输入 import
然后按 ; 时,它会自动优化掉这个导入,因为你还没有用到它。你可以选择关闭这个选项,或者不要手动输入 import
语句——在你的代码里引用这个类,让编辑器自动帮你添加 import
。