为什么Android Studio 项目中有两个build.gradle 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23241681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:56:40  来源:igfitidea点击:

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

androidandroid-studiogradleandroid-gradle-pluginbuild.gradle

提问by Sabuncu

After having imported an Eclipse project into Android Studio, I see two build.gradlefiles:

将 Eclipse 项目导入 Android Studio 后,我看到两个build.gradle文件:

1 - <PROJECT_ROOT>\build.gradle
2 - <PROJECT_ROOT>\app\build.gradle

The first version is shorter, the second version contains definitions for compileSdkVersion, etc.

第一个版本较短,第二个版本包含对compileSdkVersion等的定义。

What is the purpose behind having two separate files? Are there separate build tasks?

拥有两个独立文件的目的是什么?是否有单独的构建任务?

采纳答案by Gabriele Mariotti

<PROJECT_ROOT>\app\build.gradleis specific for app module.

<PROJECT_ROOT>\app\build.gradle特定于app 模块

<PROJECT_ROOT>\build.gradleis a "Top-level build file"where you can add configuration options common to all sub-projects/modules.

<PROJECT_ROOT>\build.gradle是一个“顶级构建文件”,您可以在其中添加所有子项目/模块通用的配置选项。

If you use another module in your project, as a local library you would have another build.gradlefile: <PROJECT_ROOT>\module\build.gradle

如果您在项目中使用另一个模块,作为本地库,您将拥有另一个build.gradle文件: <PROJECT_ROOT>\module\build.gradle

For examplein 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

在你的 app\build.gradle

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
}

回答by SMR

From the official documentation:

来自官方文档:

Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build.gradlefile for build settings specific to that module.

Android Studio 项目包含一个顶级项目 Gradle 构建文件,该文件允许您添加项目中所有应用程序模块通用的配置选项。每个应用程序模块也有自己的build.gradle文件,用于特定于该模块的构建设置。

enter image description here

在此处输入图片说明

Project Build File

项目构建文件

<PROJECT_ROOT>\build.gradleor the Project Build Fileis for the entire projectso it will be used for global project configurations. A typical Project Build Filecontains the following:

<PROJECT_ROOT>\build.gradle或者项目构建文件用于整个项目,因此它将用于全局项目配置。典型的项目构建文件包含以下内容:

  • buildscript which defines:
    • repositories and
    • dependencies
  • Gradle Plugin version
  • buildscript 定义:
    • 存储库和
    • 依赖
  • Gradle 插件版本

By default, the project-level Gradle file uses buildscriptto define the Gradle repositoriesand dependencies. This allows different projects to use different Gradle versions. Supported repositories include JCenter, Maven Central, or Ivy. This example declares that the build script uses the JCenter repository and a classpath dependency artifact that contains the Android plugin for Gradle version 1.0.1.

默认情况下,项目级 Gradle 文件使用buildscript来定义 Gradle存储库依赖项。这允许不同的项目使用不同的 Gradle 版本。支持的存储库包括 JCenter、Maven Central 或 Ivy。此示例声明构建脚本使用 JCenter 存储库和包含适用于 Gradle 版本 1.0.1 的 Android 插件的类路径依赖项工件。



Module Build File

模块构建文件

<PROJECT_ROOT>\app\build.gradleor the Module Build Fileis for a specific moduleso it will be used for specific module level configs. A Module Build Filecontains the following:

<PROJECT_ROOT>\app\build.gradle或者模块构建文件用于特定模块,因此它将用于特定模块级配置。一个模块构建文件包含以下内容:

  • android settings
    • compileSdkVersion
    • buildToolsVersion
  • defaultConfig and productFlavors
    • manifest properties such as applicationId, minSdkVersion, targetSdkVersion, and test information
  • buildTypes
    • build properties such as debuggable, ProGuard enabling, debug signing, version name suffix and testinformation
  • dependencies
  • 安卓设置
    • 编译SDK版本
    • 构建工具版本
  • defaultConfig 和 productFlavors
    • 清单属性,例如 applicationId、minSdkVersion、targetSdkVersion 和测试信息
  • 构建类型
    • 构建属性,例如可调试、ProGuard 启用、调试签名、版本名称后缀和测试信息
  • 依赖

you can read the official docs here:

你可以在这里阅读官方文档:

Projects and modules build settings

项目和模块构建设置