Android Studio:Gradle 与 v4 支持库同步失败

Android Studio: Gradle Sync fail with v4 support library

我一直在关注 Android 片段教程,该教程要求我按照支持库设置文档 here.

设置我的项目以使用 v4 库

这是我遵循的步骤。

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

完成这三个步骤后,我收到了这两个错误:

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.

根据 Android SDK 管理器,我确实安装了 Android 支持存储库。

我将继续调查此问题,因为片段似乎是开发功能强大的 Android 移动应用程序的宝贵工具。帮助解决此问题将不胜感激。

正如 Sufian 在下面的评论中所要求的,这是我的 build.gradle 文件的内容。

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

这是我的 Android 独立 SDK 管理器的 screenshot,显示 Android 支持存储库已安装。

您的项目中有两个 build.gradle 文件,一个根级别,一个用于您的应用程序。您还将在另一个 build.gradle 中看到依赖项部分。您需要将 compile "com.android.support:support-v4:24.1.1" 放在那里。

您正在 <PROJECT_ROOT>\build.gradle 中添加 compile "com.android.support:support-v4:24.1.1" 而不是您应该在 <PROJECT_ROOT>\app\build.gradle

中添加上述依赖项