How to add android library dependency to a kotlin module?

919 Views Asked by At

I have a simple project that contains two modules within android my app. One is a kotlin module and another an android library. I would like to add the android library as a dependency to the kotlin module. However when I try I get an error Could not resolve project :androidlibrary.

Is there any way to implement an android library into a kotlin module?

kotlin module Build.gradle

plugins {
    id 'java-library'
    id 'org.jetbrains.kotlin.jvm'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

dependencies {
    implementation(project(":androidlibrary"))
}

android library Build.gradle

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {
...
}

dependencies {
...
}
2

There are 2 best solutions below

1
César Bermúdez On

Taken from Gradle could not resolve project :linkedin-sdk

Just change:

implementation(project(":androidlibrary"))

to

compile project(path: ':androidlibrary', configuration: 'default')
0
stobix On

Not sure if this will solve your problem, but it solved a related module inclusion problem for me:

Make sure that you have a JavaVersion that is "same enough" in the two modules!

I tried importing a project with sourceCompatibility = JavaVersion.Version_17 (i.e. v17) into a module with sourceCompatibility = JavaVersion_1_7 (i.e. v1.7) which made the latter silently ignore the classes from the former without even giving me an error on gradle sync.

Making sure that everything has the same JavaVersion everywhere (or at least > 1.7) solved my own dependency including problem; hopefully it works for you as well.