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

Android Studio中如何在build.gradle中获取项目名称?

在Android Studio的build.gradle中获取项目名称的方法

直接通过project.name就能获取当前项目的名称,这是Gradle提供的内置属性,无需额外配置。

你给出的代码里存在一处语法错误——variant -应该修正为variant ->,调整格式后完整的可运行示例如下:

buildTypes {
    release {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                String projectName = project.name
                // 这里可以添加使用projectName的逻辑,比如打印项目名或用于输出文件命名
                println("当前项目名称:$projectName")
            }
        }
    }
}

如果只是单纯获取项目名称,甚至不需要嵌套在applicationVariants的遍历逻辑里,在build.gradle的任意脚本块中直接调用project.name都能拿到对应的值。

内容的提问来源于stack exchange,提问作者M. AsadUllah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:55:13