Is it possible to integrate Dagger and AspectJ in an Android Studio project?

吃可爱长大的小学妹 提交于 2019-12-05 05:40:23

It looks that you want to use runtime weaving but as I know Dalvik doesn't support this, so you should change your gradle script to get AspectJ compile-time weaving, here you can find two solutions:

OceanLife

I have got this working but would still be considered a "hack" at this stage since you can't simply press the "run" button to install a weaved-dagger-injected app. Here is what I found whilst trying to reinstate my Spring -like @Transactional database transaction creation point-cuts.

Follow the advice from this SO response and add in the manual AspectJ parts to your application's build.gradle. Attempt to run the project, my project compiled and installed fine but then failed with the dredded;

"Please ensure code generation has been run on this module"

...error that Dagger generates. Suspecting that it was the weaving emptying the /gen folder created by dagger prior to changing the .class bytecode I commented out the line;

apply plugin: 'android-aspectj'

...and re-ran the application without making any code changes. To my surprise the weaved classes that were generated in the first attempt were maintained and then the dagger module instructions appended! This had the effect of keeping the AspectJ generated code and overlaying the Dagger module instructions! Success? Kind of...there is clearly a conflict (probably around the clearing down of the "gen" folder) that I'll look to overcome next. But, and it's an important but, I am running an app with AspectJ AOP code that uses Dagger for DI... progress...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!