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目录包含了代码和资源。

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

目录
相关文章
|
2天前
|
JavaScript Java Maven
云效产品使用常见问题之android sdk 构建出aar后,上传到私有maven仓库失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
20天前
|
移动开发 Java Android开发
构建高效Android应用:探究Kotlin与Java的性能差异
【4月更文挑战第3天】在移动开发领域,性能优化一直是开发者关注的焦点。随着Kotlin的兴起,其在Android开发中的地位逐渐上升,但关于其与Java在性能方面的对比,尚无明确共识。本文通过深入分析并结合实际测试数据,探讨了Kotlin与Java在Android平台上的性能表现,揭示了在不同场景下两者的差异及其对应用性能的潜在影响,为开发者在选择编程语言时提供参考依据。
|
21天前
|
数据库 Android开发 开发者
构建高效Android应用:Kotlin协程的实践指南
【4月更文挑战第2天】随着移动应用开发的不断进步,开发者们寻求更流畅、高效的用户体验。在Android平台上,Kotlin语言凭借其简洁性和功能性赢得了开发社区的广泛支持。特别是Kotlin协程,作为一种轻量级的并发处理方案,使得异步编程变得更加简单和直观。本文将深入探讨Kotlin协程的核心概念、使用场景以及如何将其应用于Android开发中,以提高应用性能和响应能力。通过实际案例分析,我们将展示协程如何简化复杂任务,优化资源管理,并为最终用户提供更加流畅的体验。
|
24天前
|
缓存 监控 Java
构建高效Android应用:从优化用户体验到提升性能
在竞争激烈的移动应用市场中,为用户提供流畅和高效的体验是至关重要的。本文深入探讨了如何通过多种技术手段来优化Android应用的性能,包括UI响应性、内存管理和多线程处理。同时,我们还将讨论如何利用最新的Android框架和工具来诊断和解决性能瓶颈。通过实例分析和最佳实践,读者将能够理解并实施必要的优化策略,以确保他们的应用在保持响应迅速的同时,还能够有效地利用系统资源。
|
30天前
|
编解码 算法 Java
构建高效的Android应用:内存优化策略详解
随着智能手机在日常生活和工作中的普及,用户对移动应用的性能要求越来越高。特别是对于Android开发者来说,理解并实践内存优化是提升应用程序性能的关键步骤。本文将深入探讨针对Android平台的内存管理机制,并提供一系列实用的内存优化技巧,以帮助开发者减少内存消耗,避免常见的内存泄漏问题,并确保应用的流畅运行。
|
22天前
|
Java Android开发 开发者
构建高效Android应用:Kotlin协程的实践与优化
在响应式编程范式日益盛行的今天,Kotlin协程作为一种轻量级的线程管理解决方案,为Android开发带来了性能和效率的双重提升。本文旨在探讨Kotlin协程的核心概念、实践方法及其在Android应用中的优化策略,帮助开发者构建更加流畅和高效的应用程序。通过深入分析协程的原理与应用场景,结合实际案例,本文将指导读者如何优雅地解决异步任务处理,避免阻塞UI线程,从而优化用户体验。
|
27天前
|
Java 编译器 Android开发
构建高效Android应用:探究Kotlin与Java的性能差异
在开发高性能的Android应用时,选择合适的编程语言至关重要。近年来,Kotlin因其简洁性和功能性受到开发者的青睐,但其性能是否与传统的Java相比有所不足?本文通过对比分析Kotlin与Java在Android平台上的运行效率,揭示二者在编译速度、运行时性能及资源消耗方面的具体差异,并探讨在实际项目中如何做出最佳选择。
17 4
|
23小时前
|
Android开发
Android构建系统:Android.mk(2)函数详解
Android构建系统:Android.mk(2)函数详解
5 1
|
1天前
|
Android开发
Android 动态修改参数配置
Android 动态修改参数配置
9 0
|
6天前
|
缓存 移动开发 Android开发
构建高效Android应用:从优化用户体验到提升性能表现
【4月更文挑战第18天】 在移动开发的世界中,打造一个既快速又流畅的Android应用并非易事。本文深入探讨了如何通过一系列创新的技术策略来提升应用性能和用户体验。我们将从用户界面(UI)设计的简约性原则出发,探索响应式布局和Material Design的实践,再深入剖析后台任务处理、内存管理和电池寿命优化的技巧。此外,文中还将讨论最新的Android Jetpack组件如何帮助开发者更高效地构建高质量的应用。此内容不仅适合经验丰富的开发者深化理解,也适合初学者构建起对Android高效开发的基础认识。
3 0