I am having some problems with my import statements of my view model class. If I clean or add a single line in the two ViewModels that causes the errors, and their dependencies, I can build one more time successfully. But gives me ‘Unresolved reference’ on a bunch of my injected view models if I change anything in my activities/fragments and try and build again without cleaning.
I have tried all different kinds of configurations in my gradle files, to no avail.
I tried different android studio versions, different Hilt versions, Kotlin versions. Invalidating cache, restarting and everything else. Please someone help me!
I have tried to paste my configuration below, together with my MainActivity import statements and one of the viewmodels the Unresolved Reference is referring to.
Main Activity
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}
build.gradle file
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
}
build.gradle app file
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'
Error upon building the project without cleaning:
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
3
Answers
The issue was in the generated files from both Hilt and my Apollo schema files. I had a package within my android project called
user
and a subfolder in my Apollo schema directory calledUser
which apparently confuses android studio on subsequent builds. The solution is to either delete the subfolders in the Apollo schema directory or name the packages in the project differently from subfolders in the schema directory.Hi your hilt versions might be wrong i had similar issues and this follwoing setup worked for me changing it to 2.34-beta, can you please try the following in build gradle app
and also change min sdk version to
my kotlin gradle plugin is 1.5.10 in project level (if it helps)
For me I have just added the view model life cycle dependency as follow: