转载请标明出处:http://blog.csdn.net/xx326664162/article/details/50538148 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货!
生成APK自动追加版本号
可自动区分debug和release,并追加版本号:
-
打开 build.gradle
-
在 Android 节点中插入下面代码
第一种:
applicationVariants.all{ variant->
variant.outputs.each { output->
def oldFile = output.outputFile
def newName = '';
if(variant.buildType.name.equals('release')){
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
- 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" )
}
}
groovy语言执行的最后一行是返回值
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'
}
}
}
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"
}
release {
File strFile = new File("../../Keystore/Release/release.jks")
storeFile file(strFile)
storePassword "storeRelease1234567890"
keyPassword "keyRelease1234567890"
keyAlias "releasekey"
}
}
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
- 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
2、对于 Release配置,在 buildTypes中必须指定
signingConfig signingConfigs.release
否则,会出现
Error: The apk for your currently selected variant(app-release-unsigned.apk) is not signed. please specify a signing configuration for this variant(release)
3、 signingConfigs必须在 buildTypes前面声明,否则会出现找不到配置选项的错误。