Unsupported Gradle DSL method found: 'compile()'!

前端 未结 5 1788
南方客
南方客 2020-12-14 23:29

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

相关标签:
5条回答
  • 2020-12-14 23:58

    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.

    0 讨论(0)
  • 2020-12-15 00:06

    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.

    0 讨论(0)
  • 2020-12-15 00:11

    I am not sure why a down vote is given to previously mentioned solutions(Scott Barta & JBaruch). If we go through solution and comments mentioned above, it works completely.

    However, 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.
    0 讨论(0)
  • 2020-12-15 00:17

    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!

    0 讨论(0)
  • 2020-12-15 00:24

    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.

    0 讨论(0)
提交回复
热议问题