Anil Bhaskar Anil Bhaskar - 3 months ago 50
Android Question

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

I am trying to add a dependency of Android Asynchronous Http Client into my project. So there are two build.gradle files are there in the project.

enter image description here

As long as i understood, there are different kind of dependencies:


  1. One which defined on the root level of build.gradle(Project:My-app)

  2. One inside the buildscript of the build.gradle(Project:My-app)

  3. Another is build.gradle(Modules:app)



This question is about repositories for dependencies of the buildScript, explain a bit about first two types.

Also build.gradle(Project:My-app) says

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




So i guess dependency code of Android Asynchronous Http Client should be added in build.gradle(Module:app).

If someone could give a clear picture of all of these for better understanding, it would be great.

Answer

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 usally contains common configs for all modules. Whatever is included in this top-level gradle, it will affect all modules.

ex:

// 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 configs, 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.

ex:

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