我尝试运行单元测试,但出现错误
错误:程序包android.support.test不存在
我的成绩档案中有
testImplementation 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
但是我仍然会出错
Gradle
import java.util.regex.Pattern
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply from: 'versionCode.gradle'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionName "1.0." + VERSION_CODE
versionCode Integer.parseInt(VERSION_CODE)
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
res.srcDirs =
[...]
java.srcDirs = ['src/main/java', 'src/main/java/UserStories']
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://jitpack.io" }
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'jniLibs', include: ['*.jar', '.so'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'co.lujun:androidtagview:1.1.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
compile 'jp.wasabeef:glide-transformations:3.0.1'
compile "com.daimajia.swipelayout:library:1.2.0@aar"
implementation 'com.tapadoo.android:alerter:2.0.6'
compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
testImplementation "org.mockito:mockito-core:2.+"
testImplementation 'junit:junit:4.12'
compile fileTree(dir: 'libs', include: ['*.jar'])
testImplementation "org.robolectric:robolectric:3.8"
compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
transitive = true;
}
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
implementation 'com.google.dagger:dagger-android:2.11'
implementation 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
答案 0 :(得分:0)
确保您的gradle文件是这样的:
android {
...
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
...
}
...
}
dependencies {
...
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit:junit:4.12'
...
}
现在可以使用吗?
答案 1 :(得分:0)
在示例2的gradle文件中,用实现替换androidTestImplementation似乎可以解决此问题。检查并更新一次。
答案 2 :(得分:0)
像波纹管一样使用:
dependencies.gradle
:
ext {
versions = [
// For project configuration
java : JavaVersion.VERSION_1_8,
compileSdk : 28,
targetSdk : 28,
minSdk : 21,
buildTools : '28.0.2',
dokka : '0.9.16',
// For app
kotlin : '1.2.61',
coroutine : '0.24.0',
appCompat : '1.0.0-beta01',
recyclerview : '1.0.0-beta01',
constraintLayout : '2.0.0-alpha1',
cardview : '1.0.0-beta01',
browser : '1.0.0-beta01',
androidKtx : '1.0.0-beta01',
dagger : '2.16',
firebaseCloudMessage : '17.1.0',
firebaseAnalytics : '16.0.3',
playServices : '15.0.1',
exoPlayer : '2.8.2',
flexbox : '1.0.0',
material : '1.0.0-beta01',
room : '2.0.0-beta01',
lifecycle : '2.0.0-beta01',
rxKotlin : '2.2.0',
rxAndroid : '2.0.2',
moshi : '1.6.0',
okhttp : '3.11.0',
timber : '4.7.1',
threeTenABP : '1.1.0',
rxBinding : '2.1.1',
fresco : '1.10.0',
kotshi : '1.0.4',
frescoImageViewer : '0.5.1',
markwon : '1.1.0',
aVLoadingIndicatorView: '2.1.3',
glide : '4.8.0-SNAPSHOT',
// For wearable
wear : '2.3.0',
playServicesWearable : '15.0.1',
supportWearable : '27.1.1',
// For testing
junit : '4.12',
truth : '0.42',
espresso : '3.1.0-alpha4',
mockito : '2.21.0'
]
libraries = [
.....
.....
// For testing
junit : "junit:junit:${versions.junit}",
espressoCore : "androidx.test.espresso:espresso-core:${versions.espresso}",
espressoIntents : "androidx.test.espresso:espresso-intents:${versions.espresso}",
roomTest : "android.arch.persistence.room:testing:${versions.room}",
truth : "com.google.truth:truth:${versions.truth}"
]
}
还有build.gradle(project...
:
buildscript {
apply from: rootProject.file('dependencies.gradle')
repositories {
jcenter()
google()
maven {
url 'https://maven.fabric.io/public'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-rc02'
classpath 'com.google.gms:google-services:4.1.0'
classpath 'io.fabric.tools:gradle:1.25.4'
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://maven.google.com' }
}
apply from: rootProject.file('dependencies.gradle')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
还有build.gradle(Module...
:
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 28
defaultConfig {
applicationId "ww.ww.ww"
minSdkVersion 16
targetSdkVersion 28
versionCode 13
versionName "2.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
}
buildTypes {
....
}
....
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
implementation 'com.android.support:exifinterface:28.0.0-rc02'
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
implementation 'com.android.support:cardview-v7:28.0.0-rc02'
implementation 'com.android.support:design:28.0.0-rc02'
implementation 'com.android.support:support-vector-drawable:28.0.0-rc02'
implementation 'com.android.support:multidex:1.0.3'
//espresso
testImplementation libraries.junit
testImplementation libraries.truth
androidTestImplementation libraries.espressoCore
androidTestImplementation libraries.espressoIntents
...
}
注意此行:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
答案 3 :(得分:0)
如果使用Context等Android功能,则需要将测试文件放在androidTest
目录中,并用@RunWith(AndroidJUnit4.class)
注释该类。