Error trying to inject GenerativeModel object into a viewModel
Closed this issue · 2 comments
Description of the bug:
I am facing an issue when trying to inject with Hilt a GenerativeModel object into a viewModel as below. I am using Jetpack Compose:
@AndroidEntryPoint
class ExampleActivity : ComponentActivity() {
....
@Composable
fun ExampleNavHost(
navController: NavHostController,
modifier: Modifier
) {
NavHost(
navController = navController,
startDestination = route1
) {
composable(route1) {
val exampleViewModel = hiltViewModel<ExampleViewModel>()
ExampleScreen(viewModel = exampleViewModel)
}
composable(route2) {
}
composable(route3) {
}
}
}
}
@Module
@InstallIn(SingletonComponent::class)
class GeminiModule {
@Provides
@Singleton
fun provideGemini(): GenerativeModel {
return GenerativeModel(
modelName = BuildConfig.gemini_1_5_flash,
apiKey = BuildConfig.apiKey,
generationConfig = generationConfig {
temperature = 1f
topK = 64
topP = 0.95f
maxOutputTokens = 8192
responseMimeType = "application/json"
},
safetySettings = listOf(
SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.MEDIUM_AND_ABOVE),
SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE),
SafetySetting(HarmCategory.SEXUALLY_EXPLICIT, BlockThreshold.MEDIUM_AND_ABOVE),
SafetySetting(HarmCategory.DANGEROUS_CONTENT, BlockThreshold.MEDIUM_AND_ABOVE),
)
)
}
}
@HiltViewModel
class ExampleViewModel @Inject constructor(val generativeModel: GenerativeModel) : ViewModel() {
}
java.lang.RuntimeException: Cannot create an instance of class com.example.presentation.ExampleViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:201)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:320)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:302)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:276)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:172)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:184)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:150)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:215)
Caused by: java.lang.NoSuchMethodException: com.example.presentation.ExampleViewModel. [] (Ask Gemini)
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getDeclaredConstructor(Class.java:2170)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:199)
Actual vs expected behavior:
No response
Any other information you'd like to share?
No response
HI @txoksue from the exception in your code it isn't clear that this is caused by the SDK, specifically
Caused by: java.lang.NoSuchMethodException: com.foodai.presentation.ExampleViewModel. [] (Ask Gemini)
seems like the error is in ExampleViewModel
and how it's created by hilt.
Yes, you are right, the problem was hilt configuration dependencies in my project. I raised this bug because everything worked as expected when I removed object injection from the ViewModel constructor.
Thank you.