Details of automatic signature APK when Jenkins packages Android Applications

Time:2020-12-29

preface

If you are using Android studio to compile the project, you will only automatically sign APK when compiling APK.

But in the default configuration, the APK compiled under Jenkins is unsigned. Some additional settings are required to sign automatically.

This function needs to be modified build.gradle Configuration file to have gradle sign after compilation.

Of course, you can also make some other changes here, such as modifying the name of the compiled APK to add a timestamp of the current time, a suffix of the compilation type, and so on, so as to facilitate identification.

1. The generated APK name plus the current time + modify the APK publishing path

stay build.gradle Under the Android configuration section of the configuration file, under the buildtypes section, find your compiled configuration item, which is generally release

Add the following code under the release section

applicationVariants.all { variant ->
  if ( variant.buildType.name . equals ('release ') {// if it is a release version
   variant.outputs.each { output -> def outputFile = output.outputFile
   if (outputFile != null &&  outputFile.name.endsWith ('. APK')) {// find all apks
    def fileName = "${releaseTime()}_ XXXXPorject_ $+ defaultConfig.versionName }. APK "// redefine the name of apk
    output.outputFile  = new File( outputFile.parent , fileName) // outputFile.parent Parameters can be changed to the publishing path you want
   }
  }
 }
}

And then in the build.gradle Add a method at the end of the configuration file to get the current time

def releaseTime() {
  // return new Date().format("yyyyMMdd",  TimeZone.getTimeZone ("UTC") // mm / DD / yyyy
  return new Date().format("yyyyMMdd HH-mm-ss",  TimeZone.getTimeZone ("GMT + 8:00") // mm / DD / yyyy, H / min / sec
}

2. To realize automatic signature, don’t forget to put JKS file in the project

build.gradle Add the following configuration segment to define the signature key

Signingconfigs {// signature configuration
  release {
   Storefile ("signature. JKS")
   Storepassword 'password'
   Keyalias' alias'
   Keypassword 'password'
  }
}

Add a line under the release section of buildtypes section under Android configuration section to call the above signature configuration

Note that after modifying the name of APK, debugging mode cannot be started in Android studio, indicating that APK cannot be found

signingConfig signingConfigs.release

design sketch

The first is the debug version

The second is the release version without signature

The third is the release version with the name changed after signing

If you want to compile a version, you need to modify the command line parameters of gradle

Reference example

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 {// signature configuration
    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 ') {// if it is a release version
          variant.outputs.each { output -> def outputFile = output.outputFile
            if (outputFile != null &&  outputFile.name.endsWith ('. APK')) {// find all apks
              def fileName = "MIUI Purify_ ${releaseTime()}_ $+ defaultConfig.versionName }. APK "// redefine the name of apk
              output.outputFile  = new File( outputFile.parent , fileName) // outputFile.parent Parameters can be changed to the publishing path you want
            }
          }
        }
      }
    }



  }
  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") // mm / DD / yyyy
  return new Date().format("yyyyMMdd HH-mm-ss",  TimeZone.getTimeZone ("GMT + 8:00") // mm / DD / yyyy, H / min / sec
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]