Android 工程使用 opencv 4.10.0 后编译失败问题

313 阅读2分钟

Android 工程升级 opencv 到 4.10.0 后编译失败,错误信息如下:

Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

这个错误信息表明我在使用的某些 Kotlin 库或依赖项是用不同版本的 Kotlin 编译器编译的,导致二进制不兼容问题。具体来说,错误信息显示 kotlin.Unit 类是用 Kotlin 1.8.0 编译的,而我项目使用的 Kotlin 版本是 1.6.0。

解决方法

  1. 更新项目的 Kotlin 版本:将项目中的 Kotlin 版本更新到与依赖项一致的版本(即 1.8.0)。

  2. 降级不兼容的依赖项:将不兼容的依赖项降级到与我的项目 Kotlin 版本一致的版本(即 1.6.0)。

因为 opencv 4.10.0 是三方依赖,它使用 kotlin 高版本编译,需要 kotlin 1.8.0+,不应该修改它。应该升级我自己的 Android 工程 kotlin 版本(我的工程创建时间较早,kotlin 插件版本是 1.6.0)。

首先将 libs.versions.toml 中的 kotlin 插件 org.jetbrains.kotlin.android 版本改成 1.8.x 的最高版本。

[versions]
...
kotlin = "1.8.22"[plugins]
...
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

升级后编译,得到如下错误:

Task :common:compileDebugKotlin
kotlinOptions.freeCompilerArgs were changed on task :common:compileDebugKotlin execution phase: -Xplugin=/Users/jiemeng/.gradle/caches/modules-2/files-2.1/androidx.compose.compiler/compiler/1.1.1/c65ccf3977fa296bcaef3a4a1f4cf879b8476a11/compiler-1.1.1.jar, -P, plugin:androidx.compose.plugins.idea:enabled=true, -Xallow-unstable-dependencies, -P, plugin:androidx.compose.compiler.plugins.kotlin:sourceInformation=true, -P, plugin:androidx.compose.compiler.plugins.kotlin:liveLiterals=true
This behaviour is deprecated and become an error in future releases!
Approximate place of modification at execution phase:
com.android.build.gradle.internal.utils.KgpUtils$addComposeArgsToKotlinCompile$1.execute(kgpUtils.kt:229)
com.android.build.gradle.internal.utils.KgpUtils$addComposeArgsToKotlinCompile$1.execute(kgpUtils.kt:205)
e: This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.8.22 which is not known to be compatible.  Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don't say I didn't warn you!).

这个错误表明我使用的 Compose Compiler 版本 (1.1.1) 与 Kotlin 插件版本 (1.8.22) 不兼容。为了解决这个问题,我需要更新 Compose Compiler 版本以匹配 Kotlin 插件版本。

确保 Compose Compiler 版本与 Kotlin 版本兼容非常重要,因为 Compose Compiler 是一个 Kotlin 编译器插件,它依赖于 Kotlin 编译器的内部实现和 API。如果这两个版本不兼容,可能会导致编译错误、运行时错误,甚至无法构建项目。

解决方法

  1. 更新 Compose 编译器版本:确保使用的 Compose Compiler 版本与 Kotlin 版本 (1.8.22) 兼容。

更新 Compose 编译器版本

首先,找到与 Kotlin 1.8.22 兼容的 Compose 编译器版本。可以在 Compose Compiler 版本发布页面 查找合适的版本。

因为我用的 kotlin 插件版本是 1.8.22,就搜索 1.8.22。找到了:

Version 1.4.8
June 28, 2023
​
androidx.compose.compiler:compiler:1.4.8, androidx.compose.compiler:compiler-daemon:1.4.8, and androidx.compose.compiler:compiler-hosted:1.4.8 are released. Version 1.4.8 contains these commits.
​
New Features
​
Target Kotlin compiler version is bumped to 1.8.22.
Bug Fixes
​
Improved error message for @Composable overrides. Now it correctly points out annotation mismatch.
Warn about redundant @Composable annotation on inline lambdas that should not be marked as composable. This feature will not be supported with K2 compiler.

适合的 Compose Compiler 版本就是 1.4.8,

接下来就是修改工程配置:

libs.versions.toml 中定义 Compose Compiler 版本**

[versions]
kotlin = "1.8.22"
composeCompiler = "1.4.8"  # 假设这个版本与 Kotlin 1.8.22 兼容
​
...
​
[plugins]
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

build.gradle.kts 中引用 Compose 编译器

android {
    defaultConfig {
        ...
    }
    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
    composeOptions {
        kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get() // 使用 libs.versions.toml 中定义的 composeCompiler 版本
    }
}
​
...

重新编译成功。