简体   繁体   English

在处理器生成的类中使用库

[英]Use a Library in the processor's generated classes

I'm developing a library to generate classes using annotations and processors. 我正在开发一个库来使用注释和处理器生成类。 The generated classes should use Gson library from google. 生成的类应该使用谷歌的Gson库。

My question is : Where should I add the Gson dependency ? 我的问题是: 我应该在哪里添加Gson依赖项? 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中,但是当生成类时,找不到Gson并且Android Studio建议将其添加到app模块。

build.gradle of the processor : build.gradle 处理器

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 : 应用程序 的build.gradle:

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

Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

PS The project is meant to be a library. PS该项目旨在成为一个图书馆。 I expect the users to only include my library in their gradle file, not the "sub dependency". 我希望用户只将我的库包含在他们的gradle文件中,而不是“子依赖”。

Simply using implementation prevent you from using transitive dependency . 简单地使用实现可以防止您使用传递依赖

The main project can't use or invoke the dependencies you added to your library. 主项目无法使用或调用您添加到库中的依赖项。

As reminded to docs the implementation configuration should be used to declare dependencies which are internal to the component. 正如提醒文档的那样,应该使用实现配置来声明组件内部的依赖关系。

So you have to use api OR compile instead of implementation or add a condition to your transitive library eg :- 因此,您必须使用apicompile而不是implementation或向您的传递库添加条件,例如: -

implementation ('com.google.code.gson:gson:2.8.1'){
    transitive = true;
}

Besides the fact implementation hides the dependency and therefore keeping it out of the app's path (so if you need to expose Gson, avoid using implementation), the problem is due to being compiling/implementing the library module instead of using a regular aar/jar/apklib package. 除了事实实现隐藏依赖关系并因此将其保持在应用程序的路径之外(因此,如果您需要公开Gson,避免使用实现),问题是由于编译/实现库模块而不是使用常规的aar / jar / apklib包。 Try this: 试试这个:

-Add the Android Maven Gradle plugin to the library project and configurate it. - 将Android Maven Gradle插件添加到库项目并配置它。

-Using the plugin, compile and install the library into your local maven repository. - 使用插件,编译并将库安装到本地maven存储库中。 -instead of using compile/api/implementation for adding the library to the app, include mavenLocal() into your build.gradle, and then add the library as a regular dependency. - 不使用compile / api / implementation将库添加到app,将mavenLocal()包含到build.gradle中,然后将库添加为常规依赖项。 All the necessary dependencies should be there. 所有必要的依赖都应该存在。

here's an example of the plugin, in case you need it: https://github.com/fcopardo/EnhancedMapView 这是插件的一个例子,如果你需要它: https//github.com/fcopardo/EnhancedMapView

Okay, I finally fixed the problem! 好的,我终于解决了这个问题!

I had to add the Gson dependency into the processor build.gradle using implementation or compile : 我不得不使用implementation compileGson依赖项添加到处理器 build.gradle中:

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

And add the dependency again into the annotation build.gradle using compile : 并使用compile将依赖项再次添加到注释 build.gradle中:

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). 我认为它不起作用,因为Gson依赖是在处理器 build.gradle中,它包含使用annotationProcessor 'lib-processor' (不知何故,“transitivity”没有应用)。 Therefore I put the Gson dependency in the annotation build.gradle since I include it using implementation 'lib-annotation' and it worked. 因此,我将Gson依赖项放在注释 build.gradle中,因为我使用implementation 'lib-annotation'包含它并且它有效。

Hope it will help 希望它会有所帮助

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

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