SoManyGoblins
SoManyGoblins

Reputation: 5917

Multi-Project Android Gradle Build - Sharing configuration

I am trying to migrate a rather large Android project from Maven to Gradle, but I'm running into some problems I am not sure how to solve.

The structure is as follows (out of my control, for now).

- MainModule
-- Core UI
--- UI Module 1
---- src
------ main
------- java
-- Core Logic
--- Logic Module 1
--- Logic Module 2
--- ...
-- Services
--- Service Module 1
--- Service Module 2
--- ...

Main Module contains no code. The "src/main/java" structure is replicated in all the other modules (Logic Modules, Services Modules, UI Modules).

1- I need to access a local Artifactory, for this we need to enter our user credentials. I have three different repositories to look for artifacts (snapshots, releases, and another I don't recall). I need to use the same credentials for all of them. How can I define these credentials and declare the repositories and share the repositories (with the credentials) across all build.gradle files? I more or less understood that I can define a repository (and credentials) like this:

repositories {
     maven {
         credentials {
             username "mavenUser"
             password "mavenPassword"
         }

         url "myurl"
     }
}

However I don't know how to share this definition across all gradle build files, and I don't think copying and pasting across so many modules is the best idea. Also, it'd be useful to get the user and password from either an environment variable, or something like Maven's settings.xml.

2- We need to periodically make a release and have the artifacts published in the local Artifactory, and have the version number be incremented automatically, like we do on Maven, any pointers on how to achieve this? (granted, I haven't researched this that much, I'm stuck on 1)

3- I want to share groupId and version across all build.gradle files for the project.

4- I want to be able to declare dependencies once for multiple (all) sub modules.

Any help is appreciated.

Upvotes: 2

Views: 775

Answers (1)

JoMo
JoMo

Reputation: 81

1a,3,4) You can define an allProjects or a subProjects closure that applies whatever goes into it to all respective projects. e.g.

allprojects {
  group = "myGroup"
  version = "myVersion"

  repositories {
    maven {
      credentials {
        username "mavenUser"
        password "mavenPassword"
      }

      url "myurl"
    }
  }
}

subprojects {
  dependencies {
    compile "group:artifact:version"
  }
}

In fact, given this build.gradle in your root project you don't need to give your subprojects a build.gradle if they have nothing else special about them. They will all be given a group and version from the injected configuration and will automatically pull their name from the directory name. However, overriding these defaults or adding in extra dependencies etc. can be done as normal.

1b) You could put your username and password in a gradle.properties:

auth.user="user"
auth.pass="pass"

then call them as standard variables in the buildscript.

Upvotes: 2

Related Questions