12

I'm a newbie with gradle and I'm having a dependecy problem. I have the follow project structure:

-MyApp -MyAppLibrary -MyAppPro -MyAppFree -ThirdPartyLibraryWrapper --libs\ThirdPartyLibrary.aar 

Both MyAppPro and MyAppFree depend on MyAppLibrary, which depends on ThirdPartyLibraryWrapper. As the name suggests, ThirdPartyLibraryWrapper is a wrapper on an external library, namely ThirdPartyLibrary.aar.

This is my configuration:

build.gradle MyAppPro

apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.example" minSdkVersion 8 targetSdkVersion 22 } buildTypes { release { minifyEnabled true proguardFiles 'proguard.cfg' } } } dependencies { compile project(':MyAppLibrary') } 

build.gradle MyAppLibrary

apply plugin: 'com.android.library' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 8 targetSdkVersion 22 compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } } buildTypes { release { minifyEnabled true proguardFiles 'proguard.cfg' } } } dependencies { compile project(':ThirdPartyLibraryWrapper') compile 'com.squareup.picasso:picasso:2.5.2' } 

build.gradle ThirdPartyLibraryWrapper

apply plugin: 'com.android.library' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { minSdkVersion 8 targetSdkVersion 22 } buildTypes { release { minifyEnabled true proguardFiles 'proguard.cfg' } } } repositories { flatDir { dirs 'libs' } } dependencies { compile(name: 'ThirdPartyLibrary-0.1.0', ext: 'aar') compile "com.android.support:support-v4:22.0.0" compile fileTree(dir: 'libs', include: 'volley.jar') compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3' } 

When gradle sync completes, I have got this error:

MyApp/MyAppFre/ build.gradle: failed to resolve ThirdPartyLibrary-0.1.0 MyApp/MyAppLibrary/ build.gradle: failed to resolve ThirdPartyLibrary-0.1.0 MyApp/MyAppPro/ build.gradle: failed to resolve ThirdPartyLibrary-0.1.0 

Can someone help me figure out where is the issue?

5
  • Try to remove the - char in the name of aar. Try to use ThirdPartyLibrary_0.1.0 changing the name in the libs folder and in the gradle file. Commented Jul 22, 2015 at 8:59
  • Still not working. Please consider that ThirdPartyLibraryWrapper build is successful, I think that the problem arise while being imported in others modules. Commented Jul 22, 2015 at 9:17
  • Try to aAdd the same compile(name: 'ThirdPartyLibrary-0.1.0', ext: 'aar') with the right folder, in the other modules (MyAppLibrary...) Commented Jul 22, 2015 at 9:27
  • I already test this solution and it works, but in such case I will broke my logic... and have no sense to have a librarywrapper (which I need to have anyway ) Commented Jul 22, 2015 at 9:52
  • The problem is that MyAppLibrary doesn't know where ThirdPartyLibrary-0.1.0:aar is. For this reason you have to specify the repository where you can find it. It works in the same way for all dependencies. Commented Jul 22, 2015 at 10:27

1 Answer 1

33

The other projects are seeing that the :ThirdPartyLibraryWrapper project depends on an artifact called ThirdPartyLibrary-0.1.0:aar. Java (and Android) libraries do not bundle their own dependencies together - instead, they simply publish a list of their dependencies. The consuming project is then responsible for loading not only the library it directly depends on, but all of the libraries that library depends on.

The net effect of this is that :MyAppFree is loading in :ThirdPartyLibraryWrapper, then seeing that :ThirdPartyLibraryWrapper depends on ThirdPartyLibrary-0.1.0:aar and so thus trying to load that in as well. However, :MyAppFree doesn't know where ThirdPartyLibrary-0.1.0:aar lives.. and so it fails.

The solution will be to place similar repositories blocks in all your other projects. Try this:

repositories { flatDir { dirs project(':ThirdPartyLibraryWrapper').file('libs') } } 

Using the project(...).file(...) method will free you from having to hardcode paths, and will instead use the Gradle DSL to resolve the filesystem path by looking up the project and having it do the resolution dynamically.

Sign up to request clarification or add additional context in comments.

7 Comments

Volley.jar is bundled in ThirdPartyLibrary.aar. Unluckily grandle seems not to include .aar file: Warning:Project thirdPartyLibraryWrapper: Only Jar-type local dependencies are supported. Cannot handle: /MyApp/thirdPartyLibraryWrapper/libs/thirdPartyLibrary-0.1.0.aar
So you are correct! Did some more tinkering and found a solution that works; answer updated.
thanks for detailed description about how dependencies works. It is possible to do a bundle with ThirdPartyLibraryWrapper and ThirdPartyLibrary? I think that I will prefer this solution in order to not break my actual logic
It is possible, but it is not simple or easy - it involves the shadowJar or fatJar plugins, and can result in dependency version conflicts that are very hard to find and resolve. Take a peek at the Gradle User Guide's chapter on Dependency Management, specifically section 50.2.2, for more info.
Does the ThirdPartyLibraryWrapper project actually add anything, or is it there solely to give the other projects access to ThirdPartyLibrary? If that is the case then it is unnecessary, and you can just put the ThirdPartyLibrary in a root libs/ dir and have the other projects reference it directly.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.