【发布时间】:2015-06-08 00:38:17
【问题描述】:
测试工件选择器实际上是做什么的?出于某种原因,当我将其设置为单元测试时,Android Studio (AS) 能够识别并运行我的 Junit 4 测试。当我将其设置为 Android Instrumentation Tests 时,JUnit 4 从我的类路径中消失,并且 AS 将代码标记为不正确(无法解析符号 junit 以用于导入 org.junit.Test)。
有没有办法告诉 AS 将选择的类文件视为我不想在模拟器中运行的 JUnit 测试?这完全在他们的雷达上吗?我不得不将这个开关从 JUnit 测试转移到集成测试,这似乎很疯狂,而且它似乎会影响整个项目,而不仅仅是一种构建/发布风格,或者只是我项目的一个子目录。
这是我的 build.gradle,使用 gradle 2.2.1,Android Studio 1.3 (AI-141.1972460) 的金丝雀通道,Android SDK Tools 24.3.0
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
// Manifest version information!
def versionMajor = 0
def versionMinor = 1
def versionPatch = 0
def versionBuild = 1 // bump for dogfood builds, public betas, etc.apply plugin: 'android'
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
def buildTime = new Date().format("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"))
android {
dexOptions {
preDexLibraries = false
}
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
multiDexEnabled true
testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"
versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
buildConfigField "String", "GIT_SHA", "\"${gitSha}\""
buildConfigField "String", "BUILD_TIME", "\"${buildTime}\""
}
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion 14
}
}
buildTypes {
debug {
applicationIdSuffix '.dev'
versionNameSuffix '-dev'
}
release {
}
}
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'LICENSE.txt'
}
sourceSets {
instrumentTest.setRoot('src/test')
androidTest.setRoot('src/test')
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.google.android.gms:play-services-base:7.5.0'
compile 'com.google.android.gms:play-services-plus:7.5.0'
compile 'com.google.android.gms:play-services-identity:7.5.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:support-annotations:22.2.0'
compile 'com.squareup.dagger:dagger:1.2.2'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
compile 'com.squareup.okhttp:okhttp:2.3.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.jakewharton.timber:timber:3.0.1'
compile 'io.reactivex:rxjava:1.0.9'
compile 'io.reactivex:rxandroid:0.24.0'
compile 'io.reactivex:rxandroid-framework:0.24.0'
compile 'com.github.matthewyork:ColoursLibrary:1.0.0@aar'
compile 'com.nispok:snackbar:2.10.7'
compile 'com.github.frankiesardo:icepick:2.3.6'
provided 'com.github.frankiesardo:icepick-processor:2.3.6'
compile 'nl.qbusict:cupboard:2.1.1'
compile 'org.lucasr.twowayview:core:1.0.0-SNAPSHOT@aar'
compile 'org.lucasr.twowayview:layouts:1.0.0-SNAPSHOT@aar'
compile 'it.neokree:MaterialNavigationDrawer:1.3.3'
testCompile 'junit:junit:4.12'
}
【问题讨论】:
-
对于那些寻找此设置在 AS 2.0 中的位置的人,请参阅stackoverflow.com/questions/35708263/…
标签: java unit-testing android-studio junit android-gradle-plugin