Gradle中的Android库可以将其他Android库作为依赖项吗?

时间:2013-06-03 14:47:34

标签: android gradle android-library

是否允许这种结构?

MyProject/
 | settings.gradle
 + app/
    | build.gradle
 + libraries/
    + lib1/
       | settings.gradle
       | build.gradle
       + libraries/
          + sublib1/
             | build.gradle
          + sublib2/
             | build.gradle
    + lib2/
       | build.gradle

我使用了Ant,但是使用Gradle我的项目找不到sublib1或sublib2。

当我运行gradle时,我收到此错误:

* What went wrong:
A problem occurred evaluating project ':libraries:lib1'.
> Project with path ':libraries:sublib1' could not be found in project ':libraries:lib1'.

来自 MyProject settings.gradle

include ':libraries:lib1'
include ':libraries:lib2'

来自 MyProject build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':libraries:lib1')
    compile project(':libraries:lib2')
}

android {
    compileSdkVersion 15
    buildToolsVersion "17.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

来自 lib1 settings.gradle

include ':libraries:sublib1'
include ':libraries:sublib2'

来自 lib1 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        compile project(':libraries:sublib1')
        compile project(':libraries:sublib2')
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 15
}

2 个答案:

答案 0 :(得分:3)

Gradle似乎不支持多个settings.gradle文件。此文件应仅存在于项目根目录中,并应包括所有子项目:

include ':libraries:lib1', ':libraries:lib1:libraries:sublib1', ':libraries:lib1:libraries:sublib2', ':libraries:lib2'

来源:http://www.gradle.org/docs/current/userguide/multi_project_builds.html

答案 1 :(得分:0)

您可以在库项目中将其他库作为依赖项。 在您的settings.gradle中,只包含所有应用程序/库模块。

在依赖于另一个库模块的库项目中,您只需将其作为依赖项添加,就像项目中的任何其他模块一样。

在我的一个库模块中,gradle中的依赖关系如下所示:

dependencies {
    compile project(':library1')
    compile project(':library2')
    compile project(':library3')
}