安卓AS3.0版本根据打release版本的次数实现版本号自增-输出APK名带版本号-无关联git

关联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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值