I’m using jetpack compose in a project.
I’m using android studio Koala 2024.1.2 Patch 1. I’m using everything almost to the latest versions.
I have a simple composable screen (a bunch of Composable functions that use each other). The only special thing that I’m using into this screen is that I’m using PagingData by creating some composables dynamically, and I’m also using a viewmodel injected with Hilt.
I’ve struggled to make the preview work because of the usage of ViewModels, but I’ve workarounded by creating “Content” composables that use the flows in the viewmodel but they don’t access the viewmodel itself.
@Composable
fun SlideMovieScreen(viewModel: SlideMovieViewModel = hiltViewModel()) {
val deviceLanguage = getDeviceLocale()
viewModel.setLanguage(deviceLanguage)
val context = LocalContext.current
val vibrator = remember { getVibrator(context) }
SlideMovieScreenContent(viewModel.swipeAction, viewModel.moviesFlow, {
triggerSmallVibration(vibrator); viewModel.onLikeButtonClicked()
}, {
triggerSmallVibration(vibrator); viewModel.onDislikeButtonClicked()
}, {
viewModel.clearSwipeAction()
})
}
@Preview
@Composable
fun SlideMovieScreenPreview() {
val swipeAction =
remember { MutableStateFlow(SlideMovieViewModel.SwipeAction.LIKE).asStateFlow() }
val moviesFlow =
flowOf(PagingData.from(movies))
FilmatchApp {
SlideMovieScreenContent(swipeAction, moviesFlow, {}, {}, {})
}
}
Other than struggling with my preview, which I think it’s normal when using ViewModels in jetpack, my main problem is that LiveEdit is not working at all. When I run the app and make a minor change such as changing any color, I just see this error:
Internal error Unexpected error during compilation command java.lang.ClassCastException class.org.objectweb.asm.tree.analysis.BasicValue cannot be cast to class com.android.tools.idea.run.deployment.liveedit.analysis.IntValue
Sorry I can’t copy the full error text because when I try to move my mouse cursor to the text, it disappears.
Your issue is hard to replicate as we don’t know what is inside of your SlideMovieScreenContent
Composable. However, you can try an incremental debugging approach to find the cause.
I just created a minimal sample of your code:
@Preview
@Composable
fun SlideMovieScreenPreview() {
val swipeAction =
remember { MutableStateFlow("HELLO").asStateFlow() }
val moviesFlow =
flowOf("WORLD!")
FilmatchApp {
SlideMovieScreenContent(swipeAction, moviesFlow, {}, {}, {})
}
}
@Composable
fun SlideMovieScreenContent(
swipeAction: StateFlow<String>,
moviesFlow: Flow<String>,
onLike: () -> Unit,
onDislike: () -> Unit,
onClear: () -> Unit
) {
// ugly Text Composable is only for testing
Text(text = "${swipeAction.value} ${moviesFlow.collectAsState("").value}")
}
With this code, LiveEdit is working fine. Whenever I change HELLO
or WORLD
, it is correctly refreshed on the emulator.
Please try to paste this code on your end and see if it also works for you.
- If the minimal sample works, start to migrate it to your previous code step-by-step and see at which point LiveEdit stops working. Then you have found your issue and can investigate it further.
- If the minimal sample doesn’t work, go to your
build.gradle
file and check if there are some updated dependencies available. If there are, update all versions to use the latest version. - If it still does not work, you can also check if there are any Android Studio updates available.
1