gradle类强制转换异常:无法强制转换为'java.lang.iterable'

时间:2014-08-11 19:49:45

标签: java android gradle android-gradle build.gradle

我有一个android项目,我正在编写build.gradle文件。我有一些依赖库项目,它们将驻留在根文件夹中。所以文件夹的结构将类似于以下内容,

RootFolder:测试项目,一些图书馆项目,项目3和项目4.

我的build.gradle看起来像这样,

buildscript {

    repositories {
        mavenCentral()
    }

    dependencies {

        classpath 'com.android.tools.build:gradle:0.12.+'       

    }

}

apply plugin: 'android'

dependencies {

    compile 'com.android.support:support-v4:18.0.+'
    compile project(':test-library')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

}

ext {

    project.sourcecompatibility = "1.6"
    project.targetcompatibility = "1.6"

}

// ========================================
// Android Plugin
// ========================================

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"


    defaultConfig {
        versionName "1.1"
        minSdkVersion 8
        targetSdkVersion 19

    }

    lintOptions {

        abortOnError false

    }

    sourceSets {

        main {

            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = {'src'}
            resources.srcDirs = {'src'}
            aidl.srcDirs = {'src'}
            renderscript.srcDirs = {'src'}
            res.srcDirs = {'res'}
            assets.srcDir = {'assets'}

        }       

    }


    buildTypes {

        debug {
            debuggable = true
            zipAlign = false
        }


        release {           
            debuggable = false
            zipAlign = true
        }

    }
}

我收到以下错误,

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/someuser/Desktop/Gradle Testing Projects/TestProject_GradleTest/build.gradle' line: 75

* What went wrong:
A problem occurred evaluating root project 'TestProject_GradleTest'.
> Cannot cast object 'build_23rtd5r6a61l25hprb7evd7kna$_run_closure3_closure6_closure8_closure9@22a92801' with class 'build_23rtd5r6a61l25hprb7evd7kna$_run_closure3_closure6_closure8_closure9' to class 'java.lang.Iterable'

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

我不明白为什么我会遇到classcastexception,我该如何解决? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

没关系,问题是sourceset

 manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = {'src'}
            resources.srcDirs = {'src'}
            aidl.srcDirs = {'src'}
            renderscript.srcDirs = {'src'}
            res.srcDirs = {'res'}
            assets.srcDir = {'assets'}

应如下所示,

  manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDir = ['assets']