作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试设置一个递增任务,以跟踪文件上的内部版本号,并在构建项目时对其进行递增。这是任务:
task incrementBuildNumber(group: 'versioning', description: 'Increments the project build number') {
Properties versionProps = getVersionProperties(versionPropsName)
def versionBuild = getVersionProperty(versionProps, 'VERSION_PATCH') + 1
versionProps['VERSION_PATCH'] = versionBuild.toString()
if ('assemble' in runTasks || 'assembleRelease' in runTasks) {
// Also increments VERSION_CODE when assembling
versionProps['VERSION_CODE'] = getVersionProperty(versionProps, 'VERSION_CODE') + 1
}
setVersionProperties(versionPropsName, versionProps)
}
Error:Could not get unknown property 'runTasks' for task ':hul:incrementBuildNumber' of type org.gradle.api.DefaultTask.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'net.sf.proguard:proguard-gradle:5.2.1'
}
}
apply plugin: 'com.android.library'
def getVersionProperties(versionPropsName) {
File versionPropsFile = new File(versionPropsName);
if (!versionPropsFile.canRead()) {
throw new GradleException("Could not read $versionPropsName")
}
Properties properties = new Properties();
versionPropsFile.withInputStream {
properties.load(new FileInputStream(versionPropsFile))
}
return properties;
}
def setVersionProperties(versionPropsName, versionProps) {
File versionPropsFile = new File(versionPropsName);
if (!versionPropsFile.canWrite()) {
throw new GradleException("Could not write $versionPropsName")
}
versionProps.store(versionPropsFile.newWriter(), null)
}
def getVersionProperty(versionProps, versionKey) {
String versionValue = versionProps.get(versionKey, "")
if (!versionValue.isInteger()) {
throw new GradleException("Improperly configured properties file: expected integer value for $versionKey")
}
return versionValue.toInteger()
}
android {
compileSdkVersion 21
buildToolsVersion "23.0.2"
def versionPropsName = 'version.properties'
//noinspection GroovyAssignabilityCheck
task incrementBuildNumber(group: 'versioning', description: 'Increments the project build number') {
Properties versionProps = getVersionProperties(versionPropsName)
def versionBuild = getVersionProperty(versionProps, 'VERSION_PATCH') + 1
versionProps['VERSION_PATCH'] = versionBuild.toString()
if ('assemble' in runTasks || 'assembleRelease' in runTasks) {
// Also increments VERSION_CODE when assembling
versionProps['VERSION_CODE'] = getVersionProperty(versionProps, 'VERSION_CODE') + 1
}
setVersionProperties(versionPropsName, versionProps)
}
Properties versionProps = getVersionProperties(versionPropsName)
def versionBuild = getVersionProperty(versionProps, 'VERSION_PATCH')
def versionMinor = getVersionProperty(versionProps, 'VERSION_MINOR')
def versionMajor = getVersionProperty(versionProps, 'VERSION_MAJOR')
def internalVersion = getVersionProperty(versionProps, 'VERSION_CODE')
defaultConfig {
minSdkVersion 21
targetSdkVersion 21
//noinspection GroovyAssignabilityCheck
versionCode internalVersion
//noinspection GroovyAssignabilityCheck
versionName versionMajor + "." + versionMinor + "." + versionBuild
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
jni.srcDirs = [] //disable automatic ndk-build call
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
shrinkResources false
jniDebuggable true
}
}
def ndkDir = plugins.getPlugin('com.android.library').sdkHandler.getNdkFolder()
def srcDir = file('jni/src').absolutePath
//noinspection GroovyAssignabilityCheck
task buildRelease(type: Exec, description: 'Build for release') {
commandLine "$ndkDir/ndk-build",
'-C', srcDir,
'-j', Runtime.runtime.availableProcessors(),
'hul'
}
//noinspection GroovyAssignabilityCheck
task buildDebug(type: Exec, description: 'Build for debug') {
commandLine "$ndkDir/ndk-build",
'-C', srcDir,
'-j', Runtime.runtime.availableProcessors(),
'hul',
'NDK_DEBUG=1'
}
//noinspection GroovyAssignabilityCheck
task cleanNative(type: Exec, description: 'Clean JNI object files') {
commandLine "$ndkDir/ndk-build",
'-C', file('jni').absolutePath,
'clean'
}
//noinspection GroovyAssignabilityCheck
task cleanBinaryFolders(type: Delete, description: 'Clean binary folders') {
delete 'obj'
}
buildRelease.dependsOn 'incrementBuildNumber'
buildDebug.dependsOn 'incrementBuildNumber'
cleanNative.dependsOn 'incrementBuildNumber'
clean.dependsOn 'cleanNative'
clean.dependsOn 'cleanBinaryFolders'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
最佳答案
我猜想代码是从另一个定义了runTasks
的上下文中借用的。我无法复制您的完整版本,但作为一个小示例,请考虑以下内容:
apply plugin: 'java'
task go() << {
// crucial line:
def runTasks = gradle.taskGraph.allTasks.collect { it.name }
if ('assemble' in runTasks || 'assembleRelease' in runTasks) {
println "HELLO"
}
}
gradle assemble go
将输出HELLO。因此,如果您将关键行添加到
incrementBuildNumber
,我相信它会起作用。
关于gradle - 无法获得任务的未知属性 'runTasks',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39310131/
我正在尝试设置一个递增任务,以跟踪文件上的内部版本号,并在构建项目时对其进行递增。这是任务: task incrementBuildNumber(group: 'versioning', descri
这个问题之前似乎已经在 SO 上得到了回答,但是尽管查看了其他解决方案,我仍然无法弄清楚为什么会出现错误: function call missing argument list; use '&Run
我是一名优秀的程序员,十分优秀!