R8 Compatibility With Sealed Class In Android Studio Flamingo

3.7k Views Asked by At

When I want to release my app get this error: com.android.tools.r8.internal.jb: Sealed classes are not supported as program classes when generating class files

  • Android Studio: Flamingo 2022.2.1 (also i try 2022.3.1 Giraffe)
  • Gradle Version: 8.0 (also i try 8.1 and 8.0.2)
  • Gradle Plugin Version: 8.0.0
  • JDK: 17.0.6 (Android Studio Flamingo Embedded)

and in build.gradle i cant edit compile version because of android studio min gradle.

compileOptions {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
  }
  kotlinOptions {
    jvmTarget = "17"
  }

Sealed class i use in my module is

sealed class Resource<out T> {
  object Loading : Resource<Nothing>()
  data class Success<out T : Any>(val data: T?) : Resource<T>()
  data class Error(val exception: Exception) : Resource<Nothing>()
}

fun <T> Resource<T>.onSuccess(callback: (T?) -> Unit): Resource<T> {
  if (this is Resource.Success) {
    callback.invoke(this.data)
  }
  return this
}

fun <T> Resource<T>.onError(callback: (Exception) -> Unit): Resource<T> {
  if (this is Resource.Error) {
    callback.invoke(exception)
  }
  return this
}

fun <T> Resource<T>.onLoading(callback: () -> Unit): Resource<T> {
  if (this is Resource.Loading) {
    callback.invoke()
  }
  return this
}


fun <T> Resource<T>.isSuccess(): Boolean = this is Resource.Success
fun <T> Resource<T>.isError(): Boolean = this is Resource.Error
fun <T> Resource<T>?.dataOrNull(): T? = if (this is Resource.Success) data else null

inline fun <T> Resource<T>.withResult(
  onLoading: () -> Unit = {},
  onSuccess: (T?) -> Unit = {},
  onError: (Exception) -> Unit = {}
) {
  when (this) {
    Resource.Loading -> onLoading()
    is Resource.Success -> {
      onSuccess(data)
    }

    is Resource.Error -> {
      onError(exception)
    }
  }
}

1

There are 1 best solutions below

0
sgjesse On

This is a known issue, which is tracked in https://issuetracker.google.com/227160052. Please +1 that issue if it affects you.