j2emanue
j2emanue

Reputation: 62529

what is the purpose of the init.gradle file

Can someone explain the meaning of the purpose of this init.gradle file as it seems like just duplicating code to me:

  buildscript {
    repositories {
        maven {
            url "https://someEnterpriseURL"
        }
    }
}

allprojects {
    repositories {
        maven {
            url "https://someEnterpriseURL"
        }
    }
}

the reason for the confusion is that in the projects build.gradle file its defined like this:

    buildscript {
    repositories {
        maven {
            url "https://someEnterpriseURL"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.1'

    }
}

allprojects {
    repositories {
        maven {
            url "https://someEnterpriseURL"
        }
    }
}

so why even have this defined in the init.gradle file ? how does it help developers to have a init.gradle file when im defining the same thing in the build.gradle file?

Upvotes: 5

Views: 15516

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007099

Your build.gradle files are per-project and per-module. Your init.gradle files are set up on a per-${GRADLE_USER_HOME} basis (default is in ~/.gradle or in Gradle's home directory for local Gradle installations). I would expect to see init.gradle files used more commonly in large organizations, trying to standardize some Gradle build policies across multiple projects and development teams.

Quoting the Gradle documentation:

Here are several possible uses:

  • Set up enterprise-wide configuration, such as where to find custom plugins.

  • Set up properties based on the current environment, such as a developer's machine vs. a continuous integration server.

  • Supply personal information about the user that is required by the build, such as repository or database authentication credentials.

  • Define machine specific details, such as where JDKs are installed.

  • Register build listeners. External tools that wish to listen to Gradle events might find this useful.

  • Register build loggers. You might wish to customize how Gradle logs the events that it generates.

Upvotes: 6

samsap
samsap

Reputation: 60

As you know gradle is combination of Ant tool and Maven tool. You might have seen how ant tool works(adding jars or libs locally) and maven tool(adding jars and libs file from cloud and writing script to download it from cloud during building). So basically this init.gradle file is generally used for some jars available globally(generally used for big organisations placed in cloud) which will contain the url or address of server so as to download the jar and cache it in gradle wrapper(you can see those jar files over cloud once downloaded can be seen in .gradle->caches).

Visualize in a prospect like suppose in an organisation which is have one support jar file which keeps updating every now and then and there is another team in that organisation which needs that jar for their application. So here our guy 'init.gradle' help us to solve this problem. Automatically download the latest jar from the server whenever application is build by the later team.

Upvotes: 1

Related Questions