74

Right now I have a library project, say project Foo that depends on a library like OkHttp.

Now, Foo has a Maven buildstep that generates an AAR and pushes it up to a public place.

Now lets say I have project B, we'll call it Bar. Bar is an Android application, and Bar depends on Foo.

Well, I have that. However, when I make a call to a public static function in Foo from Bar that calls OkHttp, I get this message:

java.lang.NoClassDefFoundError: com.squareup.okhttp.OkUrlFactory
            at com.foo.sdk.utils.OkHttpStack.<init>(OkHttpStack.java:15)

Is such a thing possible? Or will Bar need to manually depend on OkHttp as well as any other dependencies Foo has?

Lii
  • 11,553
  • 8
  • 64
  • 88
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • `compile` was deprecated, you should use api for every dependency which is needed for the public API of your library. Refer this answer for more detail https://stackoverflow.com/questions/44413952/gradle-implementation-vs-api-configuration – Weiyi Mar 10 '20 at 00:55

1 Answers1

63

It took a little while but I found what I was looking for. It just happened to be the way I was wording my searches.

This lesser-seen answer was exactly what I was looking for:

Transitive dependencies not resolved for aar library using gradle

Note that dependencies are only associated with aar libraries if they are hosted in a maven repository, in the first place, as the pom file is not included in the aar.

Essentially, I needed to add a

transitive = true

...to the build.gradle of Bar

Example:

compile ('com.foo:FOO:1.0.0@aar'){
       transitive=true
}

This way it includes all of my transitive libraries.

Note, however, that this may actually cause conflicts between dependencies (especially local ones) which can be resolved using an exclude tag.

fuzzyTew
  • 3,511
  • 29
  • 24
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • 2
    I'm following that approach without much success. Accordingly to the previously explained method, my settings should be as follows: `compile ('com.my:library:2.0.1-SNAPSHOT@aar'){ transitive = true }` But when I import the final library in my project, the transitive lib is missing. I have no clue about how to proceed, any idea? – Michael Knight Nov 16 '16 at 08:49
  • 38
    @MichaelKnight If you include aar as local file using flatDir then it means that aar does not contain transitive dependencies, you should include them manually. However, if you're fetching aar from, for example, maven repo, then library has description of what dependencies it is using (in pom file) and gradle can automatically fetch them for you. – NazarK Nov 23 '16 at 12:36