vir us
vir us

Reputation: 10765

Android gradle with dynamic library projects list

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'

Upvotes: 1

Views: 1852

Answers (2)

vir us
vir us

Reputation: 10765

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)
    }
}

Upvotes: 2

cooperok
cooperok

Reputation: 4267

I tried to make same project to reproduce it, but subprojects was compiled just when i added include in settings.gradle, without compile project('...')

Anyway, try this in you build.gradle file

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()
        }    
    }
}

Upvotes: 0

Related Questions