当前位置: 移动技术网 > IT编程>移动开发>Android > Jenkins打包android应用时自动签名apk详解

Jenkins打包android应用时自动签名apk详解

2019年09月06日  | 移动技术网IT编程  | 我要评论

折翼天使之城3,最新一周立波秀,ndf外挂

前言

如果你是使用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的命令行参数

参考例子

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")) //年月日时分秒
}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网