app-build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
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'
}
}
dependencies {
//核心库
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.30"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
全局统一管理配置
object KotlinConstants {
//根目录版本
const val gradle_version = "4.1.2"
const val kotlin_version = "1.4.21"//Kotlin 版本
}
object AppConfig{
//依赖版本
const val compileSdkVersion=30//依赖版本
const val buildToolsVersion="30.0.3"//编译工具版本
const val applicationId = "com.example.aivoikeapp"//包名
const val minSdkVersion =21//最小支持sdk
const val targetSdkVersion =30 //当前基于Sdk版本
//版本编码
const val versionCode = 1
//版本名称
const val versionName = "1.0"
}
签名配置
点击Build,在弹出的菜单中选择Generate Signed Bundle /APK...,接下来会弹出如下界面👇
选择APK.png
选择APK,点击Next
build.gradle.kts 配置
//引用插件
plugins {
id ("com.android.application")
kotlin("android")
kotlin("android.extensions")
}
//Android属性
android {
compileSdkVersion(AppConfig.compileSdkVersion)
buildToolsVersion(AppConfig.buildToolsVersion)
defaultConfig {
applicationId = AppConfig.applicationId
minSdkVersion(AppConfig.minSdkVersion)
targetSdkVersion(AppConfig.targetSdkVersion)
versionCode = AppConfig.versionCode
versionName = AppConfig.versionName
}
//签名配置
signingConfigs{
register("release"){
//别名
keyAlias = "imooc"
//别名密码
keyPassword = "123456"
//路径
storeFile = file("src/main/jks/aivoice.jks")
//签名密码
storePassword = "123456"
}
}
//编译类型
buildTypes {
getByName("debug") {
}
getByName("release")
{
isMinifyEnabled = false
signingConfig = signingConfigs.getByName("release")
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
//输出类型
android.applicationVariants.all {
//编译类型
val buildType = this.buildType.name
outputs.all {
//输出APK
if (this is com.android.build.gradle.internal.api.ApkVariantOutputImpl){
//版本 类型
if(buildType=="release") {
this.outputFileName="AI_V${defaultConfig.versionName}_$buildType.apk"
}
}
}
}
//依赖操作
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
//依赖
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation (Dependencies.APP_COMPAT)
implementation (Dependencies.Ktx_CORE)
implementation (Dependencies.STD_LID)
implementation ("com.google.android.material:material:1.2.1")
implementation ("androidx.constraintlayout:constraintlayout:2.0.4")
testImplementation ("junit:junit:4.+")
androidTestImplementation("androidx.test.ext:junit:1.1.2")
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}