gpt4 book ai didi

gradle - 无法获得任务的未知属性 'runTasks'

转载 作者:行者123 更新时间:2023-12-03 05:13:20 28 4
gpt4 key购买 nike

我正在尝试设置一个递增任务,以跟踪文件上的内部版本号,并在构建项目时对其进行递增。这是任务:

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.



完整的gradle文件如下所示:
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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com