关联git版本可参考https://www.oschina.net/code/snippet_2545423_53287?tdsourcetag=s_pcqq_aiomsg
本例为不关联git单机打包release方式实现versionCode和versionName自增.
第一步:
创建一个文件,名字就叫version.properties(当然,这个不一定要这么写),位置和build.gradle文件同级
文件内容
VERSION_CODE=3
第二步:
在build.gradle文件里面加入以下代码(注意:代码的位置和android{...}平级),此方法是获取自增之后的版本号
apply plugin: 'com.android.application'
//获取生成时间
def getVersionTime() {
def date = new Date();
def versionTime = date.format('yyyyMMddHHmmss');
return versionTime
}
def getVersionCode() {// 获取版本号
def versionFile = file('version.properties')// 读取第一步新建的文件
if (versionFile.canRead()) {// 判断文件读取异常
Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionFile))
def versionCode = versionProps['VERSION_CODE'].toInteger()// 读取文件里面的版本号
def runTasks = gradle.startParameter.taskNames
if (':app:assembleRelease' in runTasks ) {//仅在assembleRelease任务是增加版本号,其他渠道包在此分别配置
// 版本号自增之后再写入文件(此处是关键,版本号自增+1)
versionCode++
versionProps['VERSION_CODE'] = versionCode.toString()
versionProps.store(versionFile.newWriter(), null)
}
return versionCode // 返回自增之后的版本号
} else {
throw new GradleException("Could not find version.properties!")
}
}
android {
signingConfigs {
config {
keyAlias 'appkey'
keyPassword '******'
storeFile file('******')
storePassword '******'
}
}
//注意这个引用的代码不能去掉
def currentVersionCode = getVersionCode()
def currentVersionTime = getVersionTime()
compileSdkVersion 26
defaultConfig {
applicationId "com.***.***"
minSdkVersion 17
targetSdkVersion 26
multiDexEnabled true
versionCode currentVersionCode
versionName "V1.0." + currentVersionCode
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
moduleName "serial_port"
}
}
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs = ['libs']
}
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.config
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//更改输出apk名称
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "banPai_${defaultConfig.versionName}_${currentVersionTime}.apk"
}
}
}
debug {
signingConfig signingConfigs.config
applicationIdSuffix 'debug'
versionNameSuffix '-debug'
}
}
buildToolsVersion '27.0.3'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
implementation 'com.google.code.gson:gson:2.8.2'
implementation files('libs/rkapi.jar')
}
输出结果:
需要特别注意的是,
:app:assembleRelease为任务名称,不要写错.
引用不能省略
多渠道打包参考地址https://blog.csdn.net/AaronYB/article/details/80594220