Search code examples
androidandroid-studioandroid-gradle-pluginbuild.gradlezxing

Could not find method compile()


I want to add a lib in android studio but it is not working. Here is a screenshot

enter image description here

I also tried to add a dependency in the gradle.build but that also does not work. Maybe it is because I am behind a proxy ?

enter image description here


Solution

  • You are using the wrong build.gradle file.

    You can't use compile in the top-level file.
    Use the module/build.gradle.

    <PROJECT_ROOT>\app\build.gradle is specific for app module.

    <PROJECT_ROOT>\build.gradle is a "Top-level build file" where you can add configuration options common to all sub-projects/modules.

    Also you should include the libraries in the dependencies block, not inside the buildscript block.

    Top level file example:

    buildscript {
        repositories {
            jcenter()
        }
    
        dependencies {
           classpath 'com.android.tools.build:gradle:2.3.0'
           classpath 'com.google.gms:google-services:3.0.0'
        }
    }
    

    In the app\build.gradle you define only the properties for the module:

    apply plugin: 'com.android.application'
    
    
    android {
        compileSdkVersion ...
        buildToolsVersion ...
    }
    
    dependencies {
        //..... HERE !
    }