使用androidstudio构建项目不得不需要对gradle了解,gradle是基于jvm,基于DSL语法的自动化构建工具。是google引入,替换ant和maven的新工具,其依赖兼容maven和ivy。gradle相比ant,扩展性更好,灵活性更高。
gradle模型
gradle模型中两个重要的概念:project和module,以往eclipse的ant模型,workspace代表的项目工程空间,project代表的项目。而在gradle中project代表的是一整个工程,module代表是一个应用或一个库。
gradle结构
project
|
|—— —— module(app)
| |
| |
| build.gradle
build.gradle
|
settings.gradle
module下的build.gradle
app或库的相关配置在此配置project下的build.gradle
1、工程需要的gradle工具的引用版本、及下载某些引用库的仓库。2、工程所有module的相关通用配置
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
- project下的settings.gradle
项目所有的module需要在此声明
include ':app'
gradle配置
一般使用中指针对app的gradle进行修改配置,所以这里对gradle的相关配置做配置说明。如果这些配置记不住,没关系,androidstudio提供了非常好用的图形界面。
选中module——>菜单File——>project struture
可以看到项目配置、sdk配置及module的各项配置。
apply plugin: 'com.android.application'
//申明这个工程的属性是安卓运行app
//而不是'com.android.library' 库工程。
def buildTime = new Date().format("yyMMddHHmmss")
**编译需要的引用配置
dependencies {
//jar引用,fileTree(dir:'libs',include:'*.jar')所有文件目录树下的libs的jar的引用,
// 剔除*v4的jar引用exclude:'*v4.jar'
compile fileTree(dir: 'libs', include: '*.jar', exclude: '*v4.jar')
//工程引用
compile project(':SmartDevice:WiFiControlLibrary')
compile project(':SmartDevice:social_sdk_library_project')
}
**工程的相关配置
android {
//编译的sdk版本
compileSdkVersion 23
//编译工具的版本
buildToolsVersion "23.0.2"
//解决sdk中org.apache.http.*相关包被剔除的问题
useLibrary 'org.apache.http.legacy'
//关闭Android Studio的PNG合法性检查的,有些.9的图片可能会出现合法性的问题。用这个可以解决
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
*java编译相关配置
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
*签名,签名可以防止app被恶意串改,只有签名相同的程序才能升级替换安装
signingConfigs {
//正式包需要的keystore相关信息
myConfig {
//keystore路径
storeFile file("build-files/xxxxx.keystore")
//keystore密码
storePassword "222222"
//keystore别名
keyAlias "xxxxxxx"
//别名的密码
keyPassword "3333333"
}
//调试包keystore的配置
debugConfig {
storeFile file("build-files/debug.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
*渠道配置,defaultConfig原生配置,可以多渠道 类似flavor_wandoujia {}
defaultConfig {
applicationId "com.xxxxx.xxxxx.xxxxxx"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
//支持multidex 解决65536爆包问题
//multiDexEnabled true
}
*build编译app的时候的配置,一般这里混淆的使用
buildTypes {
debug {
signingConfig signingConfigs.debugConfig
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
}
release {
signingConfig signingConfigs.myConfig
zipAlignEnabled true
//是否混淆
minifyEnabled true
// 移除无用的resource文件,开启这个会报错,待解决
// shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
*编译的 lint 开关
lintOptions {
// set to true to turn off analysis progress reporting by lint
quiet true
// if true, stop the gradle build if errors are found
abortOnError false
// if true, only report errors
ignoreWarnings true
checkAllWarnings false
checkReleaseBuilds false
// lintConfig file("lint.xml")
}
*为所有的子项目设置一些通用配置
subprojects {
//配置一个新的gradle一样
}
*Eclipse 中迁移过来的代码都带这个设置,一般做指定目录
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
//so库的引用
jniLibs.srcDirs = ['libs']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
后续在详细介绍多渠道打包和gradle task任务、以及gradle使用groovy语言。