在处理器生成的类中使用库 [英] Use a Library in the processor's generated classes

查看:130
本文介绍了在处理器生成的类中使用库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个库,使用注释和处理器来生成类。生成的类应该使用谷歌的 Gson 库。



我的问题是:我应该在哪里添加Gson依赖?我目前将其添加到处理器build.gradle中,但是当生成类时,Gson未找到,并且Android Studio正在建议将其添加到应用程序模块。



build.gradle 处理器:

  $'$'$'$'$'$'$'$'$'$'$ $ $ $ $ $'$'$'$'$'$'$'$'$'$'$' b $ b implementation'com.google.auto.service:auto-service:1.0-rc3'

app build.gradle

  implementation project(': lib-annotation')
annotationProcessor项目(':lib-processor')

将不胜感激,谢谢!



P.S。该项目旨在成为图书馆。我希望用户只将我的库包含在他们的gradle文件中,而不是子依赖项。

解决方案

好吧,我终于解决了问题!



我必须将 Gson 依赖项添加到处理器版本.gradle使用实现 编译

  implementation'com.google.code.gson:gson:2.8.1'

然后使用 compile 再次将依赖关系添加到注释 build.gradle中:

  compile'c​​om.google.code.gson:gson:2.8.1'






我认为它不起作用,因为 Gson 依赖项在使用 annotationProcessor'lib-processor'包含的 processor build.gradle(不知何故传递性不适用)。因此,我将 Gson 依赖项放在注释 build.gradle中,因为我使用 implementation'lib-annotation' / code>并且工作。



希望这会有所帮助


I'm developing a library to generate classes using annotations and processors. The generated classes should use Gson library from google.

My question is : Where should I add the Gson dependency ? I'm currently adding it into the processor build.gradle but when the classes are generated, Gson is not found and Android Studio is suggesting to add it to the app module.

build.gradle of the processor :

implementation project(':lib-annotation')
implementation 'com.squareup:javapoet:1.9.0'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.google.auto.service:auto-service:1.0-rc3'

build.gradle of the app :

implementation project(':lib-annotation')
annotationProcessor project(':lib-processor')

Any help would be greatly appreciated, thanks!

P.S. The project is meant to be a library. I expect the users to only include my library in their gradle file, not the "sub dependency".

解决方案

Okay, I finally fixed the problem!

I had to add the Gson dependency into the processor build.gradle using implementation or compile :

implementation 'com.google.code.gson:gson:2.8.1'

And add the dependency again into the annotation build.gradle using compile :

compile 'com.google.code.gson:gson:2.8.1'


I think it wasn't working because the Gson dependency was in the processor build.gradle which is included using annotationProcessor 'lib-processor' (Somehow the "transitivity" wasn't applying). Therefore I put the Gson dependency in the annotation build.gradle since I include it using implementation 'lib-annotation' and it worked.

Hope it will help

这篇关于在处理器生成的类中使用库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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