R8 Compatibility With Sealed Class In Android Studio Flamingo
Asked Answered
M

1

7

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)
    }
  }
}

Moist answered 19/4, 2023 at 13:59 Comment(6)
I posted an answer, but have some additional questions. 1. Are you using sealed classes directly in your code, or is this hitting you due to javac now generating enums as sealed classes? 2. Are you consuming libraries with sealed classes? 3. As this is only for class file output, are you building a library which is shipped externally? 4. Which new features of JDK-17 are you planning to use?Yeung
I used sealed classes in my library, also some sealed classes generated by compose destination in my lib. I have to use AGP 8.0 due to using Flamingo or Giraffe version of Android Studio which requires Java 17.Moist
For the stable version of Flamingo you should still be able to use AGP 7.4, and even with AGP 8.0 you can still set the JVM to 11 in the build.gradle file to avoid sealed classes being generated in the class files.Yeung
developer.android.com/build/releases/…Moist
developer.android.com/studio/…Moist
developer.android.com/studio/releases#jdk-17Moist
Y
12

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

Yeung answered 20/4, 2023 at 10:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.