dyllandry dyllandry - 3 months ago 190
Android Question

Android Studio: Gradle Sync fail with v4 support library

I have been following the Android fragments tutorial which requests that I set up my project to work with the v4 library by following the Support Library Setup document here.

Here are the steps I followed.


1) Make sure you have downloaded the Android Support Repository using
the SDK Manager.

2) Open the build.gradle file for your application.

3) Add the support library to the dependencies section. For example,
to add the v4 support library, add the following lines:

dependencies {
...
compile "com.android.support:support-v4:24.1.1"
}



After following the three steps I receive these two errors:

Gradle project sync failed...


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

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


According to the Android SDK Manager, I do have the Android Support Repository installed.

I am going to continue investigating this issue as fragments appear to be a valuable tool to develop powerful Android mobile apps. Help approaching this issue would be appreciated.

As Sufian requested in the comments below, here are the contents of my build.gradle file.

// 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.1.3'
compile "com.android.support:support-v4:24.1.1"

// 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
}


Here is a screenshot of my Android Standalone SDK Manager showing that the Android Support Repository is installed.

Answer

There are two build.gradle files in your project. One rootlevel, and one for your application. You will also see a dependies section in the other build.gradle. You need to place the compile "com.android.support:support-v4:24.1.1" in there.

Comments