当前位置:   article > 正文

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

android 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"
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

根据 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"
            )
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

配置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")


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

对应的lib文件夹目录结构

在这里插入图片描述不同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
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

关于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"
                }
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/244471
推荐阅读
相关标签
  

闽ICP备14008679号