android打包
android打包应用及配置的参考
前言
提示:最近使用到了打包,刚好有时间,今天梳理一下并记录:主要是配置和打包
提示:以下是本篇文章正文内容,下面案例可供参考
一、gradle配置
示例:
1. app.gradle下配置
都在android{
… …在这里配置
}
apply plugin: 'com.android.application'
//获取编译时间
static def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.backbay2.xxxxx"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
....
//自定义的命名格式,配置1.1
}
1.1 打包自定义的命名格式
app.gradle>android{…}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def fileName
if (output.name == 'debug') {
fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${"Test"}_${releaseTime()}.apk"
} else {
fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${output.name}_${releaseTime()}.apk"
}
output.outputFileName = fileName
}
}
编译出现:API ‘variantOutput.getPackageApplication()’ is obsolete and has been replaced with ‘variant.getPackageApplicationProvider()’.
解决方法
applicationVariants.all { variant ->
variant.outputs.each { output ->
variant.packageApplicationProvider.get().outputDirectory = new File(project.rootDir.absolutePath + "/apk")
def fileName
if (output.name == 'debug') {
fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${"Test"}_${releaseTime()}.apk"
} else {
fileName = "AppName_v${defaultConfig.versionName}_c${defaultConfig.versionCode}_${output.name}-${releaseTime()}.apk"
}
output.outputFileName = fileName
}
}
生成的包在根目录为apk的文件夹
1.2 签名配置
app.gradle->android{…}
配置签名:
第一步->在gradle.properties中配置
backbay2.jks放在根目录—> 跟这个位置RELEASE_STOREFILE
=../backbay2.jks
一定是…/,因为我们是app.gradle文件使用的这个配置。
gradle.properties
RELEASE_STOREFILE=../backbay2.jks
RELEASE_STORE_PASSWORD=你的密码
RELEASE_KEY_ALIAS=你写的别名ALias
RELEASE_KEY_PASSWORD=你的密码
signingConfigs {//设置签名文件
// debug {
// storeFile file(RELEASE_STOREFILE)
// storePassword RELEASE_STORE_PASSWORD
// keyAlias RELEASE_KEY_ALIAS
// keyPassword RELEASE_KEY_PASSWORD
// }
release {
storeFile file(RELEASE_STOREFILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
buildTypes {//设置build类型
release {
//是否优化zip
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources true
//启用代码混淆
minifyEnabled true
//防止方法数量超过65536导致错误
multiDexEnabled = true
//混淆规则配置文件
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
//指明签名文件位置
signingConfig signingConfigs.release
// buildConfigField "Boolean", "DEBUG_MODE", 'false'
}
debug {
shrinkResources false
minifyEnabled false
zipAlignEnabled true
multiDexEnabled = true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
// buildConfigField "Boolean", "DEBUG_MODE", 'true'
}
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了android打包应用及配置的参考。具体的怎么生成应用密钥博客很多。这里不多叙述了。本文是选择了一个我个人觉得可取的写法。网上其他人的同样适用,看需求。