repositories {
maven {url 'https://maven.aliyun.com/repository/public/'}
mavenCentral()
}
repositories {
maven {
setUrl("https://maven.aliyun.com/repository/public/")
}
mavenCentral()
}
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation(libs.glide)
版本号在gradle文件夹下 libs.versions.toml文件中添加
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" }
我们在新建一个项目然后直接进行Build apk,可以生成一个app_debug.apk的apk文件,那么文件是怎么产生的呢?
//**********打包设置开始**********
//自定义生成的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"
// }
// }
// }
//**********打包设置结束**********
//**********打包设置开始**********
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"
}
}
}
//**********打包设置结束**********
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
//jniLibs目录指向libs目录
sourceSets {
getByName("main") {
jniLibs.srcDirs("libs")
}
}
新建了一个demo,其依赖的AGP版本是8.0.0。但是在运行过程中报了一个错误就是找不到BuildConfig。
重新build了下代码,然后找编译后的代码,发现确实没有生成BuildConfig。清缓存,重启AS都没有用。之前代码相比,也就是AGP的版本升级了下,那猜测是不是跟AGP8.0.0的版本有关,于是在BuildType中手动添加了个buildConfigField,想以此方式强制生成下BuildConfig。
运行报错
Build Type 'debug' contains custom BuildConfig fields, but the feature is disabled.
提示BuildConfig 处于禁用状态…看来是新版本的AGP默认禁用了生成BuildConfig。BuildFeatures源码看到了一个配置buildConfig的注释如下。
果然如此,默认是禁用状态。在buildFeatures配置中把buildConfig值手动设为true,重新build下就好了
//开启dataBinding
buildFeatures {
dataBinding = true
buildConfig=true
}
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
buildConfig=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")