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

After importing the Eclipse project into Android Studio, I see two build.gradle :

 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 of two separate files? Are there separate build tasks?

+82
android android-studio android-gradle build.gradle gradle
Apr 23 '14 at 10:32
source share
2 answers

<PROJECT_ROOT>\app\build.gradle specific to the application module .

<PROJECT_ROOT>\build.gradle is a "top-level build file" where you can add configuration parameters common to all subprojects / modules.

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

In the example in the top-level file, you can specify these general properties:

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' } } ext { compileSdkVersion = 23 buildToolsVersion = "23.0.1" } 

In app\build.gradle

 apply plugin: 'com.android.application' repositories { mavenCentral() } android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion } 
+59
Apr 23 '14 at 10:41
source share

From the official documentation:

Android Studio projects contain a top-level Gradle project assembly file that allows you to add configuration parameters 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 Project build file for the entire project , so it will be used for global project configurations. A typical project build file contains the following:

  • buildscript that defines:
    • repositories and
    • dependencies
  • Gradle Plugin Version

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




Module assembly file

<PROJECT_ROOT>\app\build.gradle or The module assembly file is intended for a special module , therefore it will be used for certain module level configurations. The module assembly file contains the following:

  • Android settings
    • compileSdkVersion
    • buildToolsVersion
  • defaultConfig and productFlavors
    • exhibit properties such as applicationId, minSdkVersion, targetSdkVersion and test data
  • buildTypes
    • Create properties such as debugging, enabling ProGuard, debugging signature, version name suffix, and test information.
  • dependencies

You can read the official docs here:

Project and module assembly settings

+14
Mar 02 '15 at 12:06
source share



All Articles