Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle aar library dependecy in module: failed to resolve

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?

like image 834
evi Avatar asked Jul 22 '15 08:07

evi


1 Answers

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.

like image 179
tophyr Avatar answered Nov 10 '22 01:11

tophyr