1.添加jks相关配置
test.properties
KEYSTORE_FILE=../key/test.jks
KEYSTORE_PASSWORD= 123456
KEY_ALIAS=a
KEY_PASSWORD=123456
注意这里使用的相对路径
build.gradle
//加载签名配置的文件
Properties props = new Properties()
props.load(new FileInputStream(file("../key/test.properties")))
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.sevenday.mytest"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
signingConfigs {
release {
keyAlias props['KEY_ALIAS']
keyPassword props['KEY_PASSWORD']
storePassword props['KEYSTORE_PASSWORD']
v1SigningEnabled true
v2SigningEnabled true
storeFile file(props['KEYSTORE_FILE'])
}
}
buildTypes {
debug {
signingConfig signingConfigs.release
}
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
2.打包方式
默认方式
按照第一步配置就可以打包了
1.使用工具栏中build方式
2.直接使用gradle中task的build任务
结果:在默认文件夹中生成apk
修改包名
在默认方式中,我们一般会添加修改包名的方式
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
if (variant.buildType.name.equals('release')) {
variant.outputs.all { output ->
def buildName = "Downloader"
def type = variant.buildType.name
def releaseApkName = buildName + '_' + type + "_" + versionName + '.apk'
outputFileName = releaseApkName
}
}
}
}
修改包名,修改保存路径
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
if (variant.buildType.name.equals('release')) {
variant.outputs.all { output ->
def buildName = "Downloader"
def type = variant.buildType.name
def releaseApkName = buildName + '_' + type + "_" + versionName + '.apk'
outputFileName = releaseApkName
variant.packageApplication.outputDirectory = new File("./apk")
}
}
}
}
使用task任务移动apk
/// **********************移动文件*************************
build {
doLast {
def outFileDir = "../out";
FileTree tree = fileTree(dir: './build/outputs/apk/')
tree.include '**/app-*.apk'
tree.exclude '**/output.json'
// 遍历文件树
println "projectDir=$projectDir rootDir=$rootDir buildDir=$buildDir"
tree.each {File file ->
if (file.name.startsWith("app-")&&file.name.endsWith(".apk")) {
println file.absolutePath
// def fileName=""
// def fileName=outFileDir + "/vspn_v${android.defaultConfig.versionName}_v${android.defaultConfig.versionCode}_${releaseTime}_Build${buildSerialNumber}"
def fileName=outFileDir + "/test_v${android.defaultConfig.versionName}_v${android.defaultConfig.versionCode}"
if (file.name.contains("debug")){
fileName+="_debug"
}
fileName+=".apk"
//file.renameTo(fileName)
copy {
from file
into outFileDir
rename {
String srcFileName ->fileName
}
}
}
}
println("=====================build.doLast success.=========================")
}
}
只是移动打包好的apk文件,并改名