Android构建系统编译你的app资源和源码并且打包到APK中,你可以用来测试,部署,签名和发布。Android Studio使用Gradle,一个高级的构建套件,来自动化和管理构建进程,同时可以允许你灵活的自定义构建配置。每个构建配置可以定义它自己的代码和资源集合。

Gradle和Android插件独立于Android Studio运行。这就意味着你可以在你的机器上在命令行、Android Studio、或者没有安装Android Studio的机器中构建你的Android APP。

Android构建系统的灵活性允许你在不修改你的APP核心代码的情况下运行自定义的构建配置。

构建进程

构建进程包含很多工具和进程来帮助将你的项目转化为APK(Android Application Package)。构建进程非常灵活。



遵循如下几步:

1、 编译器将你的源码转化为DEX(Dalvik Executable)文件,包含Android设备中可以运行的字节码和其他编译的资源文件。

2、 APK打包工具将DEX文件和编译后的资源打包到一个单独的APK中,但是在你安装和部署之前,APK必须进行签名。

3、 APK打包工具会用debug key或者release key对你的APK进行签名。

4、 在你生成最终的APK之前,打包工具只用aipalign工具来优化你的代码。这可以在app运行时降低内存。

自定义构建配置

Build Types

构建类型定义在构建和打包app是Gradle使用的一些属性配置。在不同的开发周期是不同的。比如,debug 构建类型开启调试选项并且使用debug key对APK进行签名。而release 构建类型可能需要压缩、模糊并且使用release key对APK进行签名。为了构建你的app,你必须至少声明一种类型。

Product Flavors

Product flavors代表发布给用户的不同版本的APP。比如,免费和付费的APP版本。你可以通过定制Product flavors来使用不同的代码和资源,同时共用所有版本APP可复用的部分。Product Flavors是可选的,你必须手动创建它们。

Build Variants

Build variant是build type和product flavor混合的产物。这是Gradle用来构建你的app的相关配置。通过使用build variant,你可以在开发中构建你的product flavor的debug版本,或者product flavor的签名的发布版本。虽然你没有直接配置build variant,你可以通过配置build type和product flavor来实现对build variant的配置。创建额外的build type或者product flavor同样可以创建额外的build variant。

Mainfest Entries

你可以在build variant配置里声明manifest文件里的一些属性值。这些值会腹泻manifest文件中已经存在的值。

Dependencies

构建系统管理项目的依赖,从本地的依赖到远程的依赖。这个可以让你方便的对依赖进行管理。

Signing

构建系统允许你在构建配置中声明签名设置,这可以在你构建的时候自动的对你的APK进行签名。构建系统不会对release版本进行签名,除非你定义一个签名配置。

ProGuard

构建系统允许你为每一个build variant声明一个不同的ProGuard规则文件。

构建配置文件

创建自定义的配置需要你对一个或多个构建配置文件进行更改,或者build.gradle文件。这些文本使用DSL来描述和控制构建逻辑。

当你创建一个新的项目时,Android Studio自动为你创建一些文件,如图:



这是一些Gradle构建配置文件,作为Android应用标准项目结构的一部分。

Gradle设置文件

gradle.settings文件位于项目的根目录下,来通知Gradle在构建你的应用的时候需要包括哪些模块,大部分项目如下:

  1. include ‘:app

顶层的构建文件

位于项目根目录的build.gradle文件,定义了可以应用于你的项目的所有模块的构建配置。默认情况下,顶层的构建文件在buildscript{}中定义Gradle repositories和依赖,这可以应用到项目的所有的模块中。如下:

  1. /**
  2. * The buildscript {} block is where you configure the repositories and
  3. * dependencies for Gradle itself--meaning, you should not include dependencies
  4. * for your modules here. For example, this block includes the Android plugin for
  5. * Gradle as a dependency because it provides the additional instructions Gradle
  6. * needs to build Android app modules.
  7. */
  8. buildscript {
  9. /**
  10. * The repositories {} block configures the repositories Gradle uses to
  11. * search or download the dependencies. Gradle pre-configures support for remote
  12. * repositories such as JCenter, Maven Central, and Ivy. You can also use local
  13. * repositories or define your own remote repositories. The code below defines
  14. * JCenter as the repository Gradle should use to look for its dependencies.
  15. */
  16. repositories {
  17. jcenter()
  18. }
  19. /**
  20. * The dependencies {} block configures the dependencies Gradle needs to use
  21. * to build your project. The following line adds Android Plugin for Gradle
  22. * version 2.0.0 as a classpath dependency.
  23. */
  24. dependencies {
  25. classpath 'com.android.tools.build:gradle:2.0.0'
  26. }
  27. }
  28. /**
  29. * The allprojects {} block is where you configure the repositories and
  30. * dependencies used by all modules in your project, such as third-party plugins
  31. * or libraries. Dependencies that are not required by all the modules in the
  32. * project should be configured in module-level build.gradle files. For new
  33. * projects, Android Studio configures JCenter as the default repository, but it
  34. * does not configure any dependencies.
  35. */
  36. allprojects {
  37. repositories {
  38. jcenter()
  39. }
  40. }

模块构建文件

模块的build.gradle文件,位于//目录下,允许你对特定的模块进行构建配置。配置这些构建设置允许你提供自定义的打包选项,比如额外的build type和product flavor,复写mainfies中的设置或者顶层build.gradle文件的配置。代码如下:

  1. /**
  2. * The first line in the build configuration applies the Android plugin for
  3. * Gradle to this build and makes the android {} block available to specify
  4. * Android-specific build options.
  5. */
  6. apply plugin: 'com.android.application'
  7. /**
  8. * The android {} block is where you configure all your Android-specific
  9. * build options.
  10. */
  11. android {
  12. /**
  13. * compileSdkVersion specifies the Android API level Gradle should use to
  14. * compile your app. This means your app can use the API features included in
  15. * this API level and lower.
  16. *
  17. * buildToolsVersion specifies the version of the SDK build tools, command-line
  18. * utilities, and compiler that Gradle should use to build your app. You need to
  19. * download the build tools using the SDK Manager.
  20. */
  21. compileSdkVersion 23
  22. buildToolsVersion "23.0.3"
  23. /**
  24. * The defaultConfig {} block encapsulates default settings and entries for all
  25. * build variants, and can override some attributes in main/AndroidManifest.xml
  26. * dynamically from the build system. You can configure product flavors to override
  27. * these values for different versions of your app.
  28. */
  29. defaultConfig {
  30. /**
  31. * applicationId uniquely identifies the package for publishing.
  32. * However, your source code should still reference the package name
  33. * defined by the package attribute in the main/AndroidManifest.xml file.
  34. */
  35. applicationId 'com.example.myapp'
  36. // Defines the minimum API level required to run the app.
  37. minSdkVersion 14
  38. // Specifies the API level used to test the app.
  39. targetSdkVersion 23
  40. // Defines the version number of your app.
  41. versionCode 1
  42. // Defines a user-friendly version name for your app.
  43. versionName "1.0"
  44. }
  45. /**
  46. * The buildTypes {} block is where you can configure multiple build types.
  47. * By default, the build system defines two build types: debug and release. The
  48. * debug build type is not explicitly shown in the default build configuration,
  49. * but it includes debugging tools and is signed with the debug key. The release
  50. * build type applies Proguard settings and is not signed by default.
  51. */
  52. buildTypes {
  53. /**
  54. * By default, Android Studio configures the release build type to enable code
  55. * shrinking, using minifyEnabled, and specifies the Proguard settings file.
  56. */
  57. release {
  58. minifyEnabled true // Enables code shrinking for the release build type.
  59. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  60. }
  61. }
  62. /**
  63. * The productFlavors {} block is where you can configure multiple product
  64. * flavors. This allows you to create different versions of your app that can
  65. * override defaultConfig {} with their own settings. Product flavors are
  66. * optional, and the build system does not create them by default. This example
  67. * creates a free and paid product flavor. Each product flavor then specifies
  68. * its own application ID, so that they can exist on the Google Play Store, or
  69. * an Android device, simultaneously.
  70. */
  71. productFlavors {
  72. free {
  73. applicationId 'com.example.myapp.free'
  74. }
  75. paid {
  76. applicationId 'com.example.myapp.paid'
  77. }
  78. }
  79. }
  80. /**
  81. * The dependencies {} block in the module-level build configuration file
  82. * only specifies dependencies required to build the module itself.
  83. */
  84. dependencies {
  85. compile project(":lib")
  86. compile 'com.android.support:appcompat-v7:22.0.1'
  87. compile fileTree(dir: 'libs', include: ['*.jar'])
  88. }

Gradle属性文件

Gradle也包括两个属性文件,在项目的根目录。你可以用来声明Gradle构建套件的设置:

Gradle.properties

这里你可以设置全局Gradle设置。

Local.properties

为你的构建系统配置本地环境属性,比如SDK安装位置。因为这个文件的内容是Android Studio自动生成的,针对本地的开发环境,所以你不需要手动更改这个文件或者将其添加到你的版本控制系统中。

使用Gradle同步项目

当你更改了你的项目中的构建配置文件之后,Android Studio需要你同步你的项目,这样你就可以导入你的项目配置并且运行一些检测来确保你的配置不会导致创建构建错误。

同步你的项目文件,在你更改之后,会有个提示框,点击Sync Now。如果发现问题,Android Studio会报错:

Source Sets

Android Studio为每个模块合理的组织代码和资源。一个模块的main目录包含了代码和资源。

本文作者:宋志辉

个人微博:点击进入

4.0、Android Studio配置你的构建的更多相关文章

  1. Android Studio配置Git及Git文件状态说明

    Android Studio配置Git还是比较简单的,麻烦的是可能中间出现各种问题.如果你想了解或感兴趣,请往下看. 首先你得下载Git客户端,网址:http://git-scm.com/downlo ...

  2. Android studio 配置JNI环境

    Android studio配置jni开发环境,主要配置是两个build文件,以及新建一个jni文件,放c代码. 代码如下1: apply plugin: 'com.android.model.app ...

  3. Android零基础入门第13节:Android Studio配置优化,打造开发利器

    原文:Android零基础入门第13节:Android Studio配置优化,打造开发利器 是不是很多同学已经有烦恼出现了?电脑配置已经很高了,但是每次运行Android程序的时候就很卡,而且每次安装 ...

  4. 【Flutter 1-2】在 Windows 10下安装Flutter+Dart+Android Studio 配置Flutter开发环境

    在 Windows 10下安装Flutter+Dart+Android Studio 配置Flutter开发环境 文章首发地址 配置环境变量 由于部分网站被墙的原因,我们需要先配置Flutter国内镜 ...

  5. Win10下Android studio配置

    Win10下Android studio配置 一.安装Android Studio的准备工作 1.下载好JDK,去官网上找一个下载下来 2.安装JDK.并配置环境变量.安装过程:本人将使用的是jdk- ...

  6. Android studio配置Git

    Android studio配置Git 1.下载window 版git并安装:下载地址 2.Android Studio设置git插件:File->Setting->Version Con ...

  7. Android Studio 配置SVN实现代码管理

    Refference From:http://iaiai.iteye.com/blog/2267346 一.Android Studio配置SVN Android Studio关联配置SVN很简单,在 ...

  8. Android studio 配置file encoding 无效,中文乱码解决办法

    通过配置Android studio 配置file encoding 无效,中文乱码,问题出现在java编译的时候jack采用了默认编码(中文windows默认的GBK编码)而乱码,所以不管更改bui ...

  9. Android Studio配置使用git

    一.准备 如果没有安装git,那么先要到到Git官网下载git,然后按照提示一步一步安装即可,这个没有什么难度,不过要记得安装的目录. 二.Android Studio配置git File->S ...

随机推荐

  1. 贼有意思[最长上升公共子序列](SAC大佬测试题)

    题目描述Awson 最近越来越蠢了,一天就只知道 zyys.他定义了一个 zyys 数列:这个数列满足:1.是另外两个数列 A,B 的公共子序列;2.数列单调递增.现在他有一个问题,我们假设知道两个长 ...

  2. 洛谷P1856 [USACO5.5]矩形周长Picture

    题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...

  3. ●BZOJ 2149 拆迁队

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2149 题解: 斜率优化DP,栈维护凸包,LIS,分治(我也不晓得是不是CDQ分治...) 一 ...

  4. bzoj3224Tyvj 1728 普通平衡树 treap

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 17706  Solved: 7764[Submit][St ...

  5. [原创]手把手教你写网络爬虫(7):URL去重

    手把手教你写网络爬虫(7) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 本期我们来聊聊URL去重那些事儿.以前我们曾使用Python的字典来保存抓取过的URL,目的是将重复抓取的UR ...

  6. Vue.js + Webpack

    vue.js Vue.js是一个构建数据驱动的 web 界面的库.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件 以上是Vue.js官方定义,故名思议,以数据驱动视 ...

  7. OLE:对象的类没有在注册数据库中注册

    我在网上下载了破解版的SAS9.3,用了一段时间之后,今天打开就填出一个提示框:OLE:对象的类没有在注册数据库中注册 激活该对象所需的应用程序不可用.是否用"转换--"将其转换为 ...

  8. 剑指架构师系列-MySQL调优

    介绍MySQL的调优手段,主要包括慢日志查询分析与Explain查询分析SQL执行计划 1.MySQL优化 1.慢日志查询分析 首先需要对慢日志进行一些设置,如下: SHOW VARIABLES LI ...

  9. Struts2 转换器

    转换器 从一个 HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串 Http 没有 "类型" 的概念,每一项表单的输入只可能是一个字符串或一个字符串数组,在服务 ...

  10. U盘PE无人值守安装centOS6

    一.制作 1.需要用到的工具:老毛桃PX工具.系统ISO.一个8GU盘 老毛桃PE工具 http://laomaotao.net/ CentOS启动映像 http://mirrors.163.com/ ...