Android Studio Error: build scan was not published

The accepted answer in this other question works. You have to test the existence of buildScan task.

if (hasProperty('buildScan')) {
    buildScan {
        termsOfServiceUrl = 'https://gradle.com/terms-of-service'
        termsOfServiceAgree = 'yes'
    }
}

Just remove the following code from build.gradle file and Sync your Gradle again. Everything will be resolved.

apply plugin: 'com.gradle.build-scan'

buildScan {
    licenseAgreementUrl = 'https://gradle.com/terms-of-service'
    licenseAgree = 'yes'
}