【问题标题】:When trying jetpack compose it show error: compiler backend and cannot be loaded by the old compiler尝试jetpack compose时显示错误:编译器后端并且无法由旧编译器加载
【发布时间】:2020-12-16 13:27:51
【问题描述】:

类 'androidx.compose.ui.platform.ComposeView' 由新的 Kotlin 编译器后端编译,旧编译器无法加载

这是我的 onCreate 方法:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_delivered, container, false).apply {
        findViewById<ComposeView>(R.id.compose_view).setContent {
            MaterialTheme {
                Surface {
                    Text("Hello")
                }
            }
        }
    }
}

撰写版本:

accompanistVersion = "0.1.9"
composeVersion = '0.1.0-dev17'

app.gradle

buildFeatures {
        compose true
        dataBinding true
    }
    composeOptions {
        kotlinCompilerVersion rootProject.kotlinVersion
        kotlinCompilerExtensionVersion rootProject.composeVersion
    }



// Compose
    implementation "androidx.compose.runtime:runtime:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui:$rootProject.composeVersion"
    implementation "androidx.compose.foundation:foundation:$rootProject.composeVersion"
    implementation "androidx.compose.foundation:foundation-layout:$rootProject.composeVersion"
    implementation "androidx.compose.material:material:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui-viewbinding:$rootProject.composeVersion"
    implementation "androidx.ui:ui-tooling:$rootProject.composeVersion"
    implementation "androidx.compose.runtime:runtime-livedata:$rootProject.composeVersion"
    implementation "com.google.android.material:compose-theme-adapter:$rootProject.composeVersion"
    implementation "dev.chrisbanes.accompanist:accompanist-coil:$rootProject.accompanistVersion"

【问题讨论】:

    标签: android kotlin android-jetpack android-jetpack-compose


    【解决方案1】:

    请将此任务添加到您的 app/build.gradle 文件中:

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"
            freeCompilerArgs += ["-Xallow-jvm-ir-dependencies", "-Xskip-prerelease-check"]
        }
    }
    

    【讨论】:

    • 为什么需要这些编译器参数:虽然 Jetpack Compose 仍处于 alpha 阶段,但在使用由新 IR 后端编译的依赖项以及使用尚未发布的类时,这些会抑制错误。 (github.com/android/compose-samples/issues/…)
    【解决方案2】:

    默认app/build.grade,在使用“Empty Compose Activity”创建新项目时,包括以下选项。

    android {
        // other options
    
        kotlinOptions {
            jvmTarget = '1.8'
            useIR = true
        }
    
        // more options
    }
    

    添加这些选项(特别是 useIR = true)似乎可以解决我的错误。


    useIR 选项引用了 Kotlin 的 new JVM backend,文档具体说明了以下内容。

    如果您启用 Jetpack Compose,您将自动选择加入新的 JVM 后端,而无需在 kotlinOptions 中指定编译器选项。

    这似乎是不正确的。

    【讨论】:

    【解决方案3】:

    按照official setup guide 中的步骤,我遇到了同样的问题。

    添加必要的dependencies/configuration for the compose library 为我解决了这个问题。

    【讨论】:

    • tasks.withType 部分为我修复了它。
    【解决方案4】:

    .kts 版本

    tasks.withType<KotlinCompile>() {
        kotlinOptions.jvmTarget = "1.8"
        kotlinOptions.freeCompilerArgs = listOf(
            *kotlinOptions.freeCompilerArgs.toTypedArray(),
            "-Xallow-jvm-ir-dependencies",
            "-Xskip-prerelease-check")
        useIR = true
    }
    

    【讨论】:

    • 次要评论:我必须添加 kotlinOptions。使用前IR。
    【解决方案5】:

    这是在我的情况下导致相应错误的依赖项:

    androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.5'
    

    我猜玩它应该会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2017-09-22
      • 1970-01-01
      相关资源
      最近更新 更多