1. 添加 maven 仓库
1. 1. settings配置
1. 1.1. settings.gradle
repositories {
maven {url 'https://maven.aliyun.com/repository/public/'}
mavenCentral()
}
1. 1.2. settings.gradle.kts
repositories {
maven {
setUrl("https://maven.aliyun.com/repository/public/")
}
mavenCentral()
}
1. 2. gradle配置
1. 2.1. build.gradle
implementation 'com.github.bumptech.glide:glide:4.12.0'
1. 2.2. build.gradle.kts
implementation(libs.glide)
版本号在gradle文件夹下 libs.versions.toml文件中添加
1. 2.2.1. build.gradle.kts
libs.versions.toml文件用来抽离依赖来加载,文件由 4 个主要部分组成:
(1)[versions]部分用于声明可以被依赖项引用的版本
(2)[libraries]部分用于声明坐标的别名
(3)[bundles]部分用于声明依赖包
(4)[plugins]部分用于声明插件
注意:不要使用驼峰命名方式,单词使用 - 分割:
[versions]
groovy = "3.0.5"
checkstyle = "8.37"
[libraries]
groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer="3.9" } }
[bundles]
groovy = ["groovy-core", "groovy-json", "groovy-nio"]
[plugins]
versions = { id = "com.github.ben-manes.versions", version = "0.45.0" }
2. Android Studio 如何配置打包后的文件名称
我们在新建一个项目然后直接进行Build apk,可以生成一个app_debug.apk的apk文件,那么文件是怎么产生的呢?
2.1. build.gradle
//**********打包设置开始**********
//自定义生成的apk的地址及名称
def apkName;
signingConfigs {
release {
v1SigningEnabled true
v2SigningEnabled true
storeFile file('./keystore/insour_szyj.keystore')
storePassword 'insour_szyj'
keyAlias 'insour_szyj'
keyPassword 'insour_szyj'
}
debug {
v1SigningEnabled true
v2SigningEnabled true
storeFile file('./keystore/insour_szyj.keystore')
storePassword 'insour_szyj'
keyAlias 'insour_szyj'
keyPassword 'insour_szyj'
}
}
buildTypes {
debug {
// minifyEnabled false//混淆
minifyEnabled true
shrinkResources true
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
apkName = "szyj.apk"
signingConfig signingConfigs.release
}
release {
// minifyEnabled false//混淆
minifyEnabled true
shrinkResources true
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
apkName = "szyj.apk"
signingConfig signingConfigs.release
}
}
android.applicationVariants.all { variant ->
variant.outputs.all {
if (outputFileName.endsWith('.apk')) {
//这里使用之前定义apk文件名称
outputFileName = apkName
}
}
}
// android.applicationVariants.all {
// variant -> variant.outputs.all { output ->
// def date = new Date().format("yyMMdd",
// TimeZone.getTimeZone("GMT+08"))
// if (variant.buildType.name == 'debug'){
// output.outputFileName = "项目名称_" +
// "${android.defaultConfig.versionName}_${date}_debug.apk"
// }else if (variant.buildType.name == 'release'){
// output.outputFileName = "项目名称_" +
// "${android.defaultConfig.versionName}_${date}_release.apk"
// }
// }
// }
//**********打包设置结束**********
2.2. build.gradle.kts
//**********打包设置开始**********
signingConfigs {
// create("release") {
// storeFile = file("./keystore/insour_szyj.keystore")
// storePassword = "insour_szyj"
// keyAlias = "insour_szyj"
// keyPassword = "insour_szyj"
// }
getByName("debug") {
enableV1Signing =true
enableV2Signing =true
enableV3Signing =true
enableV4Signing =true
storeFile = file("./keystore/insour_szyj.keystore")
storePassword = "insour_szyj"
keyAlias = "insour_szyj"
keyPassword = "insour_szyj"
}
register("release") {
enableV1Signing =true
enableV2Signing =true
enableV3Signing =true
enableV4Signing =true
storeFile = file("./keystore/insour_szyj.keystore")
storePassword = "insour_szyj"
keyAlias = "insour_szyj"
keyPassword = "insour_szyj"
}
}
buildTypes {
debug {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(getDefaultProguardFile(
"proguard-android-optimize.txt"),
"proguard-rules.pro")
}
release {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(getDefaultProguardFile(
"proguard-android-optimize.txt"),
"proguard-rules.pro")
}
}
// 输出类型
android.applicationVariants.all {
// 编译类型
val buildType = this.buildType.name
val date = SimpleDateFormat("yyyyMMddHHmmss").format(Date())
outputs.all {
// 判断是否是输出 apk 类型
if (this is com.android.build.gradle
.internal.api.ApkVariantOutputImpl) {
this.outputFileName = "szyj" +
"_${android.defaultConfig.versionName}_${date}_${buildType}.apk"
}
}
}
//**********打包设置结束**********
3. sourceSets配置
3.1. build.gradle
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
3.2. build.gradle.kts
//jniLibs目录指向libs目录
sourceSets {
getByName("main") {
jniLibs.srcDirs("libs")
}
}
4. Kotlin Build.gradle脚本编写(build.gradle.kts)
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
}
android {
compileSdkVersion(29)
defaultConfig {
applicationId = "com.xxx.xxxxx"
minSdkVersion(21)
targetSdkVersion(29)
versionCode = 27
versionName = "2.2.0"
resConfigs("zh")
ndk {
abiFilters += listOf("armeabi-v7a","arm64-v8a")
}
}
//开启dataBinding
buildFeatures {
dataBinding = true
}
//图片已压缩 指定aapt不做图片压缩 因为可能会反而增加图片大小
aaptOptions {
// cruncherEnabled = false
}
//关闭lint检查
lintOptions {
disable("ResourceType")
// abortOnError = false
}
//jniLibs目录指向libs目录
sourceSets {
getByName("main") {
jniLibs.srcDirs("libs")
}
}
//优化transformClassDexBuilderForDebug的时间
dexOptions {
preDexLibraries = true
maxProcessCount = 8
}
//禁止生成依赖元数据 不上play用不到
dependenciesInfo {
includeInApk = false
}
//jdk1.8支持
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
//签名配置
signingConfigs {
getByName("debug") {
storeFile = file("../xxx.jks")
storePassword = "xxx"
keyAlias = "xxx"
keyPassword = "xxx"
}
}
buildTypes {
getByName("debug") {
//签名
signingConfig = signingConfigs.getByName("debug")
//git提交次数 作为测试包版本后缀
buildConfigField("int", "GIT_COMMIT_COUNT", getGitCommitCount())
}
register("alpha") {
//继承debug配置
initWith(getByName("debug"))
//混淆
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
//ZipAlignEnabled优化
isZipAlignEnabled = true
//移除无用的resource文件
isShrinkResources = true
}
getByName("release") {
//继承alpha配置
initWith(getByName("alpha"))
//关闭debug
debuggable(false)
}
}
//release打包时自定义apk名字、输出路径
android.applicationVariants.all {
outputs.all {
if (this is com.android.build.gradle.internal.api.ApkVariantOutputImpl) {
this.outputFileName = "xxxx.apk"
}
}
}
}
//获取git提交次数
fun getGitCommitCount(): String {
val os = org.apache.commons.io.output.ByteArrayOutputStream()
project.exec {
commandLine = "git rev-list --count HEAD".split(" ")
standardOutput = os
}
return String(os.toByteArray()).trim()
}
//依赖库
apply(from = "depends.gradle")