Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can we have 2 build gradle?

Hello , can i create multiple build. gradle for one project? Yes. You can have multiple build files in one project.

How many build gradle file is there in one Android project?

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

How many types of Gradle build files do we have in Android?

Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files.

What is the use of build gradle file in Android?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.


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

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