I am working with Android Studio 3.1 Canary 6 and I am getting this error when I build my project. I search this error and many users get this with different reasons, but I did
maybe this can help, as this worked for me, just delete the taskhistory.bin file in
(projectFolder)/.gradle/4.1/taskHistory
and clean and rebuild the project.
best of luck!