Kotlin Gradle DSL统一构建脚本

使用Kotlin编写settings.gradle.kts

将工程根目录的settings.gradle改为settings.gradle.kts并修改相应的属性

include(":module_weather")
include(":module_voice_setting")
include(":module_setting")
include(":module_joke")
include(":module_developer")
include(":module_constellation")
include(":module_app_manager")
include(":module_map")
include(":lib_voice")
include(":lib_network")
include(":lib_base")
include(":app")
rootProject.name = "AiVoiceApp"
rootProject.buildFileName="build.gradle.kts"

使用Kotlin编写project-build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    val kotlin_version = "1.3.71"
    val gradle_version = "4.0.1"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:${KotlinConstants.gradle_version}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${KotlinConstants.kotlin_version}")
    }
}
allprojects {
    repositories {
        google()
        jcenter()
    }
}

tasks {
    val clean by registering(Delete::class) {
        delete(buildDir)
    }
}

使用Kotlin编写app-build.gradle

//引用插件
plugins {
    id("com.android.application")
    kotlin("android")
    kotlin("android.extensions")
    kotlin("kapt")
}

//Android属性
android {
    compileSdkVersion(AppConfig.compileSdkVersion)
    buildToolsVersion(AppConfig.buildToolsVersion)
    defaultConfig {
        applicationId = AppConfig.applicationId
        minSdkVersion(AppConfig.minSdkVersion)
        targetSdkVersion(AppConfig.targetSdkVersion)
        versionCode = AppConfig.versionCode
        versionName = AppConfig.versionName
    }
    //ARouter
    kapt {
        arguments {
            arg("AROUTER_MODULE_NAME", project.name)
        }
    }

    //签名类型
    signingConfigs {
        register("release") {
            //别名
            keyAlias = "key0"
            //别名密码
            keyPassword = "123456"
            //路径
            storeFile = file("/src/main/jks/aivoice.jks")
            //密码
            storePassword = "123456"
        }
    }
    
    //编译类型
    buildTypes {
        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(project(":lib_base"))
    if (!ModuleConfig.isApp) {
        implementation(project(":module_app_manager"))
        implementation(project(":module_constellation"))
        implementation(project(":module_developer"))
        implementation(project(":module_joke"))
        implementation(project(":module_map"))
        implementation(project(":module_setting"))
        implementation(project(":module_voice_setting"))
        implementation(project(":module_weather"))
    }
    //运行时注解
    kapt(DependenciesConfig.AROUTER_COMPILER)
}

kotlin-gradle-dsl构建脚本

在根目录右键新建buildSrc目录。目录结构与文件如下所示
Kotlin Gradle DSL统一构建脚本_第1张图片
KotlinConstants.kt


//全局常量
object KotlinConstants {

    //Gradle 版本
    const val gradle_version = "3.6.2"

    //Kotlin 版本
    const val kotlin_version = "1.3.71"
}

//应用配置
object AppConfig {

    //依赖版本
    const val compileSdkVersion = 28

    //编译工具版本
    const val buildToolsVersion = "28.0.3"

    //包名
    const val applicationId = "com.imooc.aivoiceapp"

    //最小支持SDK
    const val minSdkVersion = 21

    //当前基于SDK
    const val targetSdkVersion = 28

    //版本编码
    const val versionCode = 1

    //版本名称
    const val versionName = "1.0"
}

//依赖配置
object DependenciesConfig {

    //Kotlin基础库
    const val STD_LIB = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${KotlinConstants.kotlin_version}"

    //Android标准库
    const val APP_COMPAT = "androidx.appcompat:appcompat:1.1.0"

    //Kotlin核心库
    const val KTX_CORE = "androidx.core:core-ktx:1.2.0"

    //EventBus
    const val EVENT_BUS = "org.greenrobot:eventbus:3.2.0"

    //ARouter
    const val AROUTER = "com.alibaba:arouter-api:1.5.0"
    const val AROUTER_COMPILER = "com.alibaba:arouter-compiler:1.2.2"

    //RecyclerView
    const val RECYCLERVIEW = "androidx.recyclerview:recyclerview:1.2.0-alpha01"

    //Permissions
    const val AND_PERMISSIONS = "com.yanzhenjie:permission:2.0.3"

    //Retrofit
    const val RETROFIT = "com.squareup.retrofit2:retrofit:2.8.1"
    const val RETROFIT_GSON = "com.squareup.retrofit2:converter-gson:2.8.1"

    //ViewPager
    const val VIEWPAGER = "com.zhy:magic-viewpager:1.0.1"
    const val MATERIAL = "com.google.android.material:material:1.0.0"

    //Lottie
    const val LOTTIE = "com.airbnb.android:lottie:3.4.0"

    //刷新
    const val REFRESH_KERNEL = "com.scwang.smart:refresh-layout-kernel:2.0.1"
    const val REFRESH_HEADER = "com.scwang.smart:refresh-header-classics:2.0.1"
    const val REFRESH_FOOT = "com.scwang.smart:refresh-footer-classics:2.0.1"

    //图表
    const val CHART = "com.github.PhilJay:MPAndroidChart:v3.1.0"
}

//Module配置
object ModuleConfig {

    //Module是否App
    var isApp = true

    //包名
    const val MODULE_APP_MANAGER = "com.imooc.module_app_manager"
    const val MODULE_CONSTELLATION = "com.imooc.module_constellation"
    const val MODULE_DEVELOPER = "com.imooc.module_developer"
    const val MODULE_JOKE = "com.imooc.module_joke"
    const val MODULE_MAP = "com.imooc.module_map"
    const val MODULE_SETTING = "com.imooc.module_setting"
    const val MODULE_VOICE_SETTING = "com.imooc.module_voice_setting"
    const val MODULE_WEATHER = "com.imooc.module_weather"
}

build.gradle.kts

apply {
    plugin("kotlin")
}
buildscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath(kotlin("gradle-plugin", "1.3.71"))
    }
}
dependencies {
    implementation(gradleKotlinDsl())
    implementation(kotlin("stdlib", "1.3.71"))
}
repositories {
    gradlePluginPortal()
}

settings.gradle.kts

rootProject.buildFileName = "build.gradle.kts"

你可能感兴趣的:(android)