Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,目前也增长了基于Kotlin语言的kotlin-based DSL,抛弃了基于XML的各类繁琐配置,面向Java应用为主。当前其支持的语言限于Java、Groovy、Kotlin和Scala,计划将来将支持更多的语言。 AndroidStudio使用Gradle做为基础的构建工具,因此咱们须要对Gradle作进一步的了解,这就提到了Groovy语言。javascript
Groovy是一种基于JVM(Java虚拟机)的敏捷开发语言,它结合了Python、Ruby和Smalltalk的许多强大的特性,Groovy 代码可以与 Java 代码很好地结合,也能用于扩展示有代码。因为其运行在JVM上的特性,Groovy也可使用其余非Java语言编写的库。Groovy是用于Java虚拟机的一种敏捷的动态语言,它是一种成熟的面向对象编程语言,既能够用于面向对象编程,又能够用做纯粹的脚本语言。使用该种语言没必要编写过多的代码,同时又具备闭包和动态语言中的其余特性。java
在项目中,有可能出现多个module的状况,不少moudle都拥有相同的依赖,好比V7包,若是想作V7包版本的变更,极可能形成版本的不兼容。因此对依赖进行统一的管理对于有多个moudle的状况很是方便。 那么如何进行统一管理呢?android
在主项目根目录下创建 config.gradlegit
编写以下代码,对须要用到的版本号和库进行声明。github
ext {
android = [
minSdkVersion : 16,
compileSdkVersion: 26,
targetSdkVersion : 26,
buildToolsVersion: "26.0.2",
supportVersion : "26.1.0",
versionCode : 1,
versionName : "1.0",
resConfigs : "zh"
]
plug = [
mainVersion : 1,
mainVersionName : '1.0.0',
libBaseVersion : 1,
libBaseVersionName : '1.0.0',
libStyleVersion : 1,
libStyleVersionName: '1.0.0',
libUtilVersion : 1,
libUtilVersionName : '1.0.0'
]
def supportVersion = "26.1.0"
def constraintSupportVersion = "1.1.3"
def cardViewVersion = '25.3.0'
dependencies = [
// Android support library
SupportDesign : "com.android.support:design:$supportVersion",
SupportAppcompatV7 : "com.android.support:appcompat-v7:$supportVersion",
SupportConstraint : "com.android.support.constraint:constraint-layout:$constraintSupportVersion",
CardView : "com.android.support:cardview-v7:$cardViewVersion",
]
}
复制代码
打开项目的build.gradle,在其最上方添加代码编程
apply from: "config.gradle
复制代码
对版本号作统一管理:微信
//实例化统一版本管理器
def app = rootProject.ext.android
compileSdkVersion app.compileSdkVersion
efaultConfig {
applicationId "com.tianzi.chuji"
minSdkVersion app.minSdkVersion
targetSdkVersion app.targetSdkVersion
versionCode app.versionCode
versionName app.versionName
multiDexEnabled true
ndk {}
}
复制代码
对第三方库作统一管理:闭包
dependencies {
def app = rootProject.ext.dependencies compile fileTree(dir: 'libs', include: ['*.jar']) compile app.SupportAppcompatV7 compile app.SupportDesign compile app.CardView compile app.Gson compile app.Okttp compile app.OkHttpLoggingInterceptor compile app.Retrofit compile app.RetrofitConverterGson compile app.RetrofitAdapterRxJava compile app.RxJava compile app.RxAndroid compile app.EventBus compile app.SmartRefreshLayout compile app.SmartRefreshHeader compile app.AndroidAnnotations compile app.RxKit compile app.RxUi } 复制代码
咱们在打包apk时,会有一些项目配置,好比是否混淆、是否压缩、是否消除冗余资源引用。若是都都写在gradle中是难以管理的,最好进行统一的管理。app
在打包的moudle根目录下创建gradle-build.properties文件。编程语言
#是否发布
deliver=true
#发布位置
appReleaseDir=C:/Users/xxhdp/Desktop/release/release
#签名信息
keyAlias=xxxxxx
keyPassword=xxxxxx
storeFile=xxxxxx
storePassword=xxxxxx
#打包配置
############################## release ###################
#Log开关 上线版本设置为“false”
release_LOG_DEBUG=true
#上线版本设置为“false”
release_isDev=false
release_versionNameSuffix="-release"
#是否混淆
release_minifyEnabled=false
#是否资源优化
release_zipAlignEnabled=true
#是否删除无效资源
release_shrinkResources=false
############################## debug ######################
#Log开关 上线版本设置为“false”
debug_LOG_DEBUG=true
#是不是线下版本 上线版本设置为“false”
debug_isDev=true
debug_versionNameSuffix="-debug"
#是否混淆
debug_minifyEnabled=false
#是否资源优化
debug_zipAlignEnabled=true
#是否删除无效资源
debug_shrinkResources=false
复制代码
使用Groovy语法定义两个变量,其实只须要一个就够了,我这里本身增长了一条打包路径
ext.buildProperty = null
ext.appReleaseDir = ''
def loadProperties() {
def proFile = file("gradle-build.properties")
Properties p = new Properties()
proFile.withInputStream { stream ->
p.load(stream)
}
buildProperty = p
appReleaseDir = p.appReleaseDir
}
loadProperties()//调用方法
复制代码
signingConfigs {
debug {
keyAlias buildProperty.keyAlias
keyPassword buildProperty.keyPassword storeFile file(buildProperty.storeFile) storePassword buildProperty.storePassword } release {
keyAlias buildProperty.keyAlias
keyPassword buildProperty.keyPassword storeFile file(buildProperty.storeFile) storePassword buildProperty.storePassword } } 复制代码
buildTypes {
debug {
buildConfigField "boolean", "LOG_DEBUG", buildProperty.debug_LOG_DEBUG
buildConfigField "boolean", "isDev", buildProperty.debug_isDev
buildConfigField "String", "versionNameSuffix", buildProperty.debug_versionNameSuffix
minifyEnabled Boolean.parseBoolean(buildProperty.debug_minifyEnabled)
zipAlignEnabled Boolean.parseBoolean(buildProperty.debug_zipAlignEnabled)
shrinkResources Boolean.parseBoolean(buildProperty.debug_shrinkResources)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.debug } release {
buildConfigField "boolean", "LOG_DEBUG", buildProperty.release_LOG_DEBUG
buildConfigField "boolean", "isDev", buildProperty.release_isDev
buildConfigField "String", "versionNameSuffix", buildProperty.release_versionNameSuffix
minifyEnabled Boolean.parseBoolean(buildProperty.release_minifyEnabled)
zipAlignEnabled Boolean.parseBoolean(buildProperty.release_zipAlignEnabled)
shrinkResources Boolean.parseBoolean(buildProperty.release_shrinkResources)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "ChuJi_v${app.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
if (Boolean.parseBoolean(buildProperty.deliver)) {//是否正式发布,须要配置发布版打包位置
output.outputFile = new File(appReleaseDir + '/' + releaseTime(), fileName)
}
}
}
}
}
}
复制代码
只须要在productFlavors里面添加须要的渠道名便可
productFlavors {//配置渠道包或者版本包
'ChuJi' {
}
'BaiDu' {
}
}
productFlavors.all { flavors ->
flavors.manifestPlaceholders = [channelValue: name]
}
复制代码
在buildTypes中有这段代码,在打开release模式的状况下,会按照以下方式在gradle-build.properties声明的位置生成fileName格式的apk包。
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "ChuJi_v${app.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
if (Boolean.parseBoolean(buildProperty.deliver)) {//是否正式发布,须要配置发布版打包位置
output.outputFile = new File(appReleaseDir + '/' + releaseTime(), fileName)
}
}
}
}
复制代码
经过gradle方式进行打包,点击项目右侧gradle标签,展开项目下须要打包的moudle,点击Task展开列表,再展开build列表。点击assembleRelease便可打包。打包成功后经过乐固进行apk加固。
长路漫漫,菜不是原罪,堕落才是原罪。
个人CSDN:blog.csdn.net/wuyangyang_…
个人简书:www.jianshu.com/u/20c2f2c35…
个人掘金:juejin.im/user/58009b…
个人GitHub:github.com/wuyang2000
我的网站:www.xiyangkeji.cn
我的app(茜茜)蒲公英链接:www.pgyer.com/KMdT
个人微信公众号:茜洋 (按期推送优质技术文章,欢迎关注)
Android技术交流群:691174792
以上文章都可转载,转载请注明原创。