Nah
Nah

Reputation: 1768

Loading local `maven` repository path for `buildscript` dynamically

I tried to find an appropriate solution for mentioned problem but couldn't find a way out so far, hence posting here to get some correct and precise solution to the problem.

I have included app build.gradle in code versioning being that there are often changes in build.gradle (at plugins dependency levels) I need this file to be tracked in versions - cutting long story short. Now, being every team member having different location for a following specified dependency - I tried to load this from my customLocalPaths.properties but loading variables from .properties file doesn't seem to work here. The url property listed below only seems to accept plain string.

buildscript{
  repositories{
    google()
    jcenter()
    maven{
      url 'D:\\android_workspace_dependencies\\My_XYZ_dependency\\lib'  // <-- need to make this path dynamic
    }
  }
}

Any idea how can I make the url path dynamic (like load it from a variable) instead of having it static string. I use to get unable to resolve path kind of error.

Note: The .properties file works fine. I am using other variables from same .properties file in my app build.gradle without any issues. I also tried to load partial path i.e. single directory name but in vain.

Upvotes: 3

Views: 2082

Answers (2)

M.Ricciuti
M.Ricciuti

Reputation: 12106

First of all, if you want to resolve dependencies from a local libs directory, you should use the flat directory repository type and not a maven repository.

To solve your specific problem, here are at least two simple solutions based on standard Gradle features:

Solution 1 - use Init Script

The Init Script feature allows you to customize Gradle execution based on current build environment. Each team member could declare their specific local repository in their local Init script, as follows:

$USER_HOME/.gradle/init.gradle

allprojects {
    buildscript {
        repositories {
            flatDir {
                dirs "c:/DEV/libs"
            }
            // or with local mavel repo:
            // maven { url  "c:/DEV/libs"  }
        }
    }
}

The project build script will then contain only common/shared repositories

build.gradle

buildscript{
  repositories{
    google()
    jcenter()    
  }
}

Solution 2 - use Gradle user properties

You can use a Gradle property to configure the repository url/path, and let team members provide this property value in their user-specific gradle.properties file:

build.gradle

buildscript {
    repositories {
        google()
        jcenter()  
        if (project.hasProperty("myLocalLibRepo")) {
            flatDir {
                dirs myLocalLibRepo
            }
            // or with local mavel repo:
            //  maven { url  myLocalLibRepo  }
        }
    }
}

Team member's properties file:

$USER_HOME/.gradle/gradle.properties

myLocalLibRepo=c:/DEV/libs

Upvotes: 2

J Fabian Meier
J Fabian Meier

Reputation: 35823

Usually, inside a company you run a Nexus or Artifactory server for the artifact. This the place to put your dependencies, and you need no more local paths.

Upvotes: 0

Related Questions