androidandroid-jetpack-compose

What is causing this Error: com.android.tools.r8.internal.nc: Sealed classes are not supported as program classes


I'm learning Jetpack Compose, and while learning I ran into some problems that I don't know how to solve. Below is my development environment, code and compiled result.

Environment:

Code:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DemoTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting()
                }
            }
        }
    }
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Greeting(
    viewModel: GreetingViewModel = remember {
        GreetingViewModel()
    }
) {
    val uiState = viewModel.uiState.collectAsState().value
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(32.dp)
    ) {
        TextField(value = uiState.name, onValueChange = {

            viewModel.handleAction(
                GreetingViewModel.UiAction.NameChanged(it)
            )
        })
        Text(text = "Hello ${uiState.name}!")
    }
}
class GreetingViewModel {
    data class UiState(val name: String)

    private val _uiState = MutableStateFlow(UiState(name = ""))
    val uiState: StateFlow<UiState> = _uiState.asStateFlow()

    sealed class UiAction {
        class NameChanged(val name: String) : UiAction()
    }

    fun handleAction(action: UiAction) {
        when (action) {
            is UiAction.NameChanged -> {
                _uiState.value = uiState.value.copy(
                    name = action.name
                )
            }
        }
    }
}

Result:

GreetingViewModel$UiAcion.class: Decompiled.class file, bytecode version:61.0(Java 17)

Waring: One or more classes has class file version >= 56 which is not officially supported.

Error: com.android.tools.r8.internal.nc: Sealed classes are not supported as program classes

Solution

  • After upgrading to AGP 8.0 a few issues including this one came up, here's how to resolve each one.

    Resolving Sealed Class

    As of Aug 16th 2023 you can use the following block of code in your settings.gradle or settings.gradle.kts files until the release of Android Studio Hedgehog / AGP 8.2

    pluginManagement {
    buildscript {
        repositories {
            mavenCentral()
            maven {
                url = uri("https://storage.googleapis.com/r8-releases/raw")
            }
        }
        dependencies {
            classpath("com.android.tools:r8:8.2.24")
        }
    }
    

    source: https://issuetracker.google.com/issues/227160052#comment37

    Resolving java.lang.StringConcatFactory

    add this rule to your proguard-rules.pro

    -dontwarn java.lang.invoke.StringConcatFactory
    

    source: https://issuetracker.google.com/issues/250197571#comment25

    Missing Classes

    See this answer for resolving the issue https://stackoverflow.com/a/76201356/1269953