You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在多个build.gradle.kts文件间共享Android配置片段?

Android Gradle Kotlin DSL 共享配置方案(signingConfigs、buildTypes)

1. 创建共享配置脚本

在项目根目录新建shared-config.gradle.kts,封装要复用的配置逻辑:

// 配置共享的签名信息
fun configureSigningConfigs(android: com.android.build.api.dsl.CommonExtension<*, *, *, *, *, *>) {
    android.signingConfigs {
        create("release") {
            keyAlias = "release"
            keyPassword = "XXXXXXXXXXX"
            // 用根项目路径定位密钥文件,避免模块间路径差异
            storeFile = rootProject.file("keystore/release.jks")
            storePassword = "YYYYYYYYYYYYYYYYY"
        }
    }
}

// 配置共享的构建类型
fun configureBuildTypes(android: com.android.build.api.dsl.CommonExtension<*, *, *, *, *, *>) {
    android.buildTypes {
        debug {
            applicationIdSuffix = ".dbg"
            versionNameSuffix = "-debug"
            ndk {
                abiFilters += listOf("x86_64", "arm64-v8a")
            }
        }
        release {
            applicationIdSuffix = ""
            versionNameSuffix = ""
            isMinifyEnabled = false
            isDebuggable = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
            signingConfig = android.signingConfigs.getByName("release")
            ndk {
                abiFilters += listOf("x86_64", "arm64-v8a")
            }
            externalNativeBuild {
                cmake {
                    arguments += "-DCMAKE_BUILD_TYPE=RelWithDebInfo"
                }
            }
        }
    }
}

2. 在应用模块中引入共享配置

修改应用模块的build.gradle.kts:

plugins {
    id("com.android.application")
    kotlin("android")
}

// 引入根目录的共享配置脚本
apply(from = rootProject.file("shared-config.gradle.kts"))

android {
    namespace = "com.company.product"
    compileSdk = 33
    ndkVersion = "25.1.8937393"
    buildToolsVersion = "30.0.3"
    
    // 调用共享签名配置函数
    configureSigningConfigs(this)
    
    defaultConfig {
        applicationId = "com.company.product"
        minSdk = 29
        targetSdk = 33
        versionCode = computedVersionCode
        versionName = getVersionName(computedVersionCode)
    }
    
    // 调用共享构建类型配置函数
    configureBuildTypes(this)
}

dependencies {
    implementation("androidx.activity:activity-compose:1.7.2")
    implementation("androidx.compose.ui:ui:1.4.1")
}

3. 在库模块中引入共享配置

修改库模块的build.gradle.kts:

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

// 引入共享配置脚本
apply(from = rootProject.file("shared-config.gradle.kts"))

android {
    namespace = "com.company.product.library"
    compileSdk = 33
    ndkVersion = "25.1.8937393"
    buildToolsVersion = "30.0.3"
    
    configureSigningConfigs(this)
    
    defaultConfig {
        minSdk = 29
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"
    }
    
    configureBuildTypes(this)
}

dependencies {
    // 库模块依赖
}

关键注意事项

  • 路径统一:共享脚本中使用rootProject.file()定位文件,确保所有模块都能正确找到密钥文件
  • 敏感信息优化:实际项目中不要硬编码密钥密码,建议从local.properties或环境变量读取,示例:
    val localProps = Properties()
    localProps.load(rootProject.file("local.properties").inputStream())
    keyAlias = localProps.getProperty("signing.keyAlias") ?: "release"
    keyPassword = localProps.getProperty("signing.keyPassword") ?: ""
    
  • 兼容性:CommonExtension适配Android Gradle Plugin 7.0+,同时支持应用和库模块的配置扩展

内容的提问来源于stack exchange,提问作者Francois

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 14:36:04