59

I want to use gradle plugin having this syntax

 plugins {
  id "id" version "version"
}

but i have the error of

only build script and other plugins script blocks are allowed before plugins 

I moved it to the bloc buildscript but still not working.

How can i apply this kind of plugin ?

This is the plugin that I want to include in my project

gradle git properties plugin

and here is the output of my gradle version

    Gradle 4.3.1
------------------------------------------------------------

Build time:   2017-11-08 08:59:45 UTC
Revision:     e4f4804807ef7c2829da51877861ff06e07e006d

Groovy:       2.4.12
Toumi
  • 2,925
  • 4
  • 37
  • 31

4 Answers4

95

Whenever you write a build.gradle script and use the new plugins script block, you need to put it as first block in the file. The only exceptions from this rule are other plugins blocks or the special buildScript block, which always must go first.

As an example, this script is fine:

plugins {
    // ...
}

dependencies {
    // ...
}

This one is also fine:

buildScript {
    // ...
}

plugins {
    // ...
}

repositories {
    // ...
}

But this one is invalid:

repositories {
     // ...
}

plugins {
    // ...
}
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • 2
    How first scenario one even possible?! In root `build.gradle`, `dependencies {` is inside of `buildscript {`, and you're saying to solve the error, we should set the `plugins { after `buildScript {` and before `dependencies {` – Dr.jacky Jul 04 '19 at 13:07
  • 2
    The `dependencies` block inside a `buildscript` block is not the same as the regular `dependencies` block. Furthermore, this is completely independent from whether the project is a root project or not. – Lukas Körfer Jul 04 '19 at 13:13
  • Nailed it on the head ye did! Great suggestion! – Droid Chris Feb 04 '21 at 19:48
  • buildScript { // ... } plugins { // ... } repositories { // ... } Worked for me. – serif Apr 04 '22 at 17:00
6

I noticed a plugin line before the android block that said apply plugin: 'com.android.application'. Since the firebase configuration ask me for the 'com.google.gms.google-services' plugin I just add:

apply plugin: 'com.google.gms.google-services'
Andres R
  • 193
  • 2
  • 7
2

Google firebase documentation is kind of messed up, they are asking to add the following code

plugins {
  id 'com.google.gms.google-services'
}

but it should really be following at the very top of the file (more like include)

apply plugin 'com.google.gms.google-services'

Make sure this is the first line in the file.

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
-5

I solved this just by deleting.

task clean(type: Delete) { delete rootProject.buildDir }

Tesla
  • 9
  • 1