R8 兼容性与 Android Studio Flamingo 中的封闭类

huangapple go评论64阅读模式
英文:

R8 Compatibility With Sealed Class In Android Studio Flamingo

问题

I can help translate the code-related content:

Error message:

当我尝试发布我的应用程序时出现此错误:`com.android.tools.r8.internal.jb: 生成类文件时不支持密封类作为程序类`

* Android Studio: Flamingo 2022.2.1(也尝试了 2022.3.1 Giraffe)
* Gradle 版本:8.0(也尝试了 8.1 和 8.0.2)
* Gradle 插件版本:8.0.0
* JDK:17.0.6(Android Studio Flamingo 内嵌)

在 build.gradle 中,由于 Android Studio 的最低 Gradle 版本,我无法编辑编译版本。

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

我在我的模块中使用的密封类是:

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

Please note that the code has HTML entities (e.g., <, >) that need to be converted back to their respective symbols when using it in your code.

英文:

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

得分: 10

这是一个已知问题,已在 https://issuetracker.google.com/227160052 中进行跟踪。如果它影响了您,请+1该问题。

英文:

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

huangapple
  • 本文由 发表于 2023年4月19日 21:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定