Is it possible to declare a variable in Gradle usable in Java?

后端 未结 9 1476
春和景丽
春和景丽 2020-11-22 09:11

Is it possible to declare a variable in Gradle usable in Java ? Basically I would like to declare some vars in the build.gradle and then getting it (obviously) at build time

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 09:44

    How can you insert String result of function into buildConfigField

    Here's an example of build date in human-readable format set:

    def getDate() {
        return new SimpleDateFormat("dd MMMM yyyy", new Locale("ru")).format(new Date())
    }
    
    def buildDate = getDate()
    
    defaultConfig {
        buildConfigField "String", "BUILD_DATE", "\"$buildDate\""
    }
    

提交回复
热议问题