简体   繁体   English

如何在Gradle项目中插入新的依赖项?

[英]How to insert a new dependency into a Gradle project?

I am very new to Gradle (I always have used Maven in the past. 我是Gradle的新手(过去我一直使用Maven

I have to add this dependence to an Android project: https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Quick-Setup 我必须将此依赖项添加到Android项目中: 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: 因此,在阅读库文档时只说使用Gradle我必须添加此依赖项:

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: 我的问题是,在我的项目中,我有两个不同的build.gradle文件:

1) build.gradle refered to the project: 1) build.gradle引用项目:

// 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 2) build.gradle引用 模块应用

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? 我必须在哪里精确地放置以前的新依赖项?

app\\build.gradle or referred to Module app is specific for app module. app\\build.gradle或引用的模块app是特定于应用程序模块的。 As in Android Studio one can more than one module for a project. 与Android Studio中一样,一个项目可以有多个模块。 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. 例如,您现在拥有一个应用程序模块,您可以为您的项目添加一个watch模块,该模块将具有与智能手表相关的工作,例如表盘等。

build.gradle is a "Top-level build file" where you can add configuration options common to all modules. build.gradle是一个“顶级构建文件”,您可以在其中添加所有模块通用的配置选项。

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. 例如,现在您正在将UIL添加到与应用程序模块直接相关的项目中。 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). 因此,您应该在dependencies部分或功能中将其添加到您的应用模块中(老实说,我不知道这叫什么,如果有人可以对此进行指导,我将不胜感激)。 So you dependencies part should look like this 所以你的dependencies部分应该看起来像这样

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

您需要在依赖关系部分的模块级别 build.gradle文件中放置依赖关系

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

Each project contains one top-level Gradle build file. 每个项目都包含一个顶级Gradle构建文件。 This file is named build.gradle and can be found in the top level directory. 该文件名为build.gradle,可以在顶级目录中找到。 This file usually contains common config for all modules, common functions.. 该文件通常包含所有模块的通用配置和通用功能。

All modules have a specific build.gradle file. 所有模块都有一个特定的build.gradle文件。 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 该文件包含有关此模块的所有信息(因为一个项目可以包含更多模块),如config,build tyoes,用于签名apk的信息,依赖项

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.. 您要添加到应用程序或模块中的任何新依赖项都应在build.gradle(Module app)下,并添加依赖项,只需编写github页面上给出的compile语句即可。

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'

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM