4.0、Android Studio配置你的构建

简介: Android构建系统编译你的app资源和源码并且打包到APK中,你可以用来测试,部署,签名和发布。

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在构建你的应用的时候需要包括哪些模块,大部分项目如下:

include:app

顶层的构建文件

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

/**
 * 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 {

    /**
     * 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 2.0.0 as a classpath dependency.
     */

    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
    }
}

/**
 * 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()
   }
}

模块构建文件

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

/**
 * 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 23
  buildToolsVersion "23.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 14

    // Specifies the API level used to test the app.
    targetSdkVersion 23

    // 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 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:22.0.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Gradle属性文件

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

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

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

使用Gradle同步项目

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

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

这里写图片描述

Source Sets

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

本文作者:宋志辉
个人微博:点击进入

目录
相关文章
|
23天前
|
移动开发 监控 前端开发
构建高效Android应用:从优化布局到提升性能
【7月更文挑战第60天】在移动开发领域,一个流畅且响应迅速的应用程序是用户留存的关键。针对Android平台,开发者面临的挑战包括多样化的设备兼容性和性能优化。本文将深入探讨如何通过改进布局设计、内存管理和多线程处理来构建高效的Android应用。我们将剖析布局优化的细节,并讨论最新的Android性能提升策略,以帮助开发者创建更快速、更流畅的用户体验。
46 10
|
23天前
|
Java Android开发 C++
Android Studio JNI 使用模板:c/cpp源文件的集成编译,快速上手
本文提供了一个Android Studio中JNI使用的模板,包括创建C/C++源文件、编辑CMakeLists.txt、编写JNI接口代码、配置build.gradle以及编译生成.so库的详细步骤,以帮助开发者快速上手Android平台的JNI开发和编译过程。
68 1
|
10天前
|
XML IDE 开发工具
🔧Android Studio高级技巧大公开!效率翻倍,编码不再枯燥无味!🛠️
【9月更文挑战第11天】在软件开发领域,Android Studio凭借其强大的功能成为Android开发者的首选IDE。本文将揭示一些提升开发效率的高级技巧,包括自定义代码模板、重构工具、高级调试技巧及多模块架构。通过对比传统方法,这些技巧不仅能简化编码流程,还能显著提高生产力。例如,自定义模板可一键插入常用代码块;重构工具能智能分析并安全执行代码更改;高级调试技巧如条件断点有助于快速定位问题;多模块架构则提升了大型项目的可维护性和团队协作效率。掌握这些技巧,将使你的开发之旅更加高效与愉悦。
24 5
|
10天前
|
开发框架 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的指南
在移动应用开发的广阔天地中,安卓与iOS两大平台各占半壁江山。本文将深入浅出地对比这两大操作系统的开发环境、工具和用户体验设计,揭示它们在编程语言、开发工具以及市场定位上的根本差异。我们将从开发者的视角出发,逐步剖析如何根据项目需求和目标受众选择适合的平台,同时探讨跨平台开发框架的利与弊,为那些立志于打造下一个热门应用的开发者提供一份实用的指南。
26 5
|
23天前
|
Android开发
Android 配置蓝牙遥控器键值
本文详细介绍了Android系统中配置蓝牙遥控器键值的步骤,包括查看设备号、配置键位映射文件(kl文件)、部署kl文件以及调试过程,确保蓝牙遥控器的按键能正确映射到Android系统对应的按键功能。
34 1
|
23天前
|
编解码 Android开发
【Android Studio】使用UI工具绘制,ConstraintLayout 限制性布局,快速上手
本文介绍了Android Studio中使用ConstraintLayout布局的方法,通过创建布局文件、设置控件约束等步骤,快速上手UI设计,并提供了一个TV Launcher界面布局的绘制示例。
31 1
|
12天前
|
开发工具 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的关键考量
在数字时代的浪潮中,安卓和iOS这两大操作系统如同双子星座般耀眼夺目,引领着移动应用的潮流。它们各自拥有独特的魅力和深厚的用户基础,为开发者提供了广阔的舞台。然而,正如每枚硬币都有两面,安卓与iOS在开发过程中也展现出了截然不同的特性。本文将深入剖析这两者在开发环境、编程语言、用户体验设计等方面的显著差异,并探讨如何根据目标受众和项目需求做出明智的选择。无论你是初涉移动应用开发的新手,还是寻求拓展技能边界的资深开发者,这篇文章都将为你提供宝贵的见解和实用的建议,帮助你在安卓与iOS的开发之路上更加从容自信地前行。
|
20天前
|
Android开发 iOS开发 C#
Xamarin:用C#打造跨平台移动应用的终极利器——从零开始构建你的第一个iOS与Android通用App,体验前所未有的高效与便捷开发之旅
【8月更文挑战第31天】Xamarin 是一个强大的框架,允许开发者使用单一的 C# 代码库构建高性能的原生移动应用,支持 iOS、Android 和 Windows 平台。作为微软的一部分,Xamarin 充分利用了 .NET 框架的强大功能,提供了丰富的 API 和工具集,简化了跨平台移动应用开发。本文通过一个简单的示例应用介绍了如何使用 Xamarin.Forms 快速创建跨平台应用,包括设置开发环境、定义用户界面和实现按钮点击事件处理逻辑。这个示例展示了 Xamarin.Forms 的基本功能,帮助开发者提高开发效率并实现一致的用户体验。
46 0
|
20天前
|
Android开发 iOS开发 C#
Xamarin.Forms:从零开始的快速入门指南——打造你的首个跨平台移动应用,轻松学会用C#和XAML构建iOS与Android通用界面的每一个步骤
【8月更文挑战第31天】Xamarin.Forms 是一个强大的框架,让开发者通过单一共享代码库构建跨平台移动应用,支持 iOS、Android 和 Windows。使用 C# 和 XAML,它简化了多平台开发流程并保持一致的用户体验。本指南通过创建一个简单的 “HelloXamarin” 应用演示了 Xamarin.Forms 的基本功能和工作原理。
29 0
|
20天前
|
存储 IDE Java
探索安卓应用的构建之旅:从新手到专家
【8月更文挑战第31天】 本文是一篇面向初学者和有一定基础的开发者的技术性文章。我们将一起踏上一段激动人心的旅程,深入了解如何从零开始构建一个安卓应用。文章将引导你理解安卓开发的基础知识,掌握核心概念,并通过实际代码示例加深你的理解和技能。无论你是刚刚接触安卓开发,还是希望提升现有技能,这篇文章都将为你提供宝贵的信息和实用的技巧。准备好了吗?让我们一起开始吧!