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

Android Gradle自定义staging Build Type无法添加对应依赖的问题

问题原因与解决办法

在Kotlin DSL(build.gradle.kts)中,自定义Build Type不会像Groovy DSL那样自动生成xxxImplementation这类依赖配置的扩展函数——AGP在Kotlin环境下采用静态类型检查,无法动态创建未预先定义的引用,这就是你遇到Unresolved reference: stagingImplementation的核心原因。

下面提供两种直接可用的解决方式:

方式一:直接引用对应的配置项

通过configurations对象获取stagingImplementation配置,然后添加依赖:

dependencies {
    debugImplementation("com.squareup.leakcanary:leakcanary-android:2.9.1")
    releaseImplementation("com.example:release-library:1.0")
    // 手动获取stagingImplementation配置并添加依赖
    configurations["stagingImplementation"].dependencies.add(
        create("com.example:qa-library:1.0")
    )
}

方式二:使用变体匹配语法

利用依赖的变体匹配属性,指定仅在staging构建类型下生效:

dependencies {
    debugImplementation("com.squareup.leakcanary:leakcanary-android:2.9.1")
    releaseImplementation("com.example:release-library:1.0")
    // 通过isStaging属性限定依赖仅作用于staging构建类型
    implementation("com.example:qa-library:1.0") {
        isStaging = true
    }
}

两种方式都能解决你的问题,方式二更简洁易读,推荐使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 14:47:08