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 truev2SigningEnabled truestoreFile file('./keystore/insour_szyj.keystore')storePassword 'insour_szyj'keyAlias 'insour_szyj'keyPassword 'insour_szyj'}debug {v1SigningEnabled truev2SigningEnabled truestoreFile file('./keystore/insour_szyj.keystore')storePassword 'insour_szyj'keyAlias 'insour_szyj'keyPassword 'insour_szyj'}}buildTypes {debug {
// minifyEnabled false//混淆minifyEnabled trueshrinkResources true// 不显示LogbuildConfigField "boolean", "LOG_DEBUG", "false"proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'aaptOptions.cruncherEnabled = falseaaptOptions.useNewCruncher = falseapkName = "szyj.apk"signingConfig signingConfigs.release}release {
// minifyEnabled false//混淆minifyEnabled trueshrinkResources true// 不显示LogbuildConfigField "boolean", "LOG_DEBUG", "false"proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'aaptOptions.cruncherEnabled = falseaaptOptions.useNewCruncher = falseapkName = "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 =trueenableV2Signing =trueenableV3Signing =trueenableV4Signing =truestoreFile = file("./keystore/insour_szyj.keystore")storePassword = "insour_szyj"keyAlias = "insour_szyj"keyPassword = "insour_szyj"}register("release") {enableV1Signing =trueenableV2Signing =trueenableV3Signing =trueenableV4Signing =truestoreFile = file("./keystore/insour_szyj.keystore")storePassword = "insour_szyj"keyAlias = "insour_szyj"keyPassword = "insour_szyj"}}buildTypes {debug {isMinifyEnabled = falseisShrinkResources = falseproguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),"proguard-rules.pro")}release {isMinifyEnabled = falseisShrinkResources = falseproguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),"proguard-rules.pro")}}// 输出类型android.applicationVariants.all {// 编译类型val buildType = this.buildType.nameval 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. BuildConfig不生成
新建了一个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下就好了
//开启dataBindingbuildFeatures {dataBinding = truebuildConfig=true}
5. 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 = 27versionName = "2.2.0"resConfigs("zh")ndk {abiFilters += listOf("armeabi-v7a","arm64-v8a")}}//开启dataBindingbuildFeatures {dataBinding = truebuildConfig=true}//图片已压缩 指定aapt不做图片压缩 因为可能会反而增加图片大小aaptOptions {
// cruncherEnabled = false}//关闭lint检查lintOptions {disable("ResourceType")
// abortOnError = false}//jniLibs目录指向libs目录sourceSets {getByName("main") {jniLibs.srcDirs("libs")}}//优化transformClassDexBuilderForDebug的时间dexOptions {preDexLibraries = truemaxProcessCount = 8}//禁止生成依赖元数据 不上play用不到dependenciesInfo {includeInApk = false}//jdk1.8支持compileOptions {sourceCompatibility = JavaVersion.VERSION_1_8targetCompatibility = 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 = trueproguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")//ZipAlignEnabled优化isZipAlignEnabled = true//移除无用的resource文件isShrinkResources = true}getByName("release") {//继承alpha配置initWith(getByName("alpha"))//关闭debugdebuggable(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")