Android打包——详解build.gradle

来源:互联网 发布:java main 编辑:程序博客网 时间:2024/04/29 14:23

一个 Android Studio 项目中,会存在多个 .gradle 文件。其中, project 目录下存在一个 build.gradle 文件和每一个 module 会存在一个 build.gradle 文件。

一、工程中的build.gradle文件:

buildscript {
repositories {
jcenter() //声明使用maven仓库
}
dependencies {
//依赖android提供的2.2.2的gradle build
classpath 'com.android.tools.build:gradle:2.2.2'
}
}

//为所有工程的repositories配置为jcenters
allprojects {
repositories {
jcenter()
}
}
//清除工程任务
task clean(type: Delete) {
delete rootProject.buildDir
}

二、模块中的build.gradle文件:

// 这表示该module是一个app module
apply plugin: 'com.android.application'
android {
compileSdkVersion 23         // 基于哪个版本的sdk编译
buildToolsVersion "23.0.3"// 基于哪个构建工具版本进行构建的


// 缺省配置主要包括:应用ID,最小SDK版本,目标SDK版本,应用版本号、应用版本名
​​​​​​​defaultConfig {
   applicationId "open_open.com.helloworld"
   minSdkVersion 10
   targetSdkVersion 23
   versionCode 1
   versionName "1.0"
}
sourceSets { }
signingConfigs { }


// buildTypes是构建类型,常用的有release和debug两种,可以在这里面启用混淆,启用zipAlign以及配置签名信息等。
buildTypes {
   release {
       minifyEnabled false
       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
   }
}
productFlavors { }
compileOptions { }
packagingOption { }
jacoco { }
splits { }
}


// dependencies它定义了该module需要依赖的jar,aar,jcenter库信息。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
}


apply plugin用来指定用的是哪个插件,取值有:
com.android.application:Android APP插件(打包得到的是.apk文件)
com.android.library:Android库插件(打包得到的是.aar文件)


android用来指定Android打包插件的相关属性,其包含如下节点
compileSdkVersion(apiLevel):设置编译时用的Android版本
buildToolsVersion(buildToolsVersionName):设置编译时使用的构建工具的版本
defaultConfig:设置一些默认属性,其可用属性是buildTypes和ProductFlavors之和
sourceSets:配置相关源文件的位置,当你的项目的目录结构跟默认的有区别但又不想改的时候sourceSets就派上用场了
-- aidl 设置aidi的目录
-- assets 设置assets资源目录
-- compileConfigurationName The name of the compile configuration for this source set.
-- java Java源代码目录
-- jni JNI代码目录
-- jniLibs 已编译好的JNI库目录
-- manifest 指定清单文件
-- name The name of this source set.
-- packageConfigurationName The name of the runtime configuration for this source set.
-- providedConfigurationName The name of the compiled-only configuration for this source set.
-- renderscript Renderscript源代码目录
-- res 资源目录
-- setRoot(path) 根目录
signingConfigs:配置签名信息
-- keyAlias 签名的别名
-- keyPassword 密码
-- storeFile 签名文件的路径
-- storePassword 签名密码
-- storeType 类型
buildTypes:配置构建类型,可打出不同类型的包,默认有debug和release两种,你还可以在增加N种
-- applicationIdSuffix 修改applicationId,在默认applicationId的基础上加后缀。在buildType中修改applicationId时只能加后缀,不能完全修改
-- debuggable 设置是否生成debug版的APK
-- jniDebuggable 设置生成的APK是否支持调试本地代码
-- minifyEnabled 设置是否执行混淆
-- multiDexEnabled Whether Multi-Dex is enabled for this variant.
-- renderscriptDebuggable 设置生成的APK是否支持调试RenderScript代码
-- renderscriptOptimLevel 设置RenderScript优化级别
-- signingConfig 设置签名信息
-- versionNameSuffix 修改版本名称,在默认版本名称的基础上加后缀。在buildType中修改版本名称时只能加后缀,不能完全修改
-- zipAlignEnabled 设置是否对APK包执行ZIP对齐优化
-- proguardFile(proguardFile) 添加一个混淆文件
-- proguardFiles(proguardFileArray) 添加多个混淆文件
-- setProguardFiles(proguardFileIterable) 设置多个混淆文件
productFlavors:配置不同风格的APP,在buildTypes的基础上还可以让每一个类型的APP拥有不同的风格,所以最终打出的APK的数量就是buildTypes乘以productFlavors
-- applicationId 设置应用ID
-- multiDexEnabled Whether Multi-Dex is enabled for this variant.signingConfig Signing config used by this product flavor.
-- testApplicationId 设置测试时的应用ID
-- testFunctionalTest See instrumentation.
-- testHandleProfiling See instrumentation.
-- testInstrumentationRunner Test instrumentation runner class name.
-- versionCode 设置版本号
-- versionName 设置版本名称
-- minSdkVersion(int minSdkVersion) 设置兼容的最小SDK版本
-- minSdkVersion(String minSdkVersion) 设置兼容的最小版本
-- proguardFile(proguardFile) 添加一个混淆文件
-- proguardFiles(proguardFileArray) 添加多个混淆文件
-- setProguardFiles(proguardFileIterable) 设置多个混淆文件
-- targetSdkVersion(int targetSdkVersion) 设置目标SDK版本
-- targetSdkVersion(String targetSdkVersion) 设置目标SDK版本
compileOptions:设置编译的相关属性
-- sourceCompatibility Language level of the source code.
-- targetCompatibility Version of the generated Java bytecode.
packagingOptions:设置APK包的相关属性
-- excludes The list of excluded paths.
-- pickFirsts The list of paths where the first occurrence is packaged in the APK.
-- exclude(path) Adds an excluded paths.
-- pickFirst(path) Adds a firstPick path. First pick paths do get packaged in the APK, but only the first occurrence gets packaged.
-- jacoco:设置JaCoCo的相关属性
-- version 设置JaCoCo的版本
splits:设置如何拆分APK(比如你想拆分成arm版和x86版)
-- abi ABI settings.
-- abiFilters The list of ABI filters used for multi-apk.
-- density Density settings.
-- densityFilters The list of Density filters used for multi-apk.

0 0