低版本Kotlin项目集成CameraX遇元数据版本兼容问题求助
问题:旧版Kotlin项目集成CameraX时的版本兼容错误
我在使用Kotlin 1.3.61的Android项目中集成CameraX,操作步骤如下:
- 在根
build.gradle文件中添加配置:
buildscript { repositories { google() mavenCentral() maven { url 'https://jitpack.io' } mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:4.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() maven { url 'https://jitpack.io' } mavenCentral() } }
- 在
/pes/build.gradle中添加CameraX依赖(因冲突需排除指定组):
def camerax_version = "1.3.0-alpha07" implementation("androidx.camera:camera-core:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' } implementation ("androidx.camera:camera-camera2:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' } implementation("androidx.camera:camera-lifecycle:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' } implementation("androidx.camera:camera-video:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' } implementation("androidx.camera:camera-view:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' } implementation("androidx.camera:camera-mlkit-vision:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' } implementation("androidx.camera:camera-extensions:${camerax_version}") { exclude group: 'com.google.android.gms' exclude group: 'org.jetbrains.kotlin' }
编译时出现以下错误:
e: C:/Users/AMI/.gradle/caches/transforms-3/64ad5900933e780ce887a006f25ca2fd/transformed/jetified-camera-extensions-1.3.0-alpha07-api.jar!/META-INF/camera-extensions_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.1.16. e: C:/Users/AMI/.gradle/caches/transforms-3/7dee4547e37043c00f427a0185d4b10d/transformed/jetified-camera-view-1.3.0-alpha07-api.jar!/META-INF/camera-view_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.1.16. e: C:/Users/AMI/.gradle/caches/transforms-3/8f6f30467f847b66b16817abdf472683/transformed/jetified-camera-video-1.3.0-alpha07-api.jar!/META-INF/camera-video_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.1.16. e: C:/Users/AMI/.gradle/caches/transforms-3/99bb6fcc1c04207e3b031d7cda6d0f11/transformed/jetified-camera-core-1.3.0-alpha07-api.jar!/META-INF/camera-core_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.1.16. e: C:/Users/AMI/.gradle/caches/transforms-3/df896c991bee1d4be7efa89fb4fe6913/transformed/jetified-camera-camera2-1.3.0-alpha07-api.jar!/META-INF/camera-camera2_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.1.16.
我的项目buildscript中指定Kotlin版本为1.3.61:
buildscript { // Butterknife end of life will not work with JAVA_VERSION_11 ext.butterknifeVersion = '10.2.3' // Can not upgrade to 1.4.21, annotations issues related to not using Java 11 ext.kotlin_version = '1.3.61' }
我无法理解为何版本1.3.61高于预期的1.1.16却仍报错,恳请帮忙解释原因。
原因解释
你看到的1.1.16并不是Kotlin语言版本,而是Kotlin编译后生成的元数据二进制版本号,它和Kotlin语言版本是两套独立的编号体系:
- Kotlin语言版本是我们日常使用的
1.3.61、1.8.x这类编号,对应语言特性的迭代 - 元数据二进制版本是Kotlin编译器用来标识编译后字节码元数据格式的版本号,不同语言版本的编译器会生成对应版本的元数据
你的项目使用的Kotlin 1.3.61编译器只能识别最高到1.3版本的元数据,但你引入的CameraX 1.3.0-alpha07是用Kotlin 1.8编译的,生成的元数据版本为1.8.0。旧版编译器无法解析更高版本的元数据,因此抛出兼容性错误。错误提示里的expected version is 1.1.16是旧版编译器的表述问题,本质是当前编译器不支持处理1.8版本的元数据。
解决方案
由于你无法升级Kotlin版本(受限于ButterKnife 10.2.3不支持Java 11),只能降级CameraX到一个与Kotlin 1.3兼容的版本。CameraX 1.0.x稳定版是用旧版Kotlin编译的,可兼容你的项目环境:
修改模块build.gradle中的CameraX版本配置:
def camerax_version = "1.0.2" implementation "androidx.camera:camera-core:${camerax_version}" implementation "androidx.camera:camera-camera2:${camerax_version}" implementation "androidx.camera:camera-lifecycle:${camerax_version}" implementation "androidx.camera:camera-view:${camerax_version}" // 若不需要video、mlkit、extensions模块可省略对应依赖
同时可以去掉排除org.jetbrains.kotlin组的配置,因为旧版CameraX依赖的Kotlin版本与你的项目兼容,不会产生冲突。
内容的提问来源于stack exchange,提问作者Ameen Izhac
相关产品推荐
相关产品推荐

