Android build tools 1.1.0, unit test folder?

落花浮王杯 提交于 2019-12-10 03:46:10

问题


I recently installed the latest tools from google to my android project:

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

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.xxx"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 200
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        debug {
            ...
        }
        release {
            ...
        }
    }
    buildTypes {
        release {
            ...
        }
        debug {
            ...
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    // ---- Tests with robolectric
    testCompile 'com.google.guava:guava:14.0.1'
    testCompile 'junit:junit:4.+'
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'org.mockito:mockito-all:2.0.2-beta'

    // ---- Tests with Espresso
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile ('com.android.support.test:testing-support-lib:0.1') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
    androidTestCompile('junit:junit-dep:4.10') {
        exclude module: 'hamcrest-core'
    }
}

Where before that I used to use com.github.jcandksolutions.gradle:android-unit-test:2.1.1 to run my robolectric tests in the jvm. As google says for their new build tools: "New source folders recognized as unit tests: src/test/java, src/testDebug/java, src/testMyFlavor/java etc." But as you can see below, my test folder isn't recognised as a source folder. It worked with com.github.jcandksolutions.gradle:android-unit-test:2.1.1, but no more with the new build tools:

What I'm missing here? Thank you


回答1:


I found the solution which is to switch between Test Artifacts in the left corner of the IDE. On this screen only "Android Instrumentation Tests" is available because I downgraded my android tools but with tools 1.1.0+ you should see different types of test to get the IDE recognize them as source folders.




回答2:


I had a similar problem the other day, except that I was able to run Robolectric tests ok in Android Studio, but didn't work from the command line. What worked for me is the following.

1) Run ./gradlew clean assembleDebug test (instead of just clean test)

(now, it would find source from main packages, but I would get this problem instead)

2) Added this to the build.gradle file: android.sourceSets.test.java.srcDirs += "build/generated/source/r/debug"




回答3:


Just follow http://tools.android.com/tech-docs/unit-testing-support to enable the experimental unit test and switch test artifact to unit tests. then your unit test folder will be recognised.

when you have more issues perhaps this will help http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html




回答4:


If it's any use I set up a boiler plate project allowing the use of Unit tests and Espresso tests by the use of switching build variants. You won't need the use of any third party plugins with this.

https://github.com/hitherejoe/Android-Boilerplate



来源:https://stackoverflow.com/questions/28661618/android-build-tools-1-1-0-unit-test-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!