我的视图模型类的导入语句有一些问题。如果我在导致错误的两个viewmodel及其依赖项中清除或添加一行,我就可以再成功构建一次。但给我"未解决的参考"上一堆我注入的视图模型,如果我改变我的活动/片段的任何东西,并尝试再次构建没有清洗。
我已经在gradle文件中尝试了所有不同类型的配置,但无济于事。我尝试了不同的android studio版本,不同的Hilt版本,Kotlin版本。使缓存无效,重新启动和其他一切。谁来帮帮我吧!
我已经尝试粘贴我的配置下面,连同我的MainActivity导入语句和未解析引用引用的视图模型之一。
主要活动
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import com.fauna.fauna.appointments.AppointmentsViewModel
import com.fauna.fauna.clinic.ClinicViewModel
import com.fauna.fauna.databinding.ActivityMainBinding
import com.fauna.fauna.dischargeNotes.DischargeNotesViewModel
import com.fauna.fauna.vaccinationCards.VaccinationCardsViewModel
import com.fauna.fauna.updateApp.UpdateAppDialogFragment
import com.fauna.fauna.user.UserViewModel
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), CoroutineScope {
private val vaccinationCardsViewModel: VaccinationCardsViewModel by viewModels()
//Some functionality
}
VaccinationCardsViewModel
import androidx.lifecycle.viewModelScope
import com.fauna.fauna.fragment.VaccinationCardDetail
import com.fauna.fauna.util.NotificationService
import com.fauna.fauna.util.Result
import com.fauna.fauna.util.SnackBarViewModel
import com.fauna.fauna.util.getSeconds
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class VaccinationCardsViewModel @Inject constructor(
private val vaccinationCardsRepository: VaccinationCardsRepository,
private val notificationService: NotificationService
) : SnackBarViewModel() { //Some functionality}
构建。gradle文件
buildscript {
ext.kotlin_version = '1.5.21'
ext.apollo_version = '2.5.5'
ext.hilt_version = '2.37'
def nav_version = '2.3.2'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.android.tools.build:gradle:7.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
构建。Gradle应用文件
id("com.android.application")
id("kotlin-android")
id("com.apollographql.apollo").version("$apollo_version")
id("androidx.navigation.safeargs.kotlin")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id('com.google.gms.google-services')
id('com.google.firebase.crashlytics')
}
android {
signingConfigs {
debugConfig {
//..omittet..
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.fauna.fauna"
minSdkVersion 23
targetSdkVersion 30
versionCode 15
versionName "0.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
lintOptions {
abortOnError false
}
buildFeatures {
viewBinding true
}
buildTypes {
//..Omittet..
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-runtime-ktx:$nav_version"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha06"
implementation "androidx.security:security-crypto:1.1.0-alpha03"
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//View model
implementation "androidx.activity:activity-ktx:1.3.1"
implementation "androidx.fragment:fragment-ktx:1.3.6"
// Hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}
kapt {
correctErrorTypes = true
}
hilt {
enableExperimentalClasspathAggregation = true
}
apollo {
generateKotlinModels.set(true)
}
beforeEvaluate {
getApiKey()
}
def getSecretKey(string) {
def keysFile = file("secret.properties")
def keysProperties = new Properties()
keysProperties.load(new FileInputStream(keysFile))
def key = keysProperties[string]
println "key: " + key
return key
}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
未清理构建项目时出错:
Task :app:compileDevKotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (19, 41): Unresolved reference: VaccinationCardsViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (21, 29): Unresolved reference: UserViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 44): Unresolved reference: VaccinationCardsViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 73): Not enough information to infer type variable VM
e: /main/java/com/fauna/fauna/MainActivity.kt: (47, 73): Property delegate must have a 'getValue(MainActivity, KProperty<*>)' method. None of the following functions is suitable:
public inline operator fun <T> Lazy<???>.getValue(thisRef: Any?, property: KProperty<*>): ??? defined in kotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 32): Unresolved reference: UserViewModel
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 49): Not enough information to infer type variable VM
e: /main/java/com/fauna/fauna/MainActivity.kt: (49, 49): Property delegate must have a 'getValue(MainActivity, KProperty<*>)' method. None of the following functions is suitable:
public inline operator fun <T> Lazy<???>.getValue(thisRef: Any?, property: KProperty<*>): ??? defined in kotlin
e: /main/java/com/fauna/fauna/MainActivity.kt: (175, 93): Unresolved reference: it
嗨,你的手柄版本可能是错误的,我有类似的问题,下面的设置对我来说是有效的,把它改成2.34测试版,你能在构建gradle应用程序
//Dagger - Hilt
implementation "com.google.dagger:hilt-android:2.34-beta"
kapt "com.google.dagger:hilt-android-compiler:2.34-beta"
//extra dagger hilt dependecies for view model lifecycle attaching for your reference
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
并将最小SDK版本更改为
minSdk 24
我的kotlin gradle插件是1.5.10在项目级别(如果它有帮助)
问题存在于从Hilt和我的Apollo模式文件生成的文件中。我在我的android项目中有一个名为user
的包,在我的Apollo模式目录中有一个名为User
的子文件夹,这显然使android studio在随后的构建中感到困惑。解决方案是删除Apollo模式目录中的子文件夹,或者将项目中的包命名为与模式目录中的子文件夹不同的名称。
对于我来说,我刚刚添加了视图模型生命周期依赖关系,如下所示:
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"