André Fratelli
André Fratelli

Reputation: 6068

Could not get unknown property 'runTasks' for task

I'm trying to set a task that increments that keeps track of build numbers on a file and increments it when the project is built. Here's the task:

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)
}

This is what I'm getting:

Error:Could not get unknown property 'runTasks' for task ':hul:incrementBuildNumber' of type org.gradle.api.DefaultTask.

The full gradle file looks like this:

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'
}

I've seen other ways of doing it that work, but I'd rather use tasks for this. Is there a way to fix it?

Upvotes: 0

Views: 4777

Answers (1)

Michael Easter
Michael Easter

Reputation: 24468

I'm guessing that the code was borrowed from another context that defined runTasks. I'm not able to reproduce your full build, but as a tiny example, consider the following:

apply plugin: 'java'

task go() << {
    // crucial line:
    def runTasks = gradle.taskGraph.allTasks.collect { it.name }

    if ('assemble' in runTasks || 'assembleRelease' in runTasks) {
        println "HELLO"
    }    
}

In this case, the command-line gradle assemble go will print HELLO. So, if you add the crucial line to incrementBuildNumber, I believe it will work.

Upvotes: 1

Related Questions