模块中的gradle aar library dependecy:无法解析

时间:2015-07-22 08:44:29

标签: android android-gradle

我是一个有篮球的新手,我有一个依赖性问题。我有以下项目结构:

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

MyAppProMyAppFree都取决于MyAppLibrary,这取决于ThirdPartyLibraryWrapper。顾名思义,ThirdPartyLibraryWrapper是外部库的包装器,即ThirdPartyLibrary.aar

这是我的配置:

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'

}

当gradle sync完成后,我遇到了这个错误:

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

有人可以帮我找出问题所在吗?

1 个答案:

答案 0 :(得分:31)

其他项目发现:ThirdPartyLibraryWrapper项目依赖于名为ThirdPartyLibrary-0.1.0:aar的工件。 Java(和Android)库不会将它们自己的依赖项捆绑在一起 - 相反,它们只是发布它们的依赖项列表。然后,耗费项目不仅负责加载它直接依赖的库,而且还负责加载库所依赖的所有库。

这样做的最终结果是:MyAppFree加载了:ThirdPartyLibraryWrapper,然后看到:ThirdPartyLibraryWrapper取决于ThirdPartyLibrary-0.1.0:aar,因此也尝试将其加载到:MyAppFree中。但是,ThirdPartyLibrary-0.1.0:aar并不知道repositories的生活地点......所以它失败了。

解决方案是在所有其他项目中放置类似的repositories { flatDir { dirs project(':ThirdPartyLibraryWrapper').file('libs') } } 块。试试这个:

project(...).file(...)

使用background-方法将使您无需硬编码路径,而是使用Gradle DSL通过查找项目并让它动态地执行分辨率来解析文件系统路径。