10

I'm going through Google's documentation on "Add Google Play Services to Your Project" in Android Studio: https://developer.android.com/google/play-services/setup.html

I'm using that documentation to modify the build.gradle file of a freshly created Android project. In Step 2 (Add Google Play Services to Your Project), it states:

Add this line:

apply plugin: 'android'

Under Dependencies, add this:

compile 'com.google.android.gms:play-services:5.0.77'

It also says to update that version after updating Google Play Services, which is now at 18 according to Android SDK Manager.

Here is my entire build.gradle file at the top-level (parent of this file is the root folder).

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'android'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        compile 'com.google.android.gms:play-services:18'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Upon saving, it prompts for a Sync. I Sync it, but get:

Build script error, unsupported Gradle DSL method found: 'compile()'!

Error:(10, 0) Possible causes could be:
              - you are using a Gradle version where the method is absent
              - you didn't apply Gradle plugin which provides the method
              - or there is a mistake in a build script

I'm using Android Studio 0.8.2. I didn't install Gradle, just using the plugin that came with Android Studio.

It's interesting to note that the build.gradle file generated when I made this new project says:

//NOTE: Do not place your application dependencies here

But Google's documentation says (which conflicts with the above):

Note: Android Studio projects contain a top-level build.gradle file and a build.gradle
      file for each module. Be sure to edit the file for your application module.

What's wrong with my build.gradle file (or environment)?

Gerard
  • 638
  • 4
  • 8
  • 19

5 Answers5

17

The Google documentation you quoted is correct, and doesn't conflict. There's more than one build.gradle file. Instead of putting dependencies in the top-level one as you have, put them in the build file that's in your module's directory.

Also, don't put an apply plugin: 'android' statement in that top-level build file; it will cause an error.

You can also add dependencies through the Project Structure UI, which does the right thing.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Scott, you of all people, should know that your answer is not an answer, it does not explain how to fix the error. Moving the entire `buildScript` part to submodule won't help, will it? This is perfectly valid comment, but not an answer. – JBaruch Jul 14 '14 at 16:58
  • I don't understand your comment. The edits were made in the wrong build file. They went in the top-level one instead of the module-level one. I don't say anything about moving a `buildScript` block. Also, if that `apply plugin` statement is mistakenly left in the top-level build file, it will cause very strange errors. – Scott Barta Jul 14 '14 at 17:19
  • Ah, I see, so you didn't spot the problem. The problem not the build file (it works perfectly fine in top level file as well), the problem is wrong closure. You can't use the `compile` configuration in `buildscript` context, because `buildscript` stage can have only one configuration - the `classpath` and it has nothing to do with your project dependencies. It configures the build script itself (i.e. adds jar with `android` plugin to compile the gradle stuff). So, it's just a wrong answer :) – JBaruch Jul 14 '14 at 17:29
  • 1
    Scott Barta, I put the dependency in the module build.gradle file, it worked. Now, on that Google Play Services setup page, did I still have to put the "apply plugin: 'android'" line into my module's build.gradle file? – Gerard Jul 17 '14 at 04:35
8

Do not add dependencies in your project by editing its most 'external' build.gradle (YourProject/build.gradle). Edit the one that is under the 'app' module instead (YourProject/app/build.gradle).

There, by the way, you will find the declaration of one dependency, such as:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

This block will be just below android { ... } configuration block. In my case, I am just adding leeloo dependencies, so it became:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'net.smartam.leeloo:oauth2-client:0.1'
    compile 'net.smartam.leeloo:oauth2-common:0.1'
}

Then sync your project and dependencies will be downloaded. Hope it helps!

1

the compile-time dependencies should reside in the dependencies block under allprojects, not under buildscript:

apply plugin: 'android'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

allprojects {
    repositories {
        jcenter()
    }
    dependencies {
        compile 'com.google.android.gms:play-services:18'
    }
}

This should work fine.

JBaruch
  • 22,610
  • 5
  • 62
  • 90
  • Don't put dependencies in the allprojects block, please, unless you really intend it to apply to every module in the project, which you almost certainly don't. Put it in individual module build files. – Scott Barta Jul 14 '14 at 16:27
  • Not sure if this play-services jar makes sense for all the modules as a common dependency. If it's not, you're right, it should go to sub-modules. – JBaruch Jul 14 '14 at 16:56
1

Think of “Gradle DSL method” as a Java method. So in Gradle, methods can be distinguished by either {} or “.”. So

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

is the same as

dependencies.compile fileTree(dir: 'libs', include: ['*.jar'])

where both “dependencies” and “compile” are methods.

So you are including a method somewhere in your build.gradle file that is not supported by your program. For example, make your dependencies this:

dependencies {
    nothing 'this.does.nothing.build:gradle:0.7.+'
}

Which is the same as writing:

dependencies.nothing 'this.does.nothing.build:gradle:0.7.+'

And you will see an error saying “unsupported Gradle DSL method found: ‘nothing()’!” Obviously "nothing" is not a real method. I just made it up.

So one of your "compile" methods inside your build.gradle is wrong.

Gene
  • 10,819
  • 1
  • 66
  • 58
0

When I faced this problem I used android developer UI to import dependencies as follows:-

1 Go to View ---> Open Module Settings

  1. Select Dependency tab. Click + to add a dependency and select Library dependency. Choose the downloaded library here.
E_net4
  • 27,810
  • 13
  • 101
  • 139
Nicks
  • 16,030
  • 8
  • 58
  • 65