Android Studio 3.3
在build.gradle中:
buildscript {
ext.KOTLIN_VERSION = '1.3.21'
ext.ESPRESSO_VERSION = '3.2.0-alpha02'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
在我的app / build.gradle中:
android {
dataBinding {
enabled = true
}
compileSdkVersion 28
defaultConfig {
applicationId "com.myproject.android"
minSdkVersion 18
targetSdkVersion 28
versionCode 6
versionName "0.0.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
//androidTest.assets.srcDirs += 'src/androidTest/assets'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha03'
implementation 'com.android.support:design:28.0.0'
implementation 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.yuyh.json:jsonviewer:1.0.6'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"
implementation project(':common')
androidTestImplementation "androidx.test.espresso:espresso-core:$ESPRESSO_VERSION"
androidTestImplementation "androidx.test.espresso:espresso-intents:$ESPRESSO_VERSION"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$ESPRESSO_VERSION"
androidTestImplementation 'androidx.test:rules:1.1.2-alpha02'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
androidTestImplementation "com.squareup.okhttp3:mockwebserver:3.14.1"
testImplementation 'junit:junit:4.12'
}
这里是意式浓缩咖啡的测试:
@RunWith(AndroidJUnit4::class)
class TradersActivityTest {
val context = InstrumentationRegistry.getInstrumentation().getContext()
val targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext()
var listItemCount = 0
var checkItemCount = 0;
val mockServer = MockWebServer()
@Rule
@JvmField
var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java)
@Before
fun setup() {
mockServer.start()
val recyclerView = tradersIntentTestRule.activity.findViewById<View>(R.id.tradersRecyclerView) as RecyclerView
listItemCount = recyclerView.adapter!!.itemCount
checkItemCount = (0..listItemCount - 1).random()
}
但是我得到了错误:
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:preDebugAndroidTestBuild'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
> Could not resolve com.squareup.okhttp3:okhttp:{strictly 3.12.0}.
Required by:
project :app
> Cannot find a version of 'com.squareup.okhttp3:okhttp' that satisfies the version constraints:
Dependency path 'TM:app:unspecified' --> 'com.squareup.okhttp3:mockwebserver:3.14.1' --> 'com.squareup.okhttp3:okhttp:3.14.1'
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
答案 0 :(得分:2)
之所以发生这种情况,是因为您没有在应用程序中指定特定的OkHttp依赖关系。如果您不指定自己,则gradle将使用Retrofit指定的默认版本。如果您选中Retrofit POM file,则可以看到它指定了3.12.0
,这是您在错误中看到的此版本的来源。
解决方案是为要使用的OkHttp版本添加特定的gradle依赖项:
implementation 'com.squareup.okhttp3:okhttp:3.14.1'
然后将使用该版本代替默认的版本,并符合您的模拟服务器版本的要求