Android APK打包脚本
build.gradle版本
同目录创建config.gradle文件写入需要的信息入
config.gradle文件内容
ext {
/**
* 自定义APP运行环境
* dev: 开发
* test: 测试
* pro: 生产
*/
env = "pro"
/**
* 动态参数配置,根据自己需要添加参数
* APP_ID: 包名
* VERSION_CODE: 版本号
* VERSION_NAME: 版本名
* HOST: 网络请求前缀
*/
dev = [
APP_ID: "包名",
APP_NAME: "(开发版)",
VERSION_CODE: 1,
VERSION_NAME: "1.0.0",
HOST: "网络请求前缀"
]
pro = [
APP_ID: "包名",
APP_NAME: "正式包",
VERSION_CODE: 1,
VERSION_NAME: "1.0.0",
HOST: "网络请求前缀",
]
//签名文件
sign = [
STORE_FILE: "文件地址",
STORE_PASSWORD: "密码",
KEY_ALIAS: "秘钥名",
KEY_PASSWORD: "秘钥密码"
]
}
build.gradle中如何引用
使用 apply from: ‘config.gradle’ 添加脚本文件
将build.gradle文件中的相关信息替换成脚本文件的内容
def env = project.ext.env // 环境
def config = project.ext[env] // 根据环境获取配置信息
def sign = project.ext.sign
//noinspection GradleCompatible
compileSdkVersion 31
buildToolsVersion "29.0.3"
defaultConfig {
applicationId config.APP_ID
minSdkVersion 21
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 29
versionCode config.VERSION_CODE
versionName config.VERSION_NAME
resValue "string", "app_name", config.APP_NAME
buildConfigField "String", "HOST", "\"${config.HOST}\""
buildConfigField "String", "PATH", "\"${config.PATH}\""
buildConfigField "String", "HALL_OID", "\"${config.HALL_OID}\""
manifestPlaceholders = [FILE_PROVIDER_AUTHORITIES: config.APP_ID + ".fileProvider"]
signingConfigs {
release {
storeFile file(sign.STORE_FILE)
storePassword sign.STORE_PASSWORD
keyAlias sign.KEY_ALIAS
keyPassword sign.KEY_PASSWORD
v1SigningEnabled true
v2SigningEnabled true
}
}
//设置打包生成的apk命名
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.all { output ->
if(!variant.buildType.isDebuggable()) {
outputFileName = "${config.APP_NAME}_V${variant.versionName}_${new Date().format("yyyyMMddHHmm")}.apk"
}
}
}
signingConfig signingConfigs.release
}
}
build.gradle.kts版本
创建的config.gradle文件,内容见上方config.gradle文件内容
build.gradle和build.gradle.kts的区别在于前面是常规的写法,后面是kotlin写法。
添加脚本文件
apply(from = "config.gradle")
替换内容
val env = extra["env"] as String
val config = extra[env] as Map<*, *>
val sign = extra["sign"] as Map<*, *>
namespace = config["APP_ID"] as String
compileSdk = 34
defaultConfig {
applicationId = config["APP_ID"] as String
minSdk = 21
targetSdk = 34
versionCode = config["VERSION_CODE"] as Int
versionName = config["VERSION_NAME"] as String
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
resValue("string", "app_name", config["APP_NAME"] as String)
buildConfigField("String", "HOST", "\"${config["HOST"] as String}\"")
buildConfigField("String", "WEBSOCKET", "\"${config["WEBSOCKET"] as String}\"")
buildConfigField("String", "APP_KEY", "\"${config["APP_KEY"] as String}\"")
buildConfigField("String", "APP_SECRET", "\"${config["APP_SECRET"] as String}\"")
manifestPlaceholders["FILE_PROVIDER_AUTHORITIES"] = applicationId + ".fileProvider"
}
signingConfigs {
getByName("debug") {
storeFile = file(sign["STORE_FILE"] as String)
storePassword = sign["STORE_PASSWORD"] as String
keyAlias = sign["KEY_ALIAS"] as String
keyPassword = sign["KEY_PASSWORD"] as String
}
create("release") {
storeFile = file(sign["STORE_FILE"] as String)
storePassword = sign["STORE_PASSWORD"] as String
keyAlias = sign["KEY_ALIAS"] as String
keyPassword = sign["KEY_PASSWORD"] as String
}
}
//编写打包生成的apk名是使用了日期个时间的类型,kotlin不像build.gradle可以直接使用需要引入相关类
//import java.text.SimpleDateFormat
//import java.util.Date
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
applicationVariants.all {
outputs.all {
if (this is com.android.build.gradle.internal.api.ApkVariantOutputImpl)
outputFileName = "${config["APP_NAME"] as String}_V${config["VERSION_NAME"] as String}_${ SimpleDateFormat("yyMMdd").format(
Date()
) }.apk"
}
}
}
}
项目中使用脚本字段,先编译项目,生成BuildConfig
直接在项目中使用 BuildConfig.VERSION_CODE 获取包名或者项目版本。
其中 manifestPlaceholders = [FILE_PROVIDER_AUTHORITIES: config.APP_ID + “.fileProvider”] 创建的是FileProvider中的authorities属性
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${FILE_PROVIDER_AUTHORITIES}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
项目中使用
FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileProvider", apk);
需要修改的话只需要在config.gradle脚本文件中修改相关字段并重新编译项目即可