简单介绍
在android studio中新建一个工程,打开app文件夹下这个Module的build.gradle配置文件,也可以算是整个项目最主要的gradle配置文件,默认配置如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.ds.test"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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:26.0.0-alpha1'
testCompile 'junit:junit:4.12'
}
接下来,认识下他们:
/*
apply plugin表明当前module是一个app
如果是library则写成 apply plugin: 'com.android.library';
* */
apply plugin: 'com.android.application'
//android {...} 配置了android相关的构建选项:
android {
compileSdkVersion 26//编译的sdk版本
buildToolsVersion "26.0.2"//构建工具版本
defaultConfig {//defaultConfig 配置了AndroidManifest中的一些参数,在构建时会覆盖AndroidManifest
applicationId "com.ds.test"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {//buildTypes 配置了如何构建app,默认有debug和release两种
release {
minifyEnabled false//是否进行混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//混淆文件的位置
}
}
}
/*
dependencies 表明了当前module依赖关系,一个module可以用三种方式依赖。
* */
dependencies {
//compile project(":lib")//方式1.依赖统一工程下的另一个module
compile fileTree(dir: 'libs', include: ['*.jar'])//方式2.依赖libs目录下的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:26.0.0-alpha1'//方式3.依赖远程库
testCompile 'junit:junit:4.12'
}
学习一些技巧
1.自定义apk包名包含信息是否为debug或是release版本、打包时间、版本号等。
2.将build.gradle中数字相关的定义在gradle.properties文件中。
build.gradle文件:
/*
apply plugin表明当前module是一个app
如果是library则写成 apply plugin: 'com.android.library';
* */
apply plugin: 'com.android.application'
//android {...} 配置了android相关的构建选项:
android {
compileSdkVersion COMPILE_SDK_VERSION as int//编译的sdk版本
buildToolsVersion BUILD_TOOLS_VERSION//构建工具版本
defaultConfig {//defaultConfig 配置了AndroidManifest中的一些参数,在构建时会覆盖AndroidManifest
applicationId APPLICATION_ID
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
versionCode VERSION_CODE as int
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
applicationVariants.all { variant ->
variant.outputs.each { output ->
def fileName = "${variant.flavorName}_release_v${variant.versionName}.apk"
if (variant.buildType.isDebuggable()) {
fileName = "${variant.flavorName}_debug_v${variant.versionName}.apk"
}
output.outputFile = new File(output.outputFile.parent, fileName)
}
}
}
buildTypes {//buildTypes 配置了如何构建app,默认有debug和release两种
release {
minifyEnabled false// 是否进行混淆
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'// 混淆文件的位置
}
}
//构建不同的编译版本
productFlavors {
demo {
applicationId APPLICATION_ID1
versionName VERSION_NAME+"_"+buildTime()
}
full {
applicationId APPLICATION_ID2
versionName VERSION_NAME+"_"+buildTime()
}
}
}
def buildTime() {
return new Date().format('yyyyMMdd')
}
/*
dependencies 表明了当前module依赖关系,一个module可以用三种方式依赖。
* */
dependencies {
//compile project(":lib")//方式1.依赖统一工程下的另一个module
compile fileTree(dir: 'libs', include: ['*.jar'])//方式2.依赖libs目录下的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:26.0.0-alpha1'//方式3.依赖远程库
testCompile 'junit:junit:4.12'
}
gradle.properties文件
APPLICATION_ID = com.ds.test
APPLICATION_ID1 = com.ds.test1
APPLICATION_ID2 = com.ds.test2
COMPILE_SDK_VERSION = 26
BUILD_TOOLS_VERSION = 26.0.2
MIN_SDK_VERSION = 15
TARGET_SDK_VERSION = 26
VERSION_CODE = 1
VERSION_NAME = 1.0
经过上面的配置,我们可生成四种名字的apk:
demo_debug_v1.0_20171120.apk
demo_release_v1.0_20171120.apk
full_debug_v1.0_20171120.apk
full_release_v1.0_20171120.apk