Anuj TBE Anuj TBE - 25 days ago 30
Android Question

Could not found method compile() in gradle Android Studio

I'm new to Android Studio and android development and developing my first application.

I want to use

CarView
and
RecyclerView
to display data. According to many tutorials on it, I need to add following dependencies

compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'


This is how my
build.gradle
is

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

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

allprojects {
repositories {
jcenter()
}
}

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

dependencies {
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}


But on Sync gradle, it gives error as

Error:Could not find method compile() for arguments [com.android.support:cardview-v7:21.0.+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Please install the Android Support Repository from the Android SDK Manager.


I tried many solutions from Google search but couldn't found any working.

Answer

Android Studio creates a project with two build.gradle files.

This section of code

dependencies {
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
}

Goes into app/build.gradle of the project. You also want to update the 21 version number to the SDK that you're compiling with.

After that, you also have to install the Android Support Repository from the Android SDK Manager.