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.

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!

PS 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".


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 :-

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. Try this:

-Add the Android Maven Gradle plugin to the library project and configurate it.

-Using the plugin, compile and install the library into your local maven repository. -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. All the necessary dependencies should be there.

here's an example of the plugin, in case you need it: https://github.com/fcopardo/EnhancedMapView


follow this link, I have imported without any problems. http://blog.madadipouya.com/2015/09/21/how-to-add-gson-library-to-android-studio-project/

链接地址: http://www.djcxy.com/p/97082.html

上一篇: 散列多态类型是正确的方式

下一篇: 在处理器生成的类中使用库