我正在为我们的项目编写一个kotlin库。完成后,我构建了一个.aar文件并将其发送给团队。但他们有一个错误,即"Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16"
(当时库的核心ktx版本是1.3.2,kotlin-gradle插件版本是1.5.0(。
我研究了一下,发现了这个线索。"模块是使用不兼容的Kotlin版本编译的。其元数据的二进制版本为1.5.1,预期版本为1.1.16";
我在这里尝试了给定的解决方案,但到目前为止都不起作用。每当我使用低于1.5版本的kotlin-gradle插件时,我都会看到类似Runtime JAR files in the classpath have the version 1.4, which is older than the API version 1.5?
的错误
我在这里共享gradle文件。
build.gradle(项目(:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"
// classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(:app(:
plugins {
id 'com.android.application'
id 'kotlin-android'
// id 'com.google.gms.google-services'
}
android {
compileSdk 30
defaultConfig {
applicationId "com.neco.myDemoProject"
minSdk 21
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation project(path: ':myLibrary')
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
}
build.gradle(:myLibrary(:
plugins {
id 'com.android.library'
id 'kotlin-android'
}
android {
compileSdk 30
defaultConfig {
minSdk 21
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.appcompat:appcompat:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.code.gson:gson:2.8.6'
}
以下是我需要实现库的项目依赖项的版本(它们在一个文本文件中,所以我从那里复制(:
minSdkVersion: 21
targetSdkVersion: 30
compileSdkVersion: 30
appCompatVersion: "1.0.0"
gradleVersion : "3.3.0"
kotlinVersion: "1.3.60"
coreKtxVersion: "1.0.2"
有什么建议吗?
在build.gradle中更新它对我很有效(在根级别build.gradle中,而不是在app/build.gradle(
buildscript {
ext.kotlin_version = '1.6.0'
如果您使用较新版本的Kotlin(例如1.8.10(创建库,并且希望该库在使用较旧版本的Kodlin(例如1.5.0(的项目中工作,则需要设置languageVersion
和apiVersion
:
compileKotlin {
kotlinOptions {
jvmTarget = '1.8'
languageVersion = '1.4'
apiVersion = '1.4'
}
}
请记住,版本越低,可以在库的源代码中使用的Kotlin新功能就越少。