不创造知识,我只是知识的搬运工。本篇文章照抄https://developer.android.com/studio/build/#build-config,建议直接点击链接
下面的工程目录示意图
1. settings.gradle文件位于项目根目录,用于指示Gradle在构建应用时应将哪些模块包括在内。对大多数项目而言,该文件很简单,只包括以下内容
include ‘:app’
不过,多模块项目需要指定应包括在最终构建之中的每个模块。
2.顶级 build.gradle文件位于项目根目录,用于定义适用于项目中所有模块的构建配置。默认情况下,这个顶级构建文件使用buildscript()代码块来定义项目中所有模块共用的Gradle存储区和依赖项。以下代码示例描述和默认设置和DSL元素可以在新建项目后的顶级buile.gradle文件中找到
/**
* The buildscript {} block is where you configure the repositories and
* dependencies for Gradle itself--meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
* buildscript {}块是您为Gradle本身配置存储库和依赖项的地方 - 这意味着,您不应在此处包含模块的依赖项。
* 例如,此块包含Gradle 的Android插件作为依赖项,因为它提供了Gradle构建Android应用程序模块所需的其他指令。
*/
buildscript {
/**
* The repositories {} block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*/
repositories {
jcenter()
}
/**
* The dependencies {} block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android Plugin for Gradle
* version 3.2.1 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
/**
* The allprojects {} block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. Dependencies that are not required by all the modules in the
* project should be configured in module-level build.gradle files. For new
* projects, Android Studio configures JCenter as the default repository, but it
* does not configure any dependencies.
*/
allprojects {
repositories {
jcenter()
}
}
3. 模块级build.gridle文件位于每个
以下的实例Android应用模块build.gradle文件概述了您应该了解的部分基本DSL元素和设置
/**
* The first line in the build configuration applies the Android plugin for
* Gradle to this build and makes the android {} block available to specify
* Android-specific build options.
*/
apply plugin: 'com.android.application'
/**
* The android {} block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*
* buildToolsVersion specifies the version of the SDK build tools, command-line
* utilities, and compiler that Gradle should use to build your app. You need to
* download the build tools using the SDK Manager.
*/
compileSdkVersion 28
buildToolsVersion "28.0.3"
/**
* The defaultConfig {} block encapsulates default settings and entries for all
* build variants, and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
/**
* applicationId uniquely identifies the package for publishing.
* However, your source code should still reference the package name
* defined by the package attribute in the main/AndroidManifest.xml file.
*/
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdkVersion 15
// Specifies the API level used to test the app.
targetSdkVersion 28
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes {} block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies Proguard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the Proguard settings file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors {} block is where you can configure multiple product
* flavors. This allows you to create different versions of your app that can
* override defaultConfig {} with their own settings. Product flavors are
* optional, and the build system does not create them by default. This example
* creates a free and paid product flavor. Each product flavor then specifies
* its own application ID, so that they can exist on the Google Play Store, or
* an Android device, simultaneously.
*/
productFlavors {
free {
applicationId 'com.example.myapp.free'
}
paid {
applicationId 'com.example.myapp.paid'
}
}
/**
* The splits {} block is where you can configure different APK builds that
* each contain only code and resources for a supported screen density or
* ABI. You'll also need to configure your build so that each APK has a
* different versionCode.
*/
splits {
// Screen density split settings
density {
// Enable or disable the density split mechanism
enable false
// Exclude these densities from splits
exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
}
}
}
/**
* The dependencies {} block in the module-level build configuration file
* only specifies dependencies required to build the module itself.
*/
dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:28.0.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
4. Gradle属性文件,Gradle还包含两个属性文件,位于项目根目录,可用于指定适用于Gradle构建工具包本身的设置。
gradle.properties,你可以在其中配置项目范围Gradle设置,例如Gradle后台进程最大堆大小。
local.properties,为构建系统配置本地环境属性,例如SDK安装路径。该文件的内容由Android Studio自动生成并且专用于
本地开发环境,因此你不应该手动修改该文件,或将其纳入你的版本控制系统。
5. 源集
Android studio 按照逻辑关系将每个模块的源代码和资源分组为源集。模块的main/源集包括其所有构建变体共用的代码
和资源。其他源集目录为可选项,在您配置新的构建变体时,Android Studio不会自动为您创建这些目录。不过,创建类似于main/的源集有助于让Gradle只在构建特定应用版本时使用的文件和资源井然有序。
src/main/此源集包括所有构建变体共用的代码资源
src/
src/
src/
例如,要生成应用的“完整调试”版本,构建系统需要合并来自以下源集的代码、设置和资源:
src/fullDebug/
(构建变体源集)src/debug/
(构建类型源集) 类型:表示是正式版本还是测试版本src/full/
(产品风味源集) 风味:表示不同的版本的应用src/main/
(主源集)如果不同源集包含同一文件的不同版本,Gradle 将按以下优先顺序决定使用哪一个文件(左侧源集替换右侧源集的文件和设置):构建变体 > 构建类型 > 产品风味 > 主源集 > 库依赖项
这样一来,Gradle 便可使用专用于您试图构建的构建变体的文件,同时对与其他应用版本共用的 Activity、应用逻辑和资源加以重复利用。在合并多个清单时,Gradle 使用同一优先顺序,这样每个构建变体都能在最终清单中定义不同的组件或权限。如需了解有关创建自定义源集的更多信息。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章