Build Scan not working for Gradle in Android

My issue was solved after writing the following in build.gradle the top-level file for the project

plugins {
    id 'com.gradle.build-scan' version '1.16'
}

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

The issue is with the following line in your build.gradle:

apply plugin: com.gradle.build-scan

You need to update as

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

Another thing you need to pay attention is ALWAYS put the com.gradle.build-scan plugin as the very first one, like this:

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

Otherwise, you would see this:

WARNING: The build scan plugin was applied after other plugins. The captured data is more comprehensive when the build scan plugin is applied first.

Please see https://gradle.com/scans/help/plugin-late-apply for how to resolve this problem.

Let me know if this works.


Edit in May 2020:

Now in May 2020, the way to do it is, add the following to the app gradle file:

plugins {
  id "com.gradle.build-scan" version "3.3"
}

buildScan {
    termsOfServiceUrl = 'https://gradle.com/terms-of-service'     
    termsOfServiceAgree = 'yes'                                   

    publishAlways()                                               
}

You should place these outside of the buildscript { .. }, e.g., right after it, rather than inside it.

Then the build scan, e.g., with ./gradlew build --scan, should work.

Edit in October 2019:

The current (October 2019) way to get the build scan working is to add the following to the app gradle file

plugins {
    id 'com.gradle.build-scan' version '2.4.2'                     
}

buildScan {
    termsOfServiceUrl = 'https://gradle.com/terms-of-service'     
    termsOfServiceAgree = 'yes'                                   

    publishAlways()                                               
}

and it would probably be best to get it directly from the gradle build scan user manual in the future for when it changes again.