自动签名
signingConfigs {
release {
storeFile file("release.keystore.jks")
storePassword "xxxxx"
keyAlias "xxxxxx"
keyPassword "xxxx"
}
debug {
storeFile file("debud.keystore.jks")
storePassword "xxxx"
keyAlias "xxxx"
keyPassword "xxxx"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
....
}
debug {
signingConfig signingConfigs.debug
....
}
}
版本号递增
一般版本号都会用 **major.minor.patch
** 表示,例如 **1.0.0
** 这样的形式。第一个数字major
表示主版本号,第二个数字minor
表示副版本号,第三个数字patch
表示小版本号或者叫补丁号。
这里的递增主要针对versionCode
,主要方式有本地文件和根据版本仓库的commit
次数;而versionName
的规则可以根据实际情况自定义的,这里我主要使用了本地文件的方式来实现的。
- 在
module
目录下新建文件version.properties
,然后添加以下内容:
tag=alpha // alpha、test、release...
build.patch=1
build.code=1 // 每次build后自增
release.code=1 // 每次打release包后自增
- 在
build.gradle
文件中,定义getVersionCode
和getVersionName
方法
def getVersionCode() {
def runTasks = gradle.startParameter.taskNames
def versionFile = file('version.properties')
if (versionFile.exists()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionFile))
if ('assembleRelease' in runTasks || 'aR' in runTasks) {
def versionCode = versionProps['release.code'].toInteger()
return versionCode
} else {
def versionCode = versionProps['build.code'].toInteger()
return versionCode
}
}
}
def getVersionName() {
def File versionFile = file('version.properties')
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionFile))
def patch = versionProps['build.patch']
def tag = versionProps['tag']
if (patch != null) {
def name = patch.toString() + tag.toString()
return name
} else {
return "1.0.0"
}
}
- 打包时动态修改
versionName
和versionCode
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
def currentVersionCode = getVersionCode()
def currentVersionName = VERSION_NAME + getReleaseTag()
defaultConfig {
applicationId "com.lichfaker.test"
minSdkVersion 14
targetSdkVersion 23
versionCode currentVersionCode
versionName currentVersionName
...
}
applicationVariants.all { variant ->
def versionCode = getVersionCode()
def versionName = getReleaseTag()
variant.mergedFlavor.versionCode = versionCode
variant.mergedFlavor.versionName = versionName
}
}
- 更新版本号
def updateVersionCode() {
def runTasks = gradle.startParameter.taskNames
if (!('assemble' in runTasks || 'assembleRelease' in runTasks || 'aR' in runTasks || 'aB' in runTasks)) {
return
}
def File versionFile = file('version.properties')
if (versionFile.exists()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionFile))
def patchs = versionProps['build.patch'].toInteger()
patchs++
versionProps['build.patch'] = patchs.toString()
if ('assembleRelease' in runTasks || 'assembleBeta' in runTasks || 'aR' in runTasks || 'aB' in runTasks) {
def versionCode = versionProps['release.code'].toInteger()
versionCode++
versionProps['release.code'] = versionCode.toString()
println('Updated version code to ' + versionCode.toString())
versionProps.store(versionFile.newWriter(), null)
} else if ('assembleDebug' in runTasks || 'aD' in runTasks) {
def versionCode = versionProps['build.code'].toInteger()
versionCode++
versionProps['build.code'] = versionCode.toString()
println('Updated version code to ' + versionCode.toString())
versionProps.store(versionFile.newWriter(), null)
}
} else {
throw new GradleException("Could not find version.properties!")
}
}
assembleRelease {}.doLast {
updateVersionCode()
}
assembleDebug {}.doLast {
updateVersionCode()
}
自动关闭Log开关
通过BuildConfig
类来关闭,在Gradle
打包时指定boolean变量,在日志控制上使用Log(BuildConfig.LOG_DEBUG)
buildTypes {
release {
buildConfigField "boolean", "LOG_DEBUG", "false"
....
}
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"
....
}
}
自定义apk输出文件名
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
buildTypes {
release {
signingConfig signingConfigs.release
...
// 'release' 是productFlavors,根据情况调整
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk') && outputFile.name.contains('release')) {
def fileName = "globalvillage_v" + VERSION_NAME +"_${releaseTime()}_release.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
微信资源混淆及多渠道打包
参考:多渠道打包与AndResGuard的结合实践
其中:project.afterEvaluate
中的内容可在resguard{} .doLast
中实现, 效果一致