Use a Library in the processor's generated cla

2019-04-08 02:57发布

问题:

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

回答1:

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 e.g :-

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


回答2:

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



回答3:

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/



回答4:

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