1

是否可以像 ant 那样动态地使 gradle rosolve android 项目的依赖项?

我想要实现的是能够添加/删除库项目(仅修改相应项目的 project.properties 文件),而无需重写 buil.gradle 脚本。

Ant 通过递归读取 project.properties 来解决它,并使用以下预定义任务获取涉及特定构建的所有库项目的列表:

<getlibpath projectPath="${basedir}" libraryFolderPathOut="project.library.folder.path" />

我目前在 gradle 中发现的是手动定义项目列表settings.gradle

include 'GradleTest_lib1'
include 'GradleTest_lib2'

project(':GradleTest_lib1').projectDir = new File('../GradleTest_lib1')
project(':GradleTest_lib2').projectDir = new File('../GradleTest_lib2')

并将依赖项添加到 build.gradle 文件中:

dependencies {
    compile fileTree('libs')
    compile project('GradleTest_lib1')
    compile project('GradleTest_lib2')
}

我相信我遗漏了一些东西,应该有一种简单的方法来“即时”查找和编译所有 lib 项目

4

2 回答 2

2

目前我用以下脚本解决了这个任务

settings.gradle

def props = readAllLibProjects()
println props
loadLibProjectsFromProps(props)

def Properties readAllLibProjects(){
        def props = new Properties()
    return readAllLibProjects("", props, 1)
}

def Properties readAllLibProjects(String basePath, props, int level){
Properties localProps = new Properties();
new File(basePath + "project.properties").withInputStream {
    stream -> localProps.load(stream) 
}
localProps.each{
    String key = it.key
    String value = it.value
    if(key.contains("android.library.reference")){
        key = key + "_" +level;
        props.put(key, value);
        readAllLibProjects(it.value + "/", props, level + 1)
    }
}
return props
}

def loadLibProjectsFromProps(Properties props){
props.each{
    if(it.key.contains("android.library.reference")){
        String projectPath = it.value;
        String projectName = projectPath.split("/").last();
        include projectName
        project(":"+projectName).projectDir = new File(projectPath);
        println "project's path = " + projectPath;
        println "project's name = " + projectName;
    }
}
}

build.gradle

dependencies {
    compile fileTree('libs')
    subprojects.findAll{
    compile project(it.name)
    }
}
于 2013-11-13T12:15:31.363 回答
0

我试图制作相同的项目来重现它,但是当我在 settings.gradle 中添加包含时编译了子项目,没有编译项目('...')

无论如何,在你的 build.gradle 文件中试试这个

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    getAllprojects().each{ Project proj ->
        //exclude root project
        if(proj != project) {
            compile proj
            //it's just for debug to see names of compiled projects
            println proj.getName()
        }    
    }
}
于 2013-11-13T08:45:23.167 回答