【问题标题】:Gradle project does not build when I add RoboBlender当我添加 RoboBlender 时,Gradle 项目没有构建
【发布时间】:2015-02-27 13:50:17
【问题描述】:

我已将 RoboGuice 3 依赖项添加到它编译和运行的 gradle 构建文件中,但是由于 NoClassDefFoundError: AnnotationDatabaseImpl 导致应用程序崩溃。是否有一些研究表明 RoboBlender 是生成定义所必需的(我熟悉不需要 RoboBlender 的 RoboGuice 2)但是当我添加 RoboBlender 时,项目不再构建。

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.koushikdutta.urlimageviewhelper:urlimageviewhelper:1.0.4'
compile 'de.hdodenhof:circleimageview:1.2.1'
compile 'com.getbase:floatingactionbutton:1.4.0'
compile 'de.hdodenhof:circleimageview:1.2.1'
compile 'org.twitter4j:twitter4j-core:4.0.2'
compile files('libs/json-me.jar')
compile files('libs/twitter_api_me-1.9.jar')
compile('ch.acra:acra:4.5.0') {
    exclude group: 'org.json'
}
compile 'org.roboguice:roboguice:3.0.1'
provided 'org.roboguice:roboblender:3.0.1'

}

构建错误:

 Error:Execution failed for task ':app:compileDebugJava'.

java.lang.ClassCastException:com.sun.tools.javac.code.Type 无法转换为 javax.lang.model.type.DeclaredType l>

  • Gradle 的依赖缓存可能已损坏(这有时会在网络连接超时后发生。) 重新下载依赖并同步项目(需要网络)
  • Gradle 构建过程(守护程序)的状态可能已损坏。停止所有 Gradle 守护程序可能会解决此问题。 停止 Gradle 构建进程(需要重启)
  • 如果 Gradle 进程损坏,您也可以尝试关闭 IDE,然后终止所有 Java 进程。

    这是什么原因造成的,我该如何解决?

    【问题讨论】:

      标签: java android build.gradle roboguice


      【解决方案1】:

      这种错误可能是由于错误使用@Inject造成的,如下例所示:

      public class Foo {
      
          @Inject
          public Foo(Context context, int code) { 
              //this won't work because of the primitive in the constructor 
              //and the @Inject annotation are being used together
          }
      
      }
      

      RoboBlender 将无法构造数据库,因为无法将原语转换为声明的类型。

      因此,您的错误消息

      java.lang.ClassCastException: com.sun.tools.javac.code.Type cannot be cast to javax.lang.model.type.DeclaredType
      

      表示原语(com.sun.tools.javac.code.Type)不能转换为引用类型javax.lang.model.type.DeclaredType

      相反,您需要编写一个 Provider:

      public class FooProvider implements Provider<Foo> {
      
          Context context;
          private static int CODE = 1;
      
          @Inject
          public FooProvider(Context context) {
              this.context = context;
          }
      
          @Override
          public Foo get() {
              return new Foo(context, CODE);
          } 
      }
      

      并将Foo 绑定到模块中的该提供程序

      binder.bind(Foo.class).toProvider(FooProvider.class);

      并从Foo 的构造函数中删除@Inject

      我建议您遍历您的对象图并在其中包含原语的构造函数上查找 @Inject 注释。如上所述删除注释并为它们编写提供程序。 RoboBlender 将正确构建 AnnotationsDatabaseImpl 并且您的项目将编译。

      【讨论】:

        【解决方案2】:

        好吧,我找到了一种解决方法,我只是禁用了 AnnotationDatabase 处理并删除了 RoboBlender 依赖项,从而解决了我的问题。我仍然想知道我为什么会遇到这个问题。

        【讨论】:

          【解决方案3】:

          我有同样的问题,在我的例子中,有一个有 2 个构造函数的类:

          @Inject
          public PaymentSelectionDialog(Context context) {
              this.context = context;
          }
          
          @Inject
          public PaymentSelectionDialog(Context context, PaymentSelectable paymentSelectable) {
                  this.context = context;
                  this.paymentSelectable = paymentSelectable;
          

          我使用第一个构造函数没有问题,但是当我使用第二个构造函数实例化我的对象时,我遇到了这个问题。所以问题是 Roboguice 试图注入一个实现 PaymentSelectable 接口的对象,但这个对象没有在任何模块中定义。

          也许您正在使用的构造函数带有您没有在任何模块中定义的引用。

          希望对你有帮助!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-30
            • 1970-01-01
            • 2018-11-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多