Android flavor 的build.gradle.kts的新写法

Groovy to KTS

Groovy to KTS 迁移指南

Android Gradle 插件 4.0 支持在 Gradle 构建配置中使用 Kotlin 脚本 (KTS),用于替代 Groovy(过去在 Gradle 配置文件中使用的编程语言)。

将来,KTS 会比 Groovy 更适合用于编写 Gradle 脚本,因为采用 Kotlin 编写的代码可读性更高,并且 Kotlin 提供了更好的编译时检查和 IDE 支持。

这里记录下,在修改 flavor时候,遇到的几个坑

配置flavor

    flavorDimensions.add("platform")
    productFlavors {
        create("zim200") {
            dimension = "platform"

        }
        create("ysm8") {
            dimension = "platform"
        }
    }

根据 buildtypes配置签名

buildTypes {
        debug {
            isDebuggable = true
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            )
            signingConfig = null

            productFlavors.getByName("ysm8") {
                signingConfig = signingConfigs.getByName("ysm8")
            }

            productFlavors.getByName("zim200") {
                signingConfig = signingConfigs.getByName("zim200")
            }

        }
        release {
            initWith(buildTypes.getByName("debug"))
            isMinifyEnabled = true
            isDebuggable = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            )
        }
    }

配置lib文件夹

dependencies {
    add(
        "zim200Implementation",
        fileTree(mapOf("dir" to "libszim", "include" to listOf("*.jar", "*.aar")))
    )
    add(
        "ysm8Implementation",
        fileTree(mapOf("dir" to "libsysm8", "include" to listOf("*.jar", "*.aar")))
    )
    implementation(project(":TtsTool"))
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit-ktx:1.1.3")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")


}

对应的lib文件夹目录结构

Android flavor 的build.gradle.kts的新写法_第1张图片不同flavor的应用,加载不同的lib,然后某些代码文件,资源也可以使用对应文件夹下的内容替代

其他

配置密钥

signingConfigs {
        create("zim200") {
            storeFile = file("../signature/platform.keystore")
            storePassword = "xxx"
            keyAlias = "xxx"
            keyPassword = "xxx"
        }
        create("ysm8") {
            storeFile = file("../signature/ysm8.jks")
            storePassword = "xxx"
            keyAlias = "xxx"
            keyPassword = "xxx"
//            v1SigningEnabled = true
//            v1SigningEnabled = true
            isV1SigningEnabled = true
        }
    }

关于abi拆分和apk命名

//按abi拆分包
    splits {
        abi {
            isEnable = true
            reset()
            include("armeabi-v7a", "arm64-v8a")//支持的ABIs
            isUniversalApk = true //要不要一个全量ABIs的包
        }
    }

    val abiCodes = mapOf("armeabi-v7a" to 1, "arm64-v8a" to 2, "x86" to 3, "x86_64" to 4)
    android.applicationVariants.all {
        val buildType = this.buildType.name
        val flavorName = this.flavorName
        val variant = this
        outputs.all {
            val name =
                this.filters.find { it.filterType == com.android.build.api.variant.FilterConfiguration.FilterType.ABI.name }?.identifier
            val baseAbiCode = abiCodes[name]
            if (baseAbiCode != null) {
                //写入cpu架构信息
                variant.buildConfigField("String", "CUP_ABI", "\"${name}\"")
            }
            if (this is com.android.build.gradle.internal.api.ApkVariantOutputImpl) {
                //修改apk名称
                if (buildType == "release") {
                    this.outputFileName =
                        "apkname_${flavorName}_${name}_${buildType}_v${variant.versionName}.apk"
                } else if (buildType == "debug") {
                    this.outputFileName =
                        "apkname_${flavorName}_${name}_${buildType}_v${variant.versionName}.apk"
                }
            }
        }
    }

你可能感兴趣的:(android)