The question is published on by Tutorial Guruji team.
Is it possible to make gradle rosolve android project’s dependencies dynamically like ant does?
What I want to acheeve is to be able to add/remove library projects (modifieng only project.properties file for corresponding project) without a need to rewrite buil.gradle script.
Ant resolves it by reading project.properties recursively getting a list of all library projects involved into specific build with the following prdefined task:
<getlibpath projectPath="${basedir}" libraryFolderPathOut="project.library.folder.path" />
What I’ve found currently in gradle is to define a list of projects manually in settings.gradle
:
include 'GradleTest_lib1' include 'GradleTest_lib2' project(':GradleTest_lib1').projectDir = new File('../GradleTest_lib1') project(':GradleTest_lib2').projectDir = new File('../GradleTest_lib2')
And add dependecies into build.gradle file:
dependencies { compile fileTree('libs') compile project('GradleTest_lib1') compile project('GradleTest_lib2') }
I beleive I’m missing something and there should be a simple way to find and compile all the lib-projects ‘on the fly’
Answer
Currently I solved this task with the following script
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; } } }
and build.gradle
:
dependencies { compile fileTree('libs') subprojects.findAll{ compile project(it.name) } }