Gradle 详细解释

1,245 阅读4分钟
原文链接: www.haotianyi.win

简单说明

Android Studio 使用 Gradle这一高级构建工具包来自动化执行和管理构建流程,同时也允许您定义灵活的自定义构建配置。每个构建配置均可自行定义一组代码和资源,同时对所有应用版本共有的部分加以重复利用。

APK构建流程

snipaste_20170406_100503

  1. 编译器将您的源代码转换成 DEX(Dalvik Executable) 文件(其中包括运行在 Android 设备上的字节码),将所有其他内容转换成已编译资源。
  2. APK 打包器将 DEX 文件和已编译资源合并成单个 APK。不过,必须先签署 APK,才能将应用安装并部署到 Android 设备上。
  3. APK 打包器使用调试或发布密钥库签署您的 APK:
    1. 如果您构建的是调试版本的应用(即专用于测试和分析的应用),打包器会使用调试密钥库签署您的应用。Android Studio 自动使用调试密钥库配置新项目。
    2. 如果您构建的是打算向外发布的发布版本应用,打包器会使用发布密钥库签署您的应用。
  4. 在生成最终 APK 之前,打包器会使用 zipalign 工具对应用进行优化,减少其在设备上运行时的内存占用。

项目结构

Android Plugin for Gradle 引入了您需要的大多数 DSL 元素(也就是在xxx.gradle中的元素),请阅读 DSL 参考文档

snipaste_20170406_101029

对应上面的项目结构有下面的gradle文件相对应:

snipaste_20170406_101456

  • Gradle设置文件

settings.gradle 文件位于项目根目录,用于指示 Gradle 在构建应用时应将哪些模块包括在内。对大多数项目而言,该文件很简单,只包括以下内容:

include ‘:app’
  • 顶级构建文件

顶级 build.gradle 文件位于项目根目录,用于定义适用于项目中所有模块的构建配置。默认情况下,这个顶级构建文件使用 buildscript {} 代码块来定义项目中所有模块共用的 Gradle 存储区依赖项

buildscript {
  //远程仓库的目录
    repositories {
        jcenter()
    }
  //Android Plugin for Gradle版本
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
//应用于所有的moudle
allprojects {
    repositories {
        jcenter()
    }
}
  • 模块级构建文件
//使得android可以使用
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "haotinayi.win.myapplication"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
      // 启用多dex,如果app中的代码方法数超过65535 
        multiDexEnabled true 
        // android单元测试配置 
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {             
           storeFile file("debug.keystore") // 签名文件相对路径 
           storePassword "android" // 签名的密码 
           keyAlias "androiddebugkey" // 别名 
           keyPassword "android" // 别名密码 
        release {
            // 在混淆时去除代码中无用的内容 
            minifyEnabled true 
            // 在混淆时去除无用的资源,针对res/目录中的内容,不用压缩图片的大小 
            shrinkResources true 
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // 配置混淆文件 
        }
    }


  //产品风味(设置不同的版本)
  productFlavors {
    free {
      applicationId 'com.example.myapp.free'
    }

    paid {
      applicationId 'com.example.myapp.paid'
    }
  }
//从一个项目代码中产生不同版本的apk
  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"
    }
  }

}



dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'
}

Gradle属性文件

  • gradle.properties

    您可以在其中配置项目范围 Gradle 设置,例如 Gradle 后台进程的最大堆大小。

  • local.properties

    为构建系统配置本地环境属性,例如 SDK 安装路径。由于该文件的内容由 Android Studio 自动生成并且专用于本地开发者环境,因此您不应手动修改该文件,或将其纳入您的版本控制系统。

Application ID

应用是按照Application ID区分的,Application ID是应用的唯一标志,只不过默情况下和包名相同,默认在defaultConfig中来设置:

android {
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    ...
}

可以改变Application ID来设置不同的APP:

android {
    defaultConfig {
        applicationId "com.example.myapp"
    }
    productFlavors {
        free {
            applicationIdSuffix ".free"
        }
        pro {
            applicationIdSuffix ".pro"
        }
    }
}

那么就变成com.example.myapp.freecom.example.myapp.pro

和mainfest对比

mainfest文件中搞的package属性:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

它有两个作用:

  1. 作为命名空间,如设置R文件,com.example.myapp.R
  2. 设置相对名称,如<activity android:name=".MainActivity">指定的就是 com.example.myapp.MainActivity文件

所以不能随便的更改package属性,但是AppicationID可以改变(也就改成了另外一个APP)

参考

整理自:developer.android.google.cn/studio/buil…

www.open-open.com/lib/view/op…