Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

android studio - Unsupported Gradle DSL method found: 'compile()'!

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...