gradle.build 错误 (28,0):Gradle 未找到 DSL 方法:'apply()'

gradle.build Error (28,0): Gradle DSL method not found: 'apply()'

我试图包括这个 library to my current project following the method suggested here 这导致我的项目崩溃,我在库的 build.gradlle 文件中收到以下错误 这是项目的 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:1.2.3'

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

subprojects {
    ext.global_compileSdkVersion = 22
    ext.global_buildToolsVersion = "23.0.0 rc2"
}

build.gradle 应用模块:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'com.android.application'

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

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

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"
}

subprojects {
    ext.global_compileSdkVersion = 22
    ext.global_buildToolsVersion = "23.0.0 rc2"
}

和我要添加的库的 build.gradle:

    /*
*    Copyright (C) 2015 Haruki Hasegawa
*
*    Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*/

apply plugin: 'com.android.library'

ext {
    // required for 'android-maven-publish.gradle'
    mavenPublishDestDir = new File(rootDir, "repository").absolutePath
    mavenPublishDataFile = file('./library-data.properties').absolutePath
    mavenPublishSigningSetting = file('../signing/library-maven-publish-signing.properties').absolutePath
}

// Common configurations

android {

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 22

        def dataProps = new Properties()
        dataProps.load(project.file(project.mavenPublishDataFile).newDataInputStream())

        versionCode dataProps.VERSION_CODE.toInteger()
        versionName dataProps.VERSION_NAME
    }
    buildTypes {
        release {
            minifyEnabled false
            shrinkResources false
            consumerProguardFiles 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            shrinkResources false
            consumerProguardFiles 'proguard-rules.pro'
        }
    }
} apply plugin: 'java'

dependencies {
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
}

tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked"
}

和settings.gradle

include ':app', ':advancedRecyclerView'

我做错了什么?

首先,你不能在顶层文件中使用apply插件。
然后删除这一行

apply plugin: 'com.android.application'

在您的项目中使用此库的最佳方式是将其作为 Maven 依赖项导入,而无需在本地克隆代码。

compile 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.7.4'

你应该有这样的结构:

root
  app
    build.gradle
  build.gradle
  settings.gradle

settings.gradle

include ':app'

在顶级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:1.3.0'

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

allprojects {
    repositories {
        jcenter()
    }
}

app/build.gradle

apply plugin: 'com.android.application'

android {
    defaultConfig {
      //....
    }

    //....

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    //Add the library
    compile 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.7.4'
}

这不是 OP 的答案,但可能对遇到错误的其他人有用

Gradle DSL method not found: 'apply()'

在我的案例中,问题出在 build.gradle 文件的编码上。我在文本编辑器中编辑了该文件,它被保存为 UTF-8-BOM,Gradle 显然不接受。所以解决方案是简单地以 ANSI 或 UTF-8(无 BOM)编码重新保存文件。