在处理器生成的类中使用库 [英] Use a Library in the processor's generated classes
问题描述
我正在开发一个库来使用注释和处理器生成类.生成的类应该使用来自 google 的 Gson
库.
我的问题是:我应该在哪里添加 Gson 依赖项?我目前正在将它添加到处理器 build.gradle 中,但是当生成类时,找不到 Gson 并且 Android Studio 建议将其添加到应用模块.
处理器的build.gradle:
实现项目(':lib-annotation')实现 'com.squareup:javapoet:1.9.0'实现 'com.google.code.gson:gson:2.8.1'实现 'com.google.auto.service:auto-service:1.0-rc3'
应用的
build.gradle:
实现项目(':lib-annotation')annotationProcessor 项目(':lib-processor')
任何帮助将不胜感激,谢谢!
P.S. 该项目旨在成为一个图书馆.我希望用户只在他们的 gradle 文件中包含我的库,而不是子依赖项".
好的,我终于解决了这个问题!
我必须使用 implementation
或 将
:Gson
依赖项添加到 processor build.gradle编译
实现 'com.google.code.gson:gson:2.8.1'
并使用 compile
将依赖项再次添加到 annotation build.gradle 中:
编译'com.google.code.gson:gson:2.8.1'
<小时>
我认为它不起作用,因为 Gson
依赖项位于使用 annotationProcessor 'lib-processor' 包含的 processor build.gradle 中
代码>(不知何故传递性"不适用).因此,我将 Gson
依赖项放在 annotation build.gradle 中,因为我使用 implementation 'lib-annotation'
包含它并且它起作用了.>
希望能帮到你
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屋!