How to add a project as a [jar] module depndency

48 views Asked by At

Update:

Upon further investigation, I found the issue https://youtrack.jetbrains.com/issue/KT-66523/jvmMain-resources-classpath-is-not-added-when-using-the-multiplatform-project-as-a-module

The resource classpath is not added when consumed by adding

    implementation(project(":sevenzipJavm"))

but when the jar is added then I can access the resources.

So, now unless the issue is fixed by JetBrains the current best workaround is to consume the project as a jar instead of classes. but this brings us to a new problem as it seems there is no easy way of adding as a jar. even when I do

  implementation(files("../sevenzipJavm/build/libs/sevenzipJavm-jvm.jar"))

It still uses build\classes. The only way I can make it use the jar is to move it out of the project and then link it

implementation(files("D:/x/sevenzipJavm-jvm.jar"))

This is very cumbersome as I need to manually build and move it every time I change something.

Thus, I have also updated the problem title. Again don't show me how to add a project as classes but as a jar and not staticly by moving it elsewhere.


Old for reference:

It seems I can't access the resources of src\jvmMain\resources\a.txt in jvmMain of the multiplatform project.

gradle kts:

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.android.library)
//    id("module.publication")
}

kotlin {
    applyDefaultHierarchyTemplate()
    jvm()


    androidTarget {

        publishLibraryVariants("release")
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }

//    iosX64()
//    iosArm64()
//    iosSimulatorArm64()
//    linuxX64()

    sourceSets {

        commonMain.dependencies {


        }

        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }

        androidMain.dependencies {
        }

        jvmMain {
            dependencies {
            }

        }


    }
}

android {
    namespace = "org.jetbrains.kotlinx.multiplatform.library.sevenZip"
    compileSdk = libs.versions.android.compileSdk.get().toInt()
    defaultConfig {
        minSdk = libs.versions.android.minSdk.get().toInt()
    }
}

sample:

        val message =
            Thread.currentThread().contextClassLoader.getResource("a.txt")?.readText()
        println(message)

Can anyone tell me what's wrong here? and how to fix it. This code works on pure JVM projects if is not inside multiplatform.

1

There are 1 answers

3
shaktiman_droid On

Instead of this

Thread.currentThread().contextClassLoader.getResource("a.txt")?.readText()

Do this,

this.javaClass.classLoader.getResource("a.txt")?.readText()

I just tested it and it works.