Kotlin DSL 编写 gradle 笔记

厉钊
2023-12-01

Kotlin DSL 编写 gradle 笔记

环境

  1. Android Studio Arctic Fox | 2020.3.1 Patch 2
  2. gradle 7.0.2
  3. jdk 11

1. 根目录 setting.gradle.kts

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    //这里的仓库地址 指的是module中 implementation 第三方库所对应的地址
    repositories {
        google()
        mavenCentral()
        maven {
            setUrl("https://jitpack.io")
        }
        maven {
            name = "amazonaws"
            setUrl("https://s3.amazonaws.com/repo.commonsware.com")
        }

        maven {
            setUrl("https://maven.aliyun.com/repository/public")
        }
    }
}
rootProject.name = "KotlinDSLDemo"
include(":app", ":nativelib")

2. 根目录 build.gradle.kts

buildscript {

    //plugin的仓库地址
    repositories {
        maven {
            setUrl("https://maven.aliyun.com/repository/public")
        }
        google()
        mavenCentral()
        maven {
            setUrl("https://jitpack.io")
        }
        maven {
            setUrl("https://s3.amazonaws.com/repo.commonsware.com")
        }

    }

    val navVersion = "2.3.5"
    val kotlinVersion = "1.5.21"
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.2")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navVersion")
        classpath("com.github.kezong:fat-aar:1.3.6")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

tasks.create("clean", Delete::class) {
    delete(rootProject.buildDir)
}

3. app Module 的 build.gradle.kts

plugins {
    id("com.android.application")
    id("kotlin-android")
    id("kotlin-kapt")
    id("kotlin-parcelize")
    //用于生成javadoc
    id("org.jetbrains.dokka") version "1.5.30"
}

android {
    compileSdk = 30

    bundle {
        language {
            enableSplit = false
        }

        density {
            enableSplit = false
        }
    }

    defaultConfig {
        val flavorDimension = "default"
        applicationId = "com.sudworth.kotlindsldemo"
        minSdk = 23
        targetSdk = 30

        //这两个属性只有主Module才有, 子Module没有
        versionCode = 1
        versionName = "1.0"
        flavorDimensions.add(flavorDimension)

        productFlavors {
            create("dev") {
                //包名后面增加.dev
                //com.sudworth.kotlindsldemo.dev
                applicationIdSuffix = ".dev"
                dimension = flavorDimension
                //替换AndroidManifest里面的 ${channel} 占位符
                manifestPlaceholders["channel"] = "dev"
            }

            create("alpha") {
                dimension = flavorDimension
                applicationIdSuffix = ".alpha"
                manifestPlaceholders["channel"] = "alpha"
            }
        }

        ndk {
            abiFilters.addAll(mutableSetOf("arm64-v8a", "armeabi-v7a"))
        }

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        create("release") {
            storeFile = File("${rootDir}/app/app_key.jks")
            storePassword = "xxx111111"
            keyAlias = "xxx"
            keyPassword = "xxx111111"
            enableV1Signing = true
            enableV2Signing = true
            enableV3Signing = true
            enableV4Signing = true
        }
    }

    buildTypes {

        //buildTypes里面的 release和debug是预设的,不可以create
        getByName("debug") {
            buildConfigField("boolean", "BUILD_DEBUG", "true")
            isMinifyEnabled = false
            //这里的release 取的是 signingConfigs里面的
            signingConfig = signingConfigs.getByName("release")
        }

        getByName("release") {
            buildConfigField("boolean", "BUILD_DEBUG", "false")
            isMinifyEnabled = true
            isShrinkResources = true
            //Changing the value of isZipAlignEnabled no longer has any effect
            //这个设置已经无效了。
            isZipAlignEnabled = true
            signingConfig = signingConfigs.getByName("release")
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    lint {
        abortOnError = false
        checkReleaseBuilds = false
    }

    buildFeatures {
        viewBinding = true
        dataBinding = true
    }
}

dependencies {

    //自定义DokkaTask 的配置
    tasks.creating(org.jetbrains.dokka.gradle.DokkaTask::class) {
        outputDirectory.set(File("${buildDir}\\dokka"))
        dependencies {
            plugins("org.jetbrains.dokka:kotlin-as-java-plugin:1.5.30")
        }
    }
    //直接使用默认配置
//    dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.5.30")


    implementation("androidx.core:core-ktx:1.6.0")
    implementation("androidx.appcompat:appcompat:1.3.0")
    implementation("com.google.android.material:material:1.3.0")
    implementation("androidx.constraintlayout:constraintlayout:2.0.4")

    kapt("com.google.dagger:dagger-compiler:2.37")

    implementation(project(":nativelib"))

    testImplementation("junit:junit:4.+")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}

4. nativelib 的build.gradle.kts

plugins {
    id("com.android.library")
    id("kotlin-android")
}


android {

    compileSdk = 30
    buildToolsVersion = "30.0.3"
    ndkVersion = "21.4.7075529"

    defaultConfig {
        minSdk = 23
        targetSdk = 30
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
        externalNativeBuild {
            cmake {
                cppFlags("")
            }
        }
    }

    buildTypes {

        getByName("release") {
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            )
        }
    }
    externalNativeBuild {
        cmake {
            path("src/main/cpp/CMakeLists.txt")
            version = "3.10.2"
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.3.2")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.3.0")
    testImplementation("junit:junit:4.+")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}

 类似资料: