简体   繁体   English

"build.gradle(项目)和 build.gradle(模块)之间的区别"

[英]Difference between build.gradle (Project) and build.gradle (Module)

I am trying to add a dependency of Android Asynchronous Http Client into my project.我正在尝试将 Android Asynchronous Http Client 的依赖项添加到我的项目中。 So there are two build.gradle files in the project.所以项目中有两个build.gradle文件。

在此处输入图像描述

As per my understanding, there are different kind of dependencies:据我了解,有不同类型的依赖项:

  1. One which defined on the root level of build.gradle (Project:My-app)build.gradle (Project:My-app) 的根级别定义的一个
  2. One inside the buildscript of the build.gradle (Project:My-app) build.gradle (Project:My-app) 的 buildscript 中的一个
  3. Another is build.gradle (Modules:app)另一个是 build.gradle (Modules:app)

This question is about repositories for dependencies of the buildScript, explain a bit about first two types. 这个问题是关于 buildScript 依赖项的存储库,解释一下前两种类型。

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

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

So I guess the dependency code of Android Asynchronous Http Client should be added in build.gradle (Module:app).所以我猜想Android Asynchronous Http Client的依赖代码应该加在build.gradle (Module:app)中。

How does it all fit together?这一切如何结合在一起?

build.gradle (Project:My-app) build.gradle (项目:我的应用程序)

Top-level build file where you can add configuration options common to all sub-projects/modules.顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

Each project contains a top-level Gradle file .每个项目都包含一个顶级 Gradle 文件 It usually contains common configurations for all modules .它通常包含所有modules通用配置 Whatever is included in this top-level Gradle gile, it will affect all modules .无论这个顶级 Gradle 文件中包含什么,它都会影响所有模块

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.gradle(模块: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.所有模块都有一个特定的 Gradle 文件。 Whatever is included in this gradle file, it will only affect the module that is included on.无论这个gradle文件中包含什么,它只会影响包含的模块

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

It's a bit confusing because Android Studio by default shows both build.gradle files right next to each other (when using the Android view).这有点令人困惑,因为默认情况下,Android Studio 将两个build.gradle文件build.gradle显示(使用 Android 视图时)。

在此处输入图片说明

If you switch to the Project view you can see the actual structure and where the different build.gradle files are located.如果您切换到 Project 视图,您可以看到实际的结构以及不同的build.gradle文件所在的位置。

在此处输入图片说明

The build.gradle (Project: MyApplication) file is in the root folder of the project and its configuration settings apply to every module in the project. build.gradle (Project: MyApplication) 文件位于项目的根文件夹中,其配置设置适用于项目中的每个模块。 A module is an isolated piece of the bigger project.模块是更大项目的一个孤立部分。 In a multi-module project, these modules have their own jobs but work together to form the whole project.在一个多模块的项目中,这些模块各司其职,但共同组成了整个项目。 Most Android projects only have one module, the app module.大多数 Android 项目只有一个模块,即 app 模块。

The build.gradle (Module: app) file here is in the app folder.此处的build.gradle (Module: app) 文件位于app文件夹中。 Its build settings apply only to the app module.它的构建设置仅适用于 app 模块。 If there were another module, then that module would have its own build.gradle file, too.如果有另一个模块,那么该模块也会有自己的build.gradle文件。 As an example , I made a library project with three modules: a library module, a demo app module, and another app module that I plan to use for testing.例如,我创建了一个包含三个模块的库项目:一个库模块、一个演示应用程序模块和另一个我计划用于测试的应用程序模块。 Each of them have their own build.gradle files that I can tweak.他们每个人都有自己的build.gradle文件,我可以调整。

在此处输入图片说明

In a basic project, almost everything you need to edit will be in the app module's build.gradle file.在基本项目中,您需要编辑的几乎所有内容都在应用程序模块的build.gradle文件中。 You can remember it like this:你可以像这样记住它:

You're making an app , so go to the build.gradle (Module: app ) file.你正在制作一个app ,所以转到build.gradle (Module: app ) 文件。

Further reading进一步阅读

About the relation of the two gradle files, hrskrs made a very clear explanation,and I will make some supplement about it.关于两个gradle文件的关系, hrskrs已经解释的很清楚了,我会做一些补充。

If your project only has one Module (such as app ), the advantage of the top build.gradle (Project:My-app) not show very clear.如果你的项目只有一个Module(比如app ),上面的build.gradle(Project:My-app)的优势就体现的不是很明显了。 Because you can configure everything in build.gradle (Module:app) about the Module, and only modify one file when upgrading in the following days.因为你可以在build.gradle(Module:app)中配置关于Module的一切,并且在接下来几天升级的时候只修改一个文件。

But if your project has five modules, and it happened that they have a same dependence A , if you don't use the top build.gradle (Project: My-app) you need to maintain five files in the following days.但是如果你的项目有五个模块,而且恰好它们有相同的依赖A ,如果你不使用最上面的build.gradle (Project: My-app)你需要在接下来的几天维护五个文件。

By the way, the build.gradle (Module:app) can overwrite the build.gradle (Project:My-app) .顺便说一句, build.gradle (Module:app)可以覆盖build.gradle (Project:My-app)

This design can improve the maintainability of the app.这种设计可以提高应用程序的可维护性。

[Project vs Module] [项目与模块]

Projects' build.gradle file is used for common/shared logic.项目的build.gradle文件用于公共/共享逻辑。 For example, you can define repositories here ( Maven , Google, JCenter , and custom) or specify ext {} with shared variables or classpath [About] .例如,您可以在此处定义存储库( Maven 、Google、 JCenter和 custom)或使用共享变量或classpath [About]指定ext {}

Module's build.gradle is used for the current module, like dependencies, minSdkVersion, targetSdkVersion, compileSdkVersion [About] , ProGuard settings [About] .模块的build.gradle用于当前模块,如依赖项、minSdkVersion、targetSdkVersion、compileSdkVersion [About] 、ProGuard 设置[About] Usually as a developer you should take care of this file.通常作为开发人员,您应该照顾好这个文件。

Things get more clear when you have multi project module, then differences of module vs project gradle are more clear.当您拥有多个项目模块时,事情变得更加清晰,然后模块与项目 gradle 的差异更加清晰。

You can use project gradle for defining required classpaths, plugins, source repositories (google, maven etc) to fetch dependencies from and module level Gradle with respective dependencies with top-level module having signing info, version details, build types, product flavours etc .您可以使用项目 gradle 来定义所需的类路径、插件、源存储库(google、maven 等) ,以从模块级 Gradle 获取依赖项,并使用具有签名信息、版本详细信息、构建类型、产品风格等的顶级模块的相应依赖项 Further lower-level modules mostly are concerned with the dependencies needed wrt that particular module, so in general we just use lower modules's build gradle for adding specific dependencies.更底层的模块主要关注特定模块所需的依赖关系,所以通常我们只使用较低级别模块的构建 gradle 来添加特定的依赖关系。

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

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