Jenkins中怎么打包android应用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
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的命令行参数
参考例子
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")) //年月日时分秒
}
看完上述内容,你们掌握Jenkins中怎么打包android应用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。