129

After having imported an Eclipse project into Android Studio, I see two build.gradle files:

1 - <PROJECT_ROOT>\build.gradle
2 - <PROJECT_ROOT>\app\build.gradle

The first version is shorter, the second version contains definitions for compileSdkVersion, etc.

What is the purpose behind having two separate files? Are there separate build tasks?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • Similar question to [Difference between build.gradle(Project) and build.gradle(Module)](http://stackoverflow.com/questions/28295933/difference-between-build-gradleproject-and-build-gradlemodule) – Suragch Aug 30 '16 at 15:06

2 Answers2

99

<PROJECT_ROOT>\app\build.gradle is specific for app module.

<PROJECT_ROOT>\build.gradle is a "Top-level build file" where you can add configuration options common to all sub-projects/modules.

If you use another module in your project, as a local library you would have another build.gradle file: <PROJECT_ROOT>\module\build.gradle

For example in your top level file you can specify these common properties:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
    }
}

ext {
    compileSdkVersion = 23
    buildToolsVersion = "23.0.1"
}

In your app\build.gradle

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
}
Graham
  • 7,431
  • 18
  • 59
  • 84
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • +1 Thank you, will accept your answer momentarily. Is the `build.gradle` file's presence mandatory to trigger a build? Also, does the top level build file call sub-level files? (Although there is no evidence of that.) – Sabuncu Apr 23 '14 at 10:45
  • The build.gradle files are mandatory if you want to build a project with gradle. Android Studio doesn't require gradle, but it is highly recommended. The top level file doesn't call sub files; gradle works with tasks, but it is difficult to explain in a comment. – Gabriele Mariotti Apr 23 '14 at 10:54
  • Thanks so much Gabriele, this will get me started. – Sabuncu Apr 23 '14 at 10:59
  • 1
    Especially the `rootProject.ext.compileSdkVersion` construct is very informative. – Sabuncu Apr 23 '14 at 11:01
  • 2
    Is it important to take the detour using ext{...} or should we directly apply the android plugin and the compileSdkVersion to the top-level build file? – PhilLab May 04 '15 at 08:04
  • So if you are using Facebook SDK, then I would use top-level build.gradle – Daniel Viglione Jan 31 '17 at 00:28
  • I've sometime seen the same dependancy classpath directed to be put in project build or app build depending on doc, for instance SMR's answer below directs dependancies in both gradles. Is there any reason to put a dependancy classpath in the app build gradle if it's already in the project build gradle? – Androidcoder Jun 17 '20 at 15:23
  • @Androidcoder if there are multiple modules and for example you want to use a plugin only in 1 module you can put it inside the module/build.gradle file. – Gabriele Mariotti Jun 17 '20 at 15:57
29

From the official documentation:

Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build.gradle file for build settings specific to that module.

Enter image description here

Project Build File

<PROJECT_ROOT>\build.gradle or the Project Build File is for the entire project, so it will be used for global project configurations. A typical Project Build File contains the following:

  • buildscript which defines:
    • repositories and
    • dependencies
  • Gradle Plugin version

By default, the project-level Gradle file uses buildscript to define the Gradle repositories and dependencies. This allows different projects to use different Gradle versions. Supported repositories include JCenter, Maven Central, or Ivy. This example declares that the build script uses the JCenter repository and a classpath dependency artifact that contains the Android plugin for Gradle version 1.0.1.


Module Build File

<PROJECT_ROOT>\app\build.gradle or the Module Build File is for a specific module so it will be used for specific module level configurations. A Module Build File contains the following:

  • android settings
  • compileSdkVersion
  • buildToolsVersion
  • defaultConfig and productFlavors
    • manifest properties such as applicationId, minSdkVersion, targetSdkVersion, and test information
  • buildTypes
    • build properties such as debuggable, ProGuard enabling, debug signing, version name suffix and testinformation
  • dependencies

You can read the official documentation here:

Projects and modules build settings

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SMR
  • 6,628
  • 2
  • 35
  • 56