Gradle找不到buildToolsVersion()方法,参数为[23.0.2],但实际上Build Tools版本存在

14

使用Gradle构建我的Android应用程序时,我遇到了一个奇怪的错误。

运行gradle build时,我收到以下错误:

Could not find method buildToolsVersion() for arguments [23.0.2] on root project 'latest'.

不过,我检查了我的Android SDK文件夹,看看这个版本是否确实遗漏了或者没有。换句话说,执行ls $ANDROID_HOME/build-tools/的结果如下:

19.1.0  20.0.0  21.1.2  22.0.1  23.0.1  23.0.2  23.0.3

如您所见,23.0.2 存在。问题似乎出在哪里?

这是我的根 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.0.0'

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

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

ext {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
}
subprojects { subproject ->
    afterEvaluate{
        if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

这是build.gradle文件。

apply plugin: 'com.android.application'
apply from: 'copyLibs.gradle'
apply plugin: 'eclipse'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
        applicationId "com.marco.myapp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 35
        versionName "1.2.0.15"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    // avoid Travis failures
    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:recyclerview-v7:22.0.0'
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':libraries:openpgp-api-lib')
    compile 'org.eclipse.jgit:org.eclipse.jgit:3.7.0.201502260915-r'
    compile 'com.jcraft:jsch:0.1.52'
    compile 'org.apache.commons:commons-io:1.3.2'
    compile 'com.jayway.android.robotium:robotium-solo:5.3.1'
    compile 'com.melnykov:floatingactionbutton:1.2.0'
}
tasks.findAll {    
    it.name.startsWith 'assemble'
}.each {
    it.dependsOn copyDependenciesIntoLibs
}
2个回答

25

你看到这个信息是因为在根项目中声明的变量缺少赋值。

ext {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
}

应该是:

ext {
    compileSdkVersion = 23
    buildToolsVersion = '23.0.2'
}

哦,天啊,正是这些小事情耗费时间。谢谢! - Forrest

-2

它在Android工具文档中有记录。

如果您有许多Android模块,您可能希望避免在所有模块中手动设置相同的值。因为您可能有android和android-library项目的混合,所以无法通过子项目闭包应用这些插件。但是,您可以在根项目上设置该值,然后从模块引用它。

似乎不能在subprojects块内使用它。

解决方法是在顶层文件中声明:

ext {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
}

然后在所有模块中使用:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
}

但这就是我刚刚做的... 在根目录下的build.gradle文件中,我放置了buildToolsVersion“23.0.2”,并在模块中放置了buildToolsVersion rootProject.ext.buildToolsVersion。 仍然无法工作... - Marco Couto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接