有没有办法通过返回布尔值来知道是否在gradle文件中编译了某个依赖项

时间:2016-07-12 10:03:02

标签: android gradle groovy android-gradle build.gradle

所以情况就是这样,在依赖结构的build.gradle文件中我有

obj1 = {
    field1: {
    col1: 1,
    col2: 'a'
  }
};
function checkExistence(arr) {
  // if arr is a string convert it into an array before checking it
  if (typeof arr == 'string') {
    arr=arr.split('.');
  }
  checked = arr[0]; i = 1;
  checks = window.hasOwnProperty(checked);
  while (checks && i < arr.length) {
    checked+='.'+arr[i];
    checks = typeof eval(checked) != 'undefined';
    ++i;
  }
  if (!checks) console.log(checked+' is undefnied');
  else console.log(checked+' is defined');
  return checks;
}
checkExistence(['obj1','field1','col1']);
// obj1.field1.col1 is defined
checkExistence(['obj1','field1','col3']);
// obj1.field1.col3 is undefnied
checkExistence(['obj1','field2','col3']);
// obj1.field2 is undefnied
checkExistence(['obj2','field2','col3']);
// obj2 is undefnied
checkExistence('obj1.field1.col1');
// obj1.field1.col1 is defined

...

if (checkExistence('obj1.field2.value1')) {
    // do something
}

但是我希望人们能够只编译A或只编译B,有没有办法知道是否通过返回一个可以在其他地方使用的全局布尔值,在gradle任务中使用依赖关系A?

所以换句话说

dependencies {
    compile 'A'
    compile 'B'
}

1 个答案:

答案 0 :(得分:1)

您可以获得所有编译依赖项:

def compile = configurations.compile.allDependencies*.with{"$it.group:$it.name:$it.version".toString()}

它将以group:name:version格式返回所有依赖项的列表。然后你可以使用:

if("org.codehaus.groovy:groovy-all:2.4.7" in compile) {
     println "org.codehaus.groovy:groovy-all:2.4.7 was compiled"
}