Why are there two build.gradle files in an Android Studio project?

Sabuncu picture Sabuncu · Apr 23, 2014 · Viewed 47.9k times · Source

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?

Answer

Gabriele Mariotti picture Gabriele Mariotti · Apr 23, 2014

<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
}