2

I am writing a kotlin library for our project. When finished i built an .aar file and sent it to the team. But they have an error as "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" (core-ktx version of the library was 1.3.2 and kotlin-gradle-plugin version was 1.5.0 at that time).

I researched about it and found this thread. "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"

I tried given solutions here but none of this worked so far. Whenever i go below version 1.5 of kotlin-gradle-plugin, i see errors like Runtime JAR files in the classpath have the version 1.4, which is older than the API version 1.5?

I am sharing gradle files here.

build.gradle(Project):

// 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'
}

And here are the versions of dependencies of our project that i need to implement the library to (they are in a text file so i'm copying from there):

minSdkVersion: 21
targetSdkVersion: 30
compileSdkVersion: 30

appCompatVersion: "1.0.0"
gradleVersion : "3.3.0"
kotlinVersion: "1.3.60"
coreKtxVersion: "1.0.2"

Any suggestions about what to do?

Neco
  • 55
  • 1
  • 1
  • 5
  • I had similar problems recently. I updated the `Room` library to `2.4.0-rc01` and as a result, I had to update the `Kotlin` to `1.6.0`. So maybe you need to update the Kotlin in your main project configs. Also because of the experience I had today about generating output files, I highly recommend invalidating Android Studio caches including `build` folder inside `app` module of your main project. – YUSMLE Jan 09 '22 at 15:44
  • @YUSMLE I solved my problem downgrading the versions of my library dependencies to the versions of the target application since i have no control over the target application. But if i did, your solution could do good also. thanks for reply. – Neco Jan 13 '22 at 11:15

2 Answers2

3

Updating this in build.gradle works for me (in root-level build.gradle not in app/build.gradle)

buildscript {
    ext.kotlin_version = '1.6.0'
Sandeep Dixit
  • 799
  • 7
  • 12
0

If you are creating a library using a newer version of Kotlin (eg. 1.8.10) and you want the library to work in projects with older versions of Kotlin (eg. 1.5.0) you need to set languageVersion and apiVersion:

compileKotlin {
    kotlinOptions {
        jvmTarget = '1.8'
        languageVersion = '1.4'
        apiVersion = '1.4'
    }
}

Keep in mind that the lower the version the fewer new Kotlin features you would be able to use in the source code of the library.

Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148