Why Are There Two Build.Gradle Files in an Android Studio Project

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

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

What is the difference of two build.gradles in project root directory and in app folder?

The "app" folder is just one module. When writing more complex apps you may have multiple modules. The build.gradle at the module level relates only to that module whereas the build.gradle at the root relates to all modules in the project.

Example from project level build.gradle:

allprojects {
repositories {
jcenter()
}
}

This is saying all modules should use jcenter repository for resolving dependencies. Now a potentially confusing thing is a terminology clash between gradle and android studio. An android studio "module" is a gradle "project" hence "allprojects".

Multiple build.gradle files in Android Studio

If your project contains multiple modules, you may use the root (project-level) build.gradle file to keep configuration common to all modules. E.g. you may put common dependencies there.

Default top-level build.gradle for Android Studio has "allprojects" block in the top-level build.gradle that adds "jcenter" repository to every module.

Difference between build.gradle (Project) and build.gradle (Module)

build.gradle (Project:My-app)

Top-level build file where you can add configuration options common to
all sub-projects/modules.

Each project contains a top-level Gradle file. It usually contains common configurations for all modules. Whatever is included in this top-level Gradle gile, it will affect all modules.

Example:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'

//Maven plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle (Module:app)

Build file of your specific module (where you add your dependencies, signing configurations, build types, flavors, etc.)

All modules have a specific Gradle file. Whatever is included in this gradle file, it will only affect the module that is included on.

Example:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.hrskrs.gesturefun"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
zipAlignEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':gesture-fun')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
}

Android Studio's project gradle file changed?


But now the project grade file seems different.

Yes, starting with the new Bumblebee update of Android Studio, the build.gradle (Project) file is changed. In order to be able to use Google Services, you have to add to your build.gradle (Project) file the following lines:

plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'com.google.gms.google-services' version '4.3.0' apply false br>}

task clean(type: Delete) {
delete rootProject.buildDir
}

And inside your build.gradle (Module) file, the following plugin IDs:

plugins {
id 'com.android.application'
id 'com.google.gms.google-services' br>}

In this way, your Firebase services will finally work.

P.S. Not 100% sure if the Firebase Assistant is updated with these new Android Studio changes.

Edit 2022-14-07:

Here is a repo that uses this new structure:

  • https://github.com/alexmamo/FirestoreCleanArchitectureApp

Edit:

There is nothing changed regarding the way you add dependencies in the build.gradle (Module) file. If for example, you want to add Firestore and Glide, please use:

dependencies {
//Regular dependecies
implementation platform("com.google.firebase:firebase-bom:29.0.4")
implementation "com.google.firebase:firebase-firestore"
implementation "com.github.bumptech.glide:glide:4.12.0"
}

Edit2:

With the new updates, you don't need to worry about adding any lines under the repositories { }. That was a requirement in the previous versions.

The build.gradle files conflict and create two totally same application on the device

Remove this from AndroidManifest.xml of omimhikingbook:

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>


Related Topics



Leave a reply



Submit