Android 学习笔记

Android Gradle 文件配置

2018-04-24  本文已影响13人  mlya

Build Configuration

Android 使用 Gradle 来管理编译过程. 结合 Android plugin, 可以使用 Gradle 来进行 Android 项目的编译.

编译流程

-w475

上图是一个典型的 Android apk 编译流程:

  1. 将源代码通过编译器编译成 DEX 文件 (Dalvik Executable), 包括运行在 Android 设备上的 bytecode 和其他所有的编译过的资源.
  2. 通过 APK Packager 签名并合并成一个 APK.
  3. 签名的过程可以使用 debug 或者 release 的 keystore.
  4. 在生成最终的 APK 前, 会进行压缩以减小包的大小.

Project, Module and Sourceset

-w330

我们使用 Android Studio 创建 Android 项目时, 会生成如上图的文件结构, 包括 Project, Module 和 Sourceset 三个层次结构.

Gradle Settings File

settings.gradle 这个文件, 位于项目的根目录下, 作用是告诉 Gradle, 在构建项目时, 应该将哪个 module 包含进来, 默认情况下, 这个文件只有简单的一行内容:

include ':app'

The Top-level Build File

在 project 的根目录下的 build.gradle 定义了应用于所有 modules 的设置.

/**
 * 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.
     *
     * New projects created using Android Studio 3.0 and higher also include
     * Google's Maven repository.
     */

    repositories {
        google()
        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.1.0 as a classpath dependency.
     */

    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.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. However, you should configure module-specific dependencies in
 * each module-level build.gradle file. For new projects, Android Studio
 * includes JCenter and Google's Maven repository by default, but it does not
 * configure any dependencies (unless you select a template that requires some).
 */

allprojects {
   repositories {
       google()
       jcenter()
   }
}

  1. buildscript{} 设置 repositories 和 Gradle 自己的 dependencies. 不应该在这里添加术语模块的依赖.
  2. repositories{} 设置 Gradle 所使用的仓库, 可以使用 JCenter, Maven Central, Ivy 或者本地的仓库或者自己的远程仓库. 在 Android Studio 3.0 之后的版本包含了 Google 的 Maven 仓库.
  3. dependencies{} 设置 Gradle 自己所使用的依赖, 上面的例子中添加的是 Android plugin 作为 classpath dependency.
  4. allprojects{} 设置了所有 modules 使用的 repository 和 dependency. 上面的例子设置了仓库.

设置面向整个项目的属性值

我们可以添加 ext 块来设定一些面向整个项目的属性值

buildscript {...}

allprojects {...}

// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 26
    // You can also create properties to specify versions for dependencies.
    // Having consistent versions between modules can avoid conflicts with behavior.
    supportLibVersion = "27.1.1"
    ...
}

之后, 我们可以在每个 module 的 build.gradle 文件中使用这些属性值:

android {
  // Use the following syntax to access properties you defined at the project level:
  // rootProject.ext.property_name
  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}
...
dependencies {
    compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    ...
}

The Module-level Build File

每个 module 的 build.gradle 位于每个 module 的目录下, 例如, app 的位于 main/app 目录下.

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

  compileSdkVersion 26

  /**
   * 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.
   *
   * If you're using Android plugin 3.0.0 or higher, this property is optional—
   * the plugin uses a recommended version of the build tools by default.
   */

  buildToolsVersion "27.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 26

    // 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 the defaultConfig block 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.
   *
   * If you're using Android plugin 3.0.0 or higher, you need to also declare
   * and assign each flavor to a flavor dimension. To learn more, read the
   * migration guide.
   */

  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 {
    // Settings to build multiple APKs based on screen density.
    density {

      // Enable or disable building multiple APKs.
      enable false

      // Exclude these densities when building multiple APKs.
      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.
 *
 * If you're using Android plugin 3.0.0 or higher, you should
 * use the new dependency configurations, which help you improve build speeds by
 * restricting which dependencies leak their APIs to other modules.
 */

dependencies {
    compile project(":lib")
    compile 'com.android.support:appcompat-v7:27.1.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

  1. apply plugin: 'com.android.application': 第一行, 说明使用 Android plugin, 使得一些针对 android 的 block 生效.
  2. android{}: 设置针对 android 的配置.
  3. compileSdkVersion: 设置编译 app 时的 API level.
  4. buildToolsVersion: 指定 SDK build tools, 在 Android Plugin 3.0.0 以后, 这个属性可以省略的.
  5. defaultConfig{}: 设置默认的构建变种 (build variants).
  6. applicationId: applicationId 是发行包的唯一标识, 一经设定就不能改变, 并且应该和 main/AndroidManifest.xml 文件中的 package 属性一致.
  7. minSdkVersion: 设定运行 app 的最小 API level.
  8. targetSdkVersion: 指定 test app 的 API level.
  9. versionCode: 指定版本.
  10. versionName: user-friendly 的版本.
  11. buildTypes{}: 可以指定多个编译类型, 比如 release 和 debug.
  12. dependencies{}: 用于引入依赖.

Application ID

Application ID 在 module 的 build.gradle 文件中定义.

android {
    defaultConfig {
        applicationId "com.example.myapp"
        ...
    }
}

我们可以看到, application id 的形式像 Java 的包名, 默认情况下, Android Studio 自动生成的 application id 和 package name 是一样的, 但是这两者其实是相互独立的.

application id 可以唯一标识应用, 在发布应用更新时, 不可以更改 application id, 不然会认为这是两个 app.

在使用 WebView 时, 需要使用 package name 作为 application ID 的前缀, 不然可能会遇上一些问题.

一些 Android API 中的方法和参数名称使用的 package name 这样的意思, 其实是指的是 application ID, 例如: Context.getPackageName() 方法返回的是 application ID.

添加依赖

上一篇下一篇

猜你喜欢

热点阅读