Luis404
Luis404

Reputation: 87

How to use nested dependencies in Android gradle?

I am facing a problem while using gradle:

I have a library project, it has two library modules(called libA, libB), libA is the project which I push to my local Maven, and libB is one of libA's dependency as this:

  compile project (':libB')

After uploading to maven, I use libA in my app as a dependency

  compile 'com.luis.lib:libA:1.0'

For some reason, I am also using libB's API in the app, so while I am compiling, error happens, it can't find libB. error code:

Error:A problem occurred configuring project ':app'.

Could not resolve all dependencies for configuration ':app:_coohuaOnlineTestReleaseCompile'. Could not find CoohuaFramework:qrscanner:unspecified. Searched in the following locations: repo1.maven.org/maven2/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.pom repo1.maven.org/maven2/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.jar maven.coohua.com:8002/nexus/content/repositories/releases/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.pom maven.coohua.com:8002/nexus/content/repositories/releases/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.jar jcenter.bintray.com/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.pom https://jcenter.bintray.com/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.jar file:/Users/douhua/.m2/repository/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.pom file:/Users/douhua/.m2/repository/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.jar file:/Users/douhua/Library/Android/sdk/extras/android/m2repository/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.pom file:/Users/douhua/Library/Android/sdk/extras/android/m2repository/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.jar file:/Users/douhua/Library/Android/sdk/extras/google/m2repository/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.pom file:/Users/douhua/Library/Android/sdk/extras/google/m2repository/CoohuaFramework/qrscanner/unspecified/qrscanner-unspecified.jar Required by: CooHuaClient:app:unspecified > com.coohua.framework:CoohuaFramework:1.6.5

I don't know why, I also tried 'transitive=true' but it didn't help.

BUT: when I moved all the code of libB to libA as part of it. everything works, I can use libB's code in the app.

Could anyone kindly tell me why this happen and how to fix it?

Upvotes: 4

Views: 765

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364858

You have pushed the libA into a Maven repo.

Since your LibA has

compile project (':libB')

in the pom file, the libA has a dependency with the libB.

You have to push the libB into the Maven Repo

This will resolve your issue, because when your project uses:

 compile 'com.luis.lib:libA:1.0'

Gradle will resolve the nested dependecies reading the pom file.

I don't know why, I also tried 'transitive=true' but it didn't help.

The transitive=true means that you want to download the nested dependencies. I dont' think that transitive=false will resolve the issue because the problem in your pom file with an unknown dependency.

BUT: when I moved all the code of libB to libA as part of it. everything works, I can use libB's code in the app.

It resolves becasue in this way you don't add a dependency but embed the code inside the libA.

Upvotes: 1

Daniël van den Berg
Daniël van den Berg

Reputation: 2365

Unfortunately you're going to have to add libB as a dependency as well, as there's no way to do that automatically. Also, you should not be copying all code from libB to libA. I found that out the hard way.

Imagine making two new libraries, libC and libD. libC requires libA, and so does libD. Now if you'd include the code from libA in libC and libD, you'll end up with a lot of double code. If you then have a project where you want to use both libC and libD, you're in trouble because the code from libA became ambiguous, it's supplied twice.

So, my reccomendation, also pushing libB and adding compile 'com.luis.lib:libB:1.0' to your build.gradle.

Upvotes: 0

Related Questions