Configuring project: 'app' and gradle sync fail

A problem occurred configuring project ‘:app’. Found on Line 20

groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.file.DefaultFilePropertyFactory$DefaultDirectoryVar.toPath() is applicable for argument types: () values:

Line 20= def relativeRootDir = output.packageApplication.outputDirectory.toPath()
Line 21= .relativize(rootDir.toPath()).toFile()
variant.packageApplicationProvider.get().outputDirectory = new File(“$relativeRootDir/build”, newApkName)

Could someone give me an example of how to fix this problem? Thank you.