我在我的 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
}
Run Code Online (Sandbox Code Playgroud)
还有这种味道:
android {
...
productFlavors {
internal {
def lastRemoteVersion = remoteGitVersion()
buildConfigField "String", "LAST_REMOTE_VERSION", "\"" + lastRemoteVersion + "\""
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
现在,由于 gradle 声明性质,每次构建项目时都会执行 remoteGitVersion 函数,无论构建风格是内部的还是其他什么都无关紧要。因此,github API 调用配额已被消耗,过了一会儿,我收到了一条很好的禁止消息。
我怎样才能避免这种情况?是否可以仅在所选风味正确时才执行该功能?
从这里参考:
在 Android/Gradle 中如何定义仅在构建特定 buildType/buildVariant/productFlavor (v0.10+) 时运行的任务
回顾一下:
task fetchGitSha << {
android.productFlavors.internal {
def lastRemoteVersion = remoteGitVersion()
buildConfigField "String", "LAST_REMOTE_VERSION", "\"" + lastRemoteVersion + "\""
}
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下,你可以使用assembleInternalDebughook into 。
tasks.whenTaskAdded { task ->
if(task.name == 'assembleInternalDebug') {
task.dependsOn fetchGitSha
}
}
Run Code Online (Sandbox Code Playgroud)
productFlavors {
internal {
# no buildConfigField here
}
}
Run Code Online (Sandbox Code Playgroud)
希望有帮助。
| 归档时间: |
|
| 查看次数: |
1880 次 |
| 最近记录: |