我不確定是什么導致了這個問題。Android Studio 中沒有編譯時錯誤。這僅在我運行 android 測驗時發生:
Task :app:compileDebugAndroidTestKotlin FAILED
e: C:\Users\alexl\Documents\Speak-Easy\app\src\androidTest\java\com\spark\chat\screen\AuthenticationScreen.kt: (4, 25): Unresolved reference: BaseScreen
e: C:\Users\alexl\Documents\Speak-Easy\app\src\androidTest\java\com\spark\chat\screen\AuthenticationScreen.kt: (6, 30): Unresolved reference: BaseScreen
e: C:\Users\alexl\Documents\Speak-Easy\app\src\androidTest\java\com\spark\chat\screen\AuthenticationScreen.kt: (9, 9): Unresolved reference: checkHint
e: C:\Users\alexl\Documents\Speak-Easy\app\src\androidTest\java\com\spark\chat\screen\AuthenticationScreen.kt: (13, 9): Unresolved reference: checkHint
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugAndroidTestKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
專案結構
應用程式在不運行 androidTest 時可以正常構建。我有一個包名為 com.spark.common 的模塊。應用包名稱為 com.spark.chat。不確定這是否與它有關。在我的android-library-module.gradle:
apply plugin: "com.android.library"
apply plugin: "org.jetbrains.kotlin.android"
android {
compileSdk DefaultConfig.compileSdk
defaultConfig {
minSdk DefaultConfig.minSdk
targetSdk DefaultConfig.targetSdk
versionCode DefaultConfig.versionCode
versionName DefaultConfig.versionName
testInstrumentationRunner DefaultConfig.testInstrumentationRunner
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation Lib.androidXCoreKotlin
implementation Lib.androidXAppCompat
implementation Lib.googleMaterial
implementation Lib.constraintLayout
testImplementation Lib.junit
androidTestImplementation Lib.androidXJunit
androidTestImplementation Lib.espresso
androidTestImplementation Lib.espressoIntents
androidTestImplementation Lib.androidXTruth
androidTestImplementation Lib.androidXRunner
androidTestImplementation Lib.androidXRules
}
在我的common.build.gradle 中:
apply from: "$rootDir/android-library-build.gradle"
在我的app.build.gradle:
apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
android {
compileSdk DefaultConfig.compileSdk
defaultConfig {
applicationId DefaultConfig.applicationId
minSdk DefaultConfig.minSdk
targetSdk DefaultConfig.targetSdk
versionCode DefaultConfig.versionCode
versionName DefaultConfig.versionName
testInstrumentationRunner DefaultConfig.testInstrumentationRunner
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation project(Modules.common)
implementation Lib.androidXCoreKotlin
implementation Lib.androidXAppCompat
implementation Lib.googleMaterial
implementation Lib.constraintLayout
testImplementation Lib.junit
androidTestImplementation Lib.androidXJunit
androidTestImplementation Lib.espresso
androidTestImplementation Lib.espressoIntents
androidTestImplementation Lib.androidXTruth
androidTestImplementation Lib.androidXRunner
androidTestImplementation Lib.androidXRules
}
在我的buildSrc.build.gradle.kts 中:
import org.gradle.kotlin.dsl.`kotlin-dsl`
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
謝謝!
uj5u.com熱心網友回復:
問題是由于 BaseTest 類是公共模塊中 androidTest 目錄的一部分。
只有主目錄中的檔案才會包含在構建檔案中。所以app模塊,在實作普通專案時,只會得到它的主目錄類。
解決方案是將類移動到模塊的主目錄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442556.html
上一篇:recyclerView中的diffutil回呼無法正常作業
下一篇:阻塞每個方法的同步集合
