是否有可能像蚂蚁一样动态地进行gradle rosolve android项目的依赖?
我想要的是能够添加/删除库项目(仅修改相应项目的project.properties文件),而无需重写buil.gradle脚本。
Ant通过读取project.properties来解析它,通过以下prdefined任务递归获取涉及特定构建的所有库项目的列表:
<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-projects'
答案 0 :(得分: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)
}
}
答案 1 :(得分:0)
我尝试制作相同的项目来重现它,但是当我在settings.gradle中添加include而没有编译项目('...')时,子项目就被编译了
无论如何,请在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()
}
}
}