0

An Android app uses a library (aar module) that uses "com.google.gson.Gson". The library has the following in its build.gradle:

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

The app builds fine, but generates the following error when it starts:

Failed resolution of: Lcom/google/gson/Gson;

The only way to solve it is adding the same compile line to the app's build.gradle:

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

Could anyone shed some light on this?

[Edit]

The library was added with the standard procedure that created a folder under the app called "androidLibrary-release". The following line has been added to the build.gradle of the app:

compile project(':androidLibrary-release')
Hong
  • 17,643
  • 21
  • 81
  • 142
  • can you specify the library? – mayosk Jan 20 '17 at 14:48
  • "An Android app uses a library" -- how? Is the library published to a Maven-style repo, and the app is referencing it from there? Is the library being used by `compile project(':modulename')`? Are you doing something else? – CommonsWare Jan 20 '17 at 14:53
  • Yes, the library is used by compile project(':modulename'). @mayosk I will edit the post by adding how the library is used. – Hong Jan 20 '17 at 15:05

1 Answers1

1

Libraries don't include their dependencies. It is up to the developer to include them as necessary in the app modules that implement them. However, if this library comes from a Maven repo, it is possible to include the information about which dependencies the library uses and they will be fetched when your project is built.

Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35
  • Thanks for the response. The app does not use com.google.code.gson:gson directly. Only some classes of the library use it. – Hong Jan 20 '17 at 15:39
  • @Hong you will still need to add the dependency if you want it to work correctly. – Roberto Betancourt Jan 20 '17 at 16:29
  • This is a bit strange because the library has many other dependencies. The app will crash only when the app uses the methods of the library using a dependency not included in the app. This means an app will rely on crashes to find out which additional dependencies should be added. – Hong Jan 20 '17 at 17:11
  • @Hong well it should rely on proper documentation, not crashes. Please mark the answer as correct if it was useful. – Roberto Betancourt Jan 21 '17 at 01:21
  • This is unfortunate. I assume using jar library does not have this problem because I have never experienced this issue with numerous jar libraries that I have used. I don't why aar cannot do the same. – Hong Jan 22 '17 at 01:19
  • 1
    @RobertoBetancourt Could you please provide the source of your statement?! I'm facing the same problem and would like to get a better understanding of the background. – CarHa Oct 31 '17 at 03:44