推荐阅读
Android Plugin DSL Reference v3.4.0 官方文档
Android gradle配置详解
module的build.gradle :
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
//默认配置项
defaultConfig {
applicationId "com.example.jingbin.cloudreader"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
multiDexEnabled true
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
//阿里路由框架配置(每个module都需要)
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
//签名的配置
signingConfigs {
debug {
storeFile file('../buildsystem/debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release {
storeFile file(pStoreFile)
storePassword pStorePassword
keyAlias pKeyAlias
keyPassword pKeyPassword
}
}
// 编译配置,release或debug版本的内容
buildTypes {
release {
minifyEnabled false
shrinkResources false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
incremental false
preDexLibraries = false
jumboMode = false
javaMaxHeapSize "2048M"
}
// 关闭PNG合法性检查的
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
// Java 的版本配置
//配置使用JDK1.8(8),比如需要使用Lambda特性时,那么就可以在这里进行JDK版本的配置
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//或
//compileOptions {
// sourceCompatibility = '1.8'
// targetCompatibility = '1.8'
//}
// 关闭增量编译
compileOptions.incremental = false
// work-runtime-ktx 2.1.0 and above now requires Java 8
kotlinOptions {
jvmTarget = "1.8"
}
//源码设置(项目目录结构的设置)
//配置项目的目录结构,其中较为常见的一个应用场景是,
//将Eclipse 中的项目迁移至AS,由于两者目录结构相差较大,所以需要手动指定
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}
//packagingOptions –打包时的相关配置
//使用packagingOptions排除不想添加到apk中的文件
//当项目中依赖的第三方库越来越多时,有可能会出现两个依赖库中存在同一个(名称)文件。
//如果这样,Gradle在打包时就会提示错误(警告)。
//那么就可以根据提示,然后使用以下方法将重复的文件剔除。
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/rxjava.properties'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
//这个是在同时使用butterknife、dagger2做的一个处理。
//同理,遇到类似的问题,只要根据gradle的提示,做类似处理即可。
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
lintOptions {
// true--关闭lint报告的分析进度
quiet true
// true--错误发生后停止gradle构建
//编译的 lint 开关,程序在buid的时候,会执行lint检查,
//有任何的错误或者警告提示,都会终止构建,我们可以将其关掉。
abortOnError false
// true--只报告error
ignoreWarnings true
// true--忽略有错误的文件的全/绝对路径(默认是true)
//absolutePaths true
// true--检查所有问题点,包含其他默认关闭项
checkAllWarnings true
// true--所有warning当做error
warningsAsErrors true
// 关闭指定问题检查
disable 'TypographyFractions','TypographyQuotes'
disable 'InvalidPackage'
//Some libraries have issues with this.
disable 'OldTargetApi'
//Lint gives this warning but SDK 20 would be Android L Beta.
disable 'IconDensities'
//For testing purpose. This is safe to remove.
disable 'IconMissingDensityFolder'
//For testing purpose. This is safe to remove.
// 打开指定问题检查
enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
// 仅检查指定问题
check 'NewApi', 'InlinedApi'
// true--error输出文件不包含源码行号
noLines true
// true--显示错误的所有发生位置,不截取
showAll true
// 回退lint设置(默认规则)
lintConfig file("default-lint.xml")
// true--生成txt格式报告(默认false)
textReport true
// 重定向输出;可以是文件或'stdout'
textOutput 'stdout'
// true--生成XML格式报告
xmlReport false
// 指定xml报告文档(默认lint-results.xml)
xmlOutput file("lint-report.xml")
// true--生成HTML报告(带问题解释,源码位置,等)
htmlReport true
// html报告可选路径(构建器默认是lint-results.html )
htmlOutput file("lint-report.html")
// true--所有正式版构建执行规则生成崩溃的lint检查,
//如果有崩溃问题将停止构建
checkReleaseBuilds true
// 在发布版本编译时检查(即使不包含lint目标),指定问题的规则生成崩溃
fatal 'NewApi', 'InlineApi'
// 指定问题的规则生成错误
error 'Wakelock', 'TextViewEdits'
// 指定问题的规则生成警告
warning 'ResourceAsColor'
// 忽略指定问题的规则(同关闭检查)
ignore 'TypographyQuotes'
}
//产品发布的一些东西,比如渠道、包名等
//通常在适配多个渠道的时候,需要为特定的渠道做部分特殊的处理,
//比如设置不同的包名、应用名等。
//场景:当我们使用友盟统计时,通常需要设置一个渠道ID,
//那么我们就可以利用productFlavors来生成对应渠道信息的包
flavorDimensions "default"
productFlavors {
wandoujia {
//豌豆荚渠道包配置
dimension "default"
applicationIdSuffix = ".wandoujia"
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
xiaomi {
dimension "default"
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
applicationId "com.wiky.gradle.xiaomi" //配置包名
}
_360 {
dimension "default"
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
}
//...
}
//统一资源前缀,规范资源引用
//不同模块对于资源的命名可能会有冲突,
//为了防止不同模块的资源应为命名冲突而被错误的覆盖,
//就需要一种机制能够检查、提示、修改冲突的资源。
//例如有个模块contact,使用前缀 contact_
//resourcePrefix "contact_"
resourcePrefix "${project.name}_"
//给 Module 内的资源名增加前缀, 避免资源名冲突
dataBinding {
enabled = true
}
}
resourcePrefix : 约定资源名称
全局设置:
subprojects {
afterEvaluate {
android {
resourcePrefix "${project.name}_"
}
}
}
sourceSets :指定资源目录
sourceSets {
main {
java {
srcDir 'src/java' // 指定源码目录
exclude '/test/**' // 不想包含文件的路径
include '**/build/**' //想包含的路径
}
resources {
srcDir 'src/resources' //资源目录
exclude '/resource/**' // 不想包含的资源文件路径
}
}
}