为什么要全局配置Gradle?
答:为方便在不同module中设置版本号等配置信息,可以通过配置全局变量来统一所有module的公共配置信息。
Gradle全局配置流程
-
在项目的根目录下新建config.gradle文件(如下图)
自定义config.gradle
2..将gradle中的公共部分写入 <font color = #008800>config.gradle</font> 文件中:
ext {
androidSupportVersion ='25.3.1'
android = [compileSdkVersion : 26,//编译的 SDK 版本,如API20
//构建工具的版本,其中包括了打包工具aapt、dx等,如API20对应的build-tool的版本就是20.0.0
buildToolsVersion : "26.0.2",
//兼容的最低 SDK 版本
minSdkVersion : 21,
//向前兼容,保存新旧两种逻辑,并通过 if-else 方法来判断执行哪种逻辑
targetSdkVersion : 22,
applicationId : 'com.suchengkeji.andeoid.confingdemo',
versionCode : 1,
versionName : 'v 1.1.1',// versionName 最后组成的是1.1.1
defaultPublishConfig : 'release',
publishNonDefault : true,
testInstrumentationRunner: 'android.support.test.runner.AndroidJUnitRunner'
]
dependencies = ["appcompat-v7": "com.android.support:appcompat-v7:${androidSupportVersion}",
//"appcompat-v7": "com.android.support:appcompat-v7:25.3.1",
"constraint" : "com.android.support.constraint:constraint-layout:1.0.2",
"junit" : 'junit:junit:4.12',
]
}
3.在主项目根目录的build.gradle中申明一下(声明在project/buile.gradle
下):
apply from :"config.gradle"
4在项目中(app/build.gradle
)引用我们的路径配置(如图:)
app/build.gradle
整体文件
apply plugin: 'com.android.application'
def Buildconfig = rootProject.ext.android
def BuildLibray = rootProject.ext.dependencies
android {
//版本配置
buildToolsVersion Buildconfig["buildToolsVersion"]
compileSdkVersion Buildconfig["compileSdkVersion"]
defaultConfig {
applicationId Buildconfig["applicationId"]
//SDK配置
minSdkVersion Buildconfig["minSdkVersion"]
targetSdkVersion Buildconfig["targetSdkVersion"]
versionCode Buildconfig["versionCode"]
versionName Buildconfig["versionName"]
}
//签名的配置
signingConfigs {
debug {
storeFile file('H:\\configTest.jks')//签名文件路径
storePassword "123456"//签名密钥密码
keyAlias "configTest"//别名
keyPassword "123456" //签名公钥密码
println("====== signingConfigs.debug ======")
}
release {
storeFile file('H:\\configTest.jks')//签名文件路径
storePassword "123456"
keyAlias "configTest"
keyPassword "123456" //签名密码
println("====== signingConfigs.release ======")
}
}
buildTypes {
release {
minifyEnabled false//是否启动混淆
zipAlignEnabled false//是否启动zipAlign优化
shrinkResources false // 是否移除无用的resource文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release//打包命令行:gradlew assembleRelease
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
File outputDirectory = new File(outputFile.parent);
if (variant.buildType.name == "release") {
//app名称_vnandroid.support.test.runner.AndroidJUnitRunner_vc1_release.apk
//def fileName = "app名称_vn${defaultConfig.versionName}_vc${defaultConfig.versionCode}_release.apk"
//名称ConfigTest_版本v 1.1.1_时间2017-12-26_打包渠道wandoujia.apk
def fileName = "ConfigTest_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputDirectory, fileName)
}
}
}
}
}
}
}
//配置多渠道打包
productFlavors {
zejian {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
huawei {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
_360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
wandoujia {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
//配置多渠道打包简写方法
// productFlavors {
//
// wandoujia {}
// baidu {}
// c360 {}
// uc {}
//
// productFlavors.all { flavor ->
// flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
// }
//
// }
lintOptions {
//设置编译的lint开关,程序在buid的时候,会执行lint检查,有任何的错误或者警告提示,都会终止构建
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
////依赖配置
compile BuildLibray["appcompat-v7"]
compile BuildLibray["constraint"]
}
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("GMT"))
}
网友评论