Gradle 'Build Script error' occurs when I

2020-06-11 14:02发布

问题:

I'm working with Android Studio and in my dependencies for my application I attempting to add a testCompile dependency as listed here: http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html When I sync my file I get the error: I don't understand what is going on, my gradle build file in my root folder is set to classpath 'com.android.tools.build:gradle:0.12.+' and that's the most recent version. Why doesn't it recognize testCompile? I don't want to deploy test dependencies to production... Any helps would be appreciated.

EDIT: Here is the project build file

buildscript {
    repositories {
        jcenter()


    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

and here is the src build file:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 17
    buildToolsVersion "20.0.0"
    defaultConfig {
        applicationId "com.example.edu.myApp"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug{
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/scribe-1.3.5.jar')
    compile files('libs/json_simple-1.1.jar')
    compile 'com.google.android.gms:play-services:5.0.77'
    // Can't be higher than 19 if we want to support smaller android versions
    compile 'com.android.support:appcompat-v7:19.+'
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile 'com.android.support:support-v4:19.+'
    // This Mockito includes all dependancies (great for us)
    testCompile "org.mockito:mockito-core:1.9.+"
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    testCompile 'org.objenesis:objenesis:1.2'
}

回答1:

You should use androidTestCompile, not testCompile. If this is due to modifying the dependency scope via the Project Structure dialog, then there's a bug where it uses the wrong statement to set up the dependency. I've filed https://code.google.com/p/android/issues/detail?id=74771 for this.



回答2:

I stumbled upon this post a year later. I was having this problem because I inherited an older project that was using an out-of-date Gradle build tools setting in the Gradle build file. I fixed this problem by updating Gradle build tools, which you need to do apparently by incrementing the version number in the Gradle build file:

dependencies {
    classpath 'com.android.tools.build:gradle:1.X.X'
}

where X.X is the latest gradle build tools version number. Right now I'm using Android Studio v1.2 and I've got my build tools version number set to:

dependencies {
    classpath 'com.android.tools.build:gradle:1.2.2'
}

I came across this problem when I was trying to implement unit tests, which are now built-in to Android Studio apparently.

Hope this helps someone with the same problem.



回答3:

I got this "method not found 'testcompile()'" error also. The problem was I had testCompile 'junit:junit:4.12' in my project build.gradle file and not in by app build.gradle file. How could I have known it was in the wrong file? The project file only states "NOTE: Do not place your application dependencies here; they belong in the individual module build.gradle files". Such a simple answer no wonder I couldn't find it.



回答4:

I have a library project. I believed the Android documentation and put the testCompile statement in my top level build.gradle file. Turns out it actually had to go in my module build.gradle file.