Skip to content

Dynamic project config

Update .gitignore

Add following file to ignore

ignore
local.json

Create example file example.local.json

json5
[
  {
    "name": "projectName1",
    "path": "/path/to/project1",
    "include": true,
    "subprojects": [
      {
        "name": "subprojectName1",
        "include": true
      }
    ]
  },
  {
    "name": "projectName2",
    "path": "/path/to/project2",
    "include": true, //Set as false to exclude from build. If false all subprojects also excluded
    "subprojects": [] //Add subprojects as needed
  }
]
  • name: Name of the project exactly as it would appear in your dependencies (e.g., "libs").
  • path: Absolute path to the project directory in your filesystem (e.g., "/develop/git/libs")
  • include: Specifies whether to include this project in your build. When true, Gradle will attempt to substitute dependencies with this project.
  • subprojects: Array of subprojects that belong to the parent project, following the same structure (name, and include)

Update the settings.gradle

groovy

def jsonLocalPath = "./local.json"


def jsonFile = file(jsonLocalPath)

class LocalConfigProject {
    String name
    String path
    boolean include
    ArrayList<LocalConfigProject> subprojects
}

if (jsonFile.exists()) {

    def jsonSlurper = new JsonSlurper()

    LocalConfigProject localConfig = jsonSlurper.parse(jsonFile)


    localConfig.each { conf ->
        if (conf.include) {
            def name = conf.name
            def path = conf.path
            def subProjects = conf.subprojects

            include ":${name}"
            project(":${name}").projectDir = new File(path)

            if (subProjects) {
                subProjects
                        .findAll { it.include }
                        .each {
                            def subProject = ":${name}:${it.name}"
                            include subProject
                            project(subProject)?.name = it.name
                            project(subProject)?.path = Path.path(":${it.name}")

                        }
            }


        }
    }
}

update build.gradle or other dev.gradle

groovy

class LocalConfigProject {
  String name
  String path
  boolean include
  ArrayList<LocalConfigProject> subprojects
}

def jsonLocalFile = file("./local.json")

if (jsonLocalFile.exists()) {
  def jsonSlurper = new JsonSlurper()

  LocalConfigProject localConfig = jsonSlurper.parse(jsonLocalFile)

  ArrayList<String> substitutionList = []

  localConfig.each {
    if (it.include) {
      substitutionList.push(it.name)
      def subs = it.subprojects

      if (subs) {
        subs.findAll { it.include }
          .each { sub -> substitutionList.push(sub.name) }
      }
    }
  }
  configurations.all {
    resolutionStrategy {
      preferProjectModules()
      dependencySubstitution {
        substitutionList.each {
          substitute module("<projectGroup>:${it}") using project(":${it}")
        }
      }
    }
  }
}

Refresh the gradle project

The guide below

Local development setup (Project Substitution)

Step 1: Define Local Projects in local.json

Define the local projects that you wish to interconnect in a file named local.json example. This should follow the structure:

json5
[
  {
    "name": "projectName1",
    "path": "/path/to/project1",
    "include": true,
    "subprojects": [
      {
        "name": "subprojectName1",
        "include": true
      }
    ]
  },
  {
    "name": "projectName2",
    "path": "/path/to/project2",
    "include": true, //Set as false to exclude from build. If false all subprojects also excluded
    "subprojects": [] //Add subprojects as needed
  }
]
  • name: Name of the project exactly as it would appear in your dependencies (e.g., "libs").
  • path: Absolute path to the project directory in your filesystem (e.g., "/develop/git/libs")
  • include: Specifies whether to include this project in your build. When true, Gradle will attempt to substitute dependencies with this project.
  • subprojects: Array of subprojects that belong to the parent project, following the same structure (name, and include)

Step 2: Run

Save changes and refresh Gradle project. Gradle should now detect the local project and substitute the project dependencies with the local projects.

Always ensure that the path in local.json file is correct and the project to be substituted is correctly build in local environment.

Private Licensed