Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a new dependency into a Gradle project?

I am very new to Gradle (I always have used Maven in the past.

I have to add this dependence to an Android project: https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Quick-Setup

So, reading into the library documentation it only say that using Gradle I have to add this dependency:

https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Quick-Setup

My problem is that, into my project, I have 2 differents build.gradle files:

1) build.gradle refered to the project:

// 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:1.5.0'

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

allprojects {
    repositories {
        jcenter()
    }
}

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

2) build.gradle refered to Module app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.android.pastafromrome"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

What are the difference between these 2 files? Where exacctly I have to put the previous new dependency?

like image 412
AndreaNobili Avatar asked Jun 02 '16 10:06

AndreaNobili


3 Answers

app\build.gradle or referred to Module app is specific for app module. As in Android Studio one can more than one module for a project. For example, an app module that you have now, you can add a watch module for your project that will have the work related to your smart watch, like watch faces etc.

build.gradle is a "Top-level build file" where you can add configuration options common to all modules.

You should add the dependencies to your required module. For example, as now you are adding UIL to your project which is directly related to your app module. So you should add it your app module in the dependencies part or function ( I honestly don't know what this is called, if someone can guide me on this I'll be grateful). So you dependencies part should look like this

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}
like image 105
Atif Farrukh Avatar answered Sep 25 '22 12:09

Atif Farrukh


You need to put dependency in Module level build.gradle file in dependencies section

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
like image 26
Mehta Avatar answered Sep 24 '22 12:09

Mehta


Each project contains one top-level Gradle build file. This file is named build.gradle and can be found in the top level directory. This file usually contains common config for all modules, common functions..

All modules have a specific build.gradle file. This file contains all info about this module (because a project can contain more modules), as config,build tyoes, info for signing your apk, dependencies

Any new dependency that you want to add to your application or module should go under build.gradle(Module app) and to add the dependency just write the compile statement that is given on the github page..

compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

So, final code should be like..

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

}
like image 33
hemdroid Avatar answered Sep 25 '22 12:09

hemdroid