注解处理器+ androidTest + dagger2 [英] annotationProcessor + androidTest + dagger2

查看:168
本文介绍了注解处理器+ androidTest + dagger2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于有仪器的测试,我有一个TestApplication创建了TestComponent,但是不再生成文件(Error:/xxx/TestApplication.java:16: The import.xxx.DaggerTestApplicationComponent cannot be resolved).我无法确定根本原因.我尝试了不同的Android Studio(2.2、2.1.2),不同的gradle插件(2.2.0-alpha6、5、4)和不同版本的Dagger(2.2至2.6).

我应该使用androidTestAnnotationProcessor吗? (以前不是这种情况)

要使用dagger 2.6,需要添加classpath 'com.google.guava:guava:19.0'

更新:Module出现问题,因此无法创建Component.但是,使用jack(即使带有调试选项),也看不到问题.现在,返回到Java 7,gradle插件2.1.2.这样,无需指定哪个guava版本,就可以使用所有最新的lib(dagger 2.6,butterknife 8.2.1,apt 1.8)

解决方案

我在Dagger 2.9中遇到了相同的问题. compileDebugAndroidTestSources成功完成,但未生成Dagger*Component.

经过大约一个小时的努力,我找到了这个问题,最后通过一些实验找到了解决方案:

添加

androidTestAnnotationProcessor 'com.google.dagger:dagger-compiler:2.9'

,然后再次执行compileDebugAndroidTestSources.现在,如果图形还可以,则应该生成源(您可能必须临时注释掉对Dagger*Component的引用才能使编译成功).

如果图形存在问题(例如,缺少@Provides),则会出现错误(与之前没有androidTestAnnotationProcessor的状态相反,在该状态下任务已完成且没有错误,但未生成源)

<rant>

我曾经爱过Dagger,但时不时地会有一些奇怪的问题,这使它对于长期的商业项目而言是很大的风险.而且他们仍然没有合适的文档,这使得新开发人员很难学习它.可能不仅仅是Dagger的错,也许gradle和Android Studio参与了这些问题,但我正在认真考虑将其丢弃.

</rant>

For instrumented tests, I have a TestApplication that creates a TestComponent, but the file is not generated anymore (Error:/xxx/TestApplication.java:16: The import.xxx.DaggerTestApplicationComponent cannot be resolved). I'm not able to identify the root cause. I've tried different Android Studio (2.2, 2.1.2), different gradle plugin (2.2.0-alpha6, 5, 4) and different versions of dagger (2.2 to 2.6).

Should I use androidTestAnnotationProcessor? (that was not the case before)

edit: to use dagger 2.6, need to add classpath 'com.google.guava:guava:19.0'

update: there was a problem with a Module, hence the Component couldn't be create. However, using jack (even with debug options), I couldn't see the problem. For now, reverting to java 7, gradle plugin 2.1.2. That way, no need to specify which guava version, and all the latest libs can be used (dagger 2.6, butterknife 8.2.1, apt 1.8)

解决方案

I had the same problem with Dagger 2.9. compileDebugAndroidTestSources was completing successfully but the Dagger*Component was not generated.

After struggling for about an hour I landed on this question and finally with some experiments found a solution:

Add

androidTestAnnotationProcessor 'com.google.dagger:dagger-compiler:2.9'

and execute compileDebugAndroidTestSources again. Now sources should be generated (you may have to temporary comment out references to your Dagger*Component in order compilation to succeed) if your graph is OK.

If there is a problem with your graph (e.g. missing @Provides) now you will get an error (in contrast of the previous state without androidTestAnnotationProcessor where the task was completing without an error but sources were not generated)

<rant>

I used to love Dagger but every now and then there are some strange problems with it that make it a big risk for long commercial projects. Also they still don't have decent documentation for it which makes learning it by new developers very hard. Probably it is not just Dagger's fault, probably gradle and Android Studio have their part in the problems but I am seriously considering to dump it.

</rant>

这篇关于注解处理器+ androidTest + dagger2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆