Use the library in classes created by the processor

I am developing a library for creating classes using annotations and processors. Generated classes must use the Gson library from Google.

My question is: Where should I add the Gson dependency? I am now adding it to the build.gradle processor, but when the classes are generated, Gson is not found, and Android Studio offers to add it to the application module.

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

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

Any help would be greatly appreciated, thanks!

PS The project should be a library. I expect that users will only include my library in their gradle file, and not "dependency dependent".

+7
java android gson annotations dependencies
source share
4 answers

Ok, I finally fixed the problem!

I had to add a Gson dependency to the Gson processor using implementation or compile :

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

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

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

I think it did not work, because the Gson dependency was in the build.gradle processor , which is enabled using the annotationProcessor 'lib-processor' (somehow, the โ€œtransitionโ€ was not applied). So I put the Gson dependency in the Gson annotation , since I included it with the implementation 'lib-annotation' , and it worked.

Hope this helps

0
source share

Just use does not allow the use of transitive dependency .

The main project cannot use or reference dependencies added to your library.

As docs recalled, the implementation configuration should be used to declare dependencies that are internal to the components.

Thus, you should use api OR compile instead of implementation or add a condition to your transitive library, for example: -

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

Besides the fact that the implementation hides the dependency and therefore supports it from the application path (so if you need to expose Gson, avoid using the implementation), the problem is with compiling / implementing the library module, and not using the regular aar / jar / apklib. Try the following:

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

- using the plugin, compiling and installing the library in the local maven repository. instead of using the / api / implementation compilation to add the library to the application, include mavenLocal () in your build.gradle, and then add the library as a regular dependency. All necessary dependencies should be there.

here is an example plugin if you need it: https://github.com/fcopardo/EnhancedMapView

+1
source share
0
source share

All Articles