What is the role of 'com.google.code.gson:gson:2.6.1' dependency in retrofit 2 when it has its own gson converter?

僤鯓⒐⒋嵵緔 提交于 2021-01-28 09:12:05

问题


// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'

// JSON Parsing
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

My question is regarding the second and third dependency added. I do understand it is related to JSON conversion. If the third dependency is added,would the second dependency still be needed?

I have seen them both being added on several examples. For e.g: https://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/ https://code.tutsplus.com/tutorials/sending-data-with-retrofit-2-http-client-for-android--cms-27845


回答1:


At the source of Retrofit Gson contverter library, there is a dependency of Gson.

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
</dependency>

So, when you include com.squareup.retrofit2:converter-gson as your dependency, com.google.code.gson:gson will be automatically included.

So, you will not need to include the Gson dependency explicitly.



来源:https://stackoverflow.com/questions/49399098/what-is-the-role-of-com-google-code-gsongson2-6-1-dependency-in-retrofit-2-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!