android 生成apk名字自动已,Jenkins打包android应用时自动签名apk详解

前言

如果你是使用android studio编译项目的化,在编译apk只会会自动给apk签名。

但默认配置下jenkins下编译出的apk是unsign的。需要一些额外的设置才能自动sign。

这个功能需要我们修改下build.gradle配置文件,让gradle在编译之后执行签名。

当然你也可以在这里做一些其他的修改,比如修改编译出的apk的名字,让他加个当前时间的时间戳,编译类型的后缀什么的,方便识别。

1.生成的apk名加上当前时间 + 修改apk的发布路径

在build.gradle配置文件下的android配置段下的buildTypes下找到你的编译配置项一般就是release

在release段下面加上如下代码

applicationVariants.all { variant ->

if (variant.buildType.name.equals('release')) { //如果是release版本

variant.outputs.each { output -> def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith('.apk')) { //查找所有的apk

def fileName = "${releaseTime()}_XXXXPorject_${defaultConfig.versionName}.apk" //重新定义apk的名称

output.outputFile = new File(outputFile.parent, fileName) //outputFile.parent参数可以改成你你想要的发布路径

}

}

}

}

然后在build.gradle配置文件的末尾加上一个方法用来获取当前时间

def releaseTime() {

// return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC")) //年月日

return new Date().format("yyyyMMdd HH-mm-ss", TimeZone.getTimeZone("GMT+8:00")) //年月日时分秒

}

2.实现自动化签名 别忘了jks文件也要放项目里

build.gradle的android段添加如下配置段 定义签名key

signingConfigs {//签名的配置

release {

storeFile file("签名.jks")

storePassword '密码'

keyAlias '别名'

keyPassword '密码'

}

}

在android配置段下的buildTypes段的release段下添加一行 表示调用上面的签名配置

注意修改apk的名字后在android studio是无法开启调试模式,提示找不到apk

signingConfig signingConfigs.release

效果图

第一个是debug版本

第二个是没签名的release版本

第三个是签名后还改了名字的release版本

如果想编译某个版本的话 需要修改下执行gradle的命令行参数

3149ec05b32022789af670a8520841c3.png

参考例子

apply plugin: 'com.android.application'

android {

compileSdkVersion 25

buildToolsVersion '24.0.3'

defaultConfig {

applicationId "com.coderstory.Purify"

minSdkVersion 19

targetSdkVersion 25

versionCode 90

versionName "1.5.0"

resConfigs "cn"

}

repositories {

mavenCentral()

}

signingConfigs {//签名的配置

release {

storeFile file("mykey.jks")

storePassword 'a1234'

keyAlias 'coolapk'

keyPassword 'b1234'

}

}

buildTypes {

release {

shrinkResources true

minifyEnabled true

signingConfig signingConfigs.release

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

applicationVariants.all { variant ->

if (variant.buildType.name.equals('release')) { //如果是release版本

variant.outputs.each { output -> def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith('.apk')) { //查找所有的apk

def fileName = "MIUI Purify_${releaseTime()}_${defaultConfig.versionName}.apk" //重新定义apk的名称

output.outputFile = new File(outputFile.parent, fileName) //outputFile.parent参数可以改成你你想要的发布路径

}

}

}

}

}

}

productFlavors {

}

lintOptions {

abortOnError false

}

}

dependencies {

provided fileTree(include: ['*.jar'], dir: 'libs')

compile project(':library')

compile project(':pull')

compile 'com.android.support:support-v4:25.0.1'

compile 'com.android.support:appcompat-v7:25.0.1'

compile 'com.android.support:design:25.0.1'

testCompile 'junit:junit:4.12'

provided 'de.robv.android.xposed:api:82'

}

def releaseTime() {

// return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC")) //年月日

return new Date().format("yyyyMMdd HH-mm-ss", TimeZone.getTimeZone("GMT+8:00")) //年月日时分秒

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值