Hi, again I rad gradle documentation but still don...
# getting-started
n
Hi, again I rad gradle documentation but still don't clearly understand how it works. When I create a very new project in IDEA as Mabile Android/iOS I got a project with multiple gradle connected files and directories. On project structure top where are two files : build.gradle, settings.gradle. settings.gradle is quite clear for me. But build.gradle is not :
Copy code
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
    }
}
repositories {
    google()
    jcenter()
}
The main thing what I don't understand is purpose of 'buildscript' section, I mean why not put everything outside of this section? What is the difference between 'repositories' inside and outside 'buildscript' (btw where is another project included into parent 'app' and it have his own 'repositories' section inside his build.gradle file )?
c
Gradle is definitely a bit of a beast to learn at first, but once you get a grasp of the basics of how it works and it to use its build scripts, it becomes more clear. The first thing to understand is that
.gradle
files are code. Specifically, Groovy code (or optionally, Kotlin). They are not markup files like Maven’s
pom.xml
or NPM’s
package.json
, it is actually real code that Gradle will compile ane execute when you run your Gradle build. That first block in the root
build.gradle
file for
buildscript
is used to configure that initial compilation step. Before it can run any plugins, you have to tell Gradle where it can find them and how it can include them in the script’s classpath so that it can even properly execute the rest of the code in that script and the other
build.gradle
scripts. The dependencies in the
buildscript
block never touch your application code. From there, Gradle separates a full “build” into “projects” which roughly correspond to your project’s top-level directories. First, the root directory becomes a project, and then
app
becomes a second “project”, and so on with
settings.gradle
being executed to determine the exact locations of those projects. For each of those projects, you then need to configure the application code and determine which dependencies are included in your app and the repositories those deps come from. So the
repositories
block inside
buildscript
is used to configure the actual script execution, while the
repositories
block in a project’s
build.gradle
file is used to configure the application being built by Gradle.
n
@Casey Brooks Thanks a lot for detailed answer!
n
there is also the better new plugin-dsl https://stackoverflow.com/a/43128713 here is how it can be used with the android plugin looks a bit nicer imo
n
@Nikky Thanks!