Could not find method leftShift() for arguments after updating studio 3.4

AndroidGradleGroovyAndroid Gradle-Pluginbuild.gradle

Android Problem Overview


After updating studio 3.4 and Gradle version to 5.1.1 I got the error on my task as Could not find method leftShift()

My task:

task incrementBetaVersion << {
    println("Incrementing Beta Version Number...")
    incrementVersionNumber('BetaVersionNumber')
    println("Incrementing Beta Version Number...")
    incrementVersionName('BetaVersionName')
}

I got the error for the left shift operator << in the line.

How to resolve this error?

Android Solutions


Solution 1 - Android

To solve this error, change << with doLast like this.

task incrementBetaVersion  {
    doLast {
        println("Incrementing Beta Version Number...")
        incrementVersionNumber('BetaVersionNumber')
        println("Incrementing Beta Version Number...")
        incrementVersionName('BetaVersionName')
    }
}

Left shift operator represent's doLast { }.

> << was deprecated in Gradle 4.x and removed in Gradle 5.0

From Docs:

<< for task definitions no longer works. In other words, you can not use the syntax

task myTask << { …​ }.

Use the Task.doLast() method instead, like this:

task myTask {
    doLast {
        ...
        ...
    }
}

More info here: https://discuss.gradle.org/t/could-not-find-method-leftshift-for-arguments-on-task-of-type-org-gradle-api-defaulttask/30614

https://docs.gradle.org/current/userguide/upgrading_version_4.html#changes_5.0

Solution 2 - Android

Just Remove "<<" from Task and add your code in doLast{}

For solution of Could not find method leftShift() for arguments on task of type org.gradle.api.DefaultTask

task incrementBetaVersion << {
   // your code
}

to

task incrementBetaVersion {
   doLast {
      // your code
    }
}

Reference https://discuss.gradle.org/t/could-not-find-method-leftshift-for-arguments-on-task-of-type-org-gradle-api-defaulttask/30614/2

><< (LeftShift()) operator is deprecated in 4.x Gradle and Removed in 5.x Gradle Version.

Solution 3 - Android

It happening because of the Left Shift operator has been replaced by doLast { }.

<< has deprecated in 4.x and removed in 5.0 version

Now you will have to change the code:

task incrementBetaVersion << {
    println("Incrementing Beta Version Number...")
    incrementVersionNumber('BetaVersionNumber')
    println("Incrementing Beta Version Number...")
    incrementVersionName('BetaVersionName')
}

to

task incrementBetaVersion  {
    doLast {
        println("Incrementing Beta Version Number...")
        incrementVersionNumber('BetaVersionNumber')
        println("Incrementing Beta Version Number...")
        incrementVersionName('BetaVersionName')
    }
}

Solution 4 - Android

To solve this error is pretty simple.

Just replace << with doLast

See below updated code, today I fix in my cordova Android project.

task cdvPrintProps {
   doLast {
       //your code
   }
}

Solution 5 - Android

I had this error in a Kotlin project that is using MockMaker to mock non final classes.

The solution is to change the old syntax to this new one:

task createTestResources {
    description = "Allows Mocking non-final classes and data classes in a Kotlin project"
    doLast {
        def mockMakerFile = new File("$projectDir/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker")
        if (System.env.MOCK_MAKER != null) {
            logger.info("Using MockMaker ${System.env.MOCK_MAKER}")
            mockMakerFile.parentFile.mkdirs()
            mockMakerFile.createNewFile()
            mockMakerFile.write(System.env.MOCK_MAKER)
        } else {
            logger.info("Using default MockMaker")
        }
    }
}

Note that a few things have changed, like including the doLast block, and removing the << from the task signature. It works for me now. Hope it does for you too :-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBhuvanesh BSView Question on Stackoverflow
Solution 1 - AndroidBhuvanesh BSView Answer on Stackoverflow
Solution 2 - AndroidNikunj ParadvaView Answer on Stackoverflow
Solution 3 - AndroidnaveenosView Answer on Stackoverflow
Solution 4 - AndroidMuddasir23View Answer on Stackoverflow
Solution 5 - AndroidvoghDevView Answer on Stackoverflow