Android Studio生成APK自动追加版本号 自定义apk名称 指定签名证书文件

时间:2019-02-14
本文章向大家介绍Android Studio生成APK自动追加版本号 自定义apk名称 指定签名证书文件,主要包括Android Studio生成APK自动追加版本号 自定义apk名称 指定签名证书文件使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
                     

转载请标明出处:http://blog.csdn.net/xx326664162/article/details/50538148   文章出自:薛瑄的博客

你也可以查看我的其他同类文章,也会让你有一定的收货!

生成APK自动追加版本号

可自动区分debug和release,并追加版本号:

  1. 打开 build.gradle

  2. 在 android 节点中插入下面代码

第一种:

   applicationVariants.all{ variant->        variant.outputs.each { output->            def oldFile = output.outputFile            def newName = '';            if(variant.buildType.name.equals('release')){               // println(variant.productFlavors[0].name)                   // def releaseApkName = 'study-' + defaultConfig.versionName + '-luckpan.apk'                def releaseApkName = defaultConfig.applicationId + "-" + buildType.name + "-" + defaultConfig.versionName + '.apk'                output.outputFile = new File(oldFile.parent, releaseApkName)            }            if(variant.buildType.name.equals('beta')){                newName = oldFile.name.replace(".apk", "-v" + getVersionNameFromManifest() + "-build" + getDate() + ".apk")                output.outputFile = new File(oldFile.parent, newName)            }            if(variant.buildType.name.equals('debug')){            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

第二种:

把productFlavors名字和 buildType名字,打包到apk文件名中

android.applicationVariants.all { variant ->    variant.outputs.each { output ->        output.outputFile = new File(output.outputFile.parent,         defaultConfig.applicationId + "-" + buildType.name + "-v" +         defaultConfig.versionName "-" + variant.productFlavors.name + "-" + defaultConfig.versionCode +                    ".apk" );    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

groovy语言执行的最后一行是返回值

  • 函数 getVersionNameFromManifest(),从manifest文件中读取的版本号

  • 版本号定义在build.gradle中,那defaultConfig.versionName就是你的版本号

Android Studio设置默认的签名文件

Android Studio 指定签名证书文件
Gradle for Android(三)多渠道打包、配置签名信息

新浪微博SSO登录,微信分享这些都需要签名打包,才能看到效果,设置默认签名文件为自己的签名jks,这样就不需要打包了直接运行起来就是正式的签名。

在android.signingConfigs{}下定义一个或者多个签名信息,然后在buildTypes{}配置使用即可。
在app目录下添加你的.jks,然后app的build.gradle文件中的增加以下内容:

第一种:

android {      signingConfigs {          debug {              storeFile file("WuXiaolong.jks")            // storeFile file("C:\\Users\\he\\apk\\apksign") 指定路径              storePassword 'android'            keyAlias 'android'            keyPassword 'android'        }              }      }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

buildTypes没有配置,也是直接取得debug,是不是不配置默认取得是debug呢?

第二种:

android {    compileSdkVersion 21    buildToolsVersion "21.1.0"    defaultConfig {        applicationId "com.test.example"        minSdkVersion 14        targetSdkVersion 21        versionCode 1        versionName "1.0"    }    signingConfigs {        debug {            File strFile = new File("../../Keystore/Debug/debug.jks")            storeFile file(strFile)            storePassword "storeDebug1234567890"            keyAlias "debugkey"            keyPassword "aliasDebug1234567890"            //println strFile.absolutePath;        }        release {            File strFile = new File("../../Keystore/Release/release.jks")            storeFile file(strFile)            storePassword "storeRelease1234567890"            keyPassword "keyRelease1234567890"            keyAlias "releasekey"            // println strFile.absolutePath;        }    }    buildTypes {        release {            signingConfig  signingConfigs.release            runProguard false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

注意:
1、storeFile, storePassword, keyAlias, keyPassword缺一不可,都必须填写,并且填写正确。

如果没有填写 keyAlias,则签名时候会报告 Android-APK signing error : Failed to read key from keystore

密码不正确的时候,会报告

java.security.UnrecoverableKeyException: Cannot recover keyThis exception may result from the fact that you had provided a key password that was different from the keystore password
  • 1
  • 2

2、对于 Release配置,在 buildTypes中必须指定

signingConfig signingConfigs.release
  • 1

否则,会出现

Error: The apk for your currently selected variant(app-release-unsigned.apk) is not signed. please specify a signing configuration for this variant(release)
  • 1

3、 signingConfigs必须在 buildTypes前面声明,否则会出现找不到配置选项的错误。

参考:http://www.sollyu.com/android-apk-studio-generated-automatically-appends-a-version-number/

http://www.th7.cn/Program/Android/201501/380594.shtml?WebShieldDRSessionVerify=SVUnBrv9lvKUzUH4e6nR

http://blog.csdn.net/jt_ontheway/article/details/50482210

 

关注我的公众号,轻松了解和学习更多技术