繁体   English   中英

添加RoboBlender时,不会构建Gradle项目

[英]Gradle project does not build when I add RoboBlender

我已将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进程。

    是什么造成这种情况,我该如何解决?

    使用@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转换为引用类型javax.lang.model.type.DeclaredType

    相反,您需要编写提供者:

    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 ,您的项目将进行编译。

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

    我有同样的问题,在我的情况下,有一个包含2个构造函数的类:

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

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

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

    希望能帮助到你!

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM