Gradle 模块中的 aar 库依赖项:解析失败

Gradle aar library dependecy in module: failed to resolve

我是 gradle 的新手,我遇到了依赖性问题。我有以下项目结构:

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

MyAppProMyAppFree都依赖于MyAppLibrary,而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 同步完成时,我收到此错误:

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

谁能帮我找出问题出在哪里?

其他项目发现 :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(...) 方法将使您不必对路径进行硬编码,而是使用 Gradle DSL 通过查找项目并让它动态解析来解析文件系统路径.