【发布时间】:2015-02-09 11:46:07
【问题描述】:
我在我的 Android 项目 build.gradle 中声明了这个函数:
def remoteGitVertsion() {
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parse(new URL("https://api.github.com/repos/github/android/commits"))
assert object instanceof List
object[0].sha
}
还有这种味道:
android {
...
productFlavors {
internal {
def lastRemoteVersion = remoteGitVersion()
buildConfigField "String", "LAST_REMOTE_VERSION", "\"" + lastRemoteVersion + "\""
}
...
}
...
}
现在,由于 gradle 声明性质,每次构建项目时都会执行 remoteGitVersion 函数,无论构建风格是内部的还是其他的都无关紧要。因此,github API 调用配额被消耗,过了一会儿,我收到了一条很好的禁止消息。
我怎样才能避免这种情况? Is it possible to execute the function only when the selected flavor is the right one?
【问题讨论】:
-
这是一个很酷的主意。希望我能给你一个答案