https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

cyberavater

03/11/2024, 7:57 AM
Hi all, I need help. It seems I can't access the resources of
src\jvmMain\resources\a.txt
in jvmMain kts:
Copy code
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:
Copy code
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 actually works on jvm project which is not inside multiplatform so I'm asking here.
m

Matt Nelson

03/11/2024, 12:54 PM
The resource must reside in an intermediate source set if you want it available from both
androidMain
and
jvmMain
. Something like
jvmAndroidMain
Wait... I totally misread the problem. Maybe prefix the resource path with
/
? Alternatively, try obtaining a class within your module and getting the resource stream from that.
Copy code
SomeClassFromModuleWithADotTxt::class.java.getResourceAsStream("/a.txt")
c

cyberavater

03/11/2024, 3:13 PM
I tried this too
Copy code
val message =
    Thread.currentThread().contextClassLoader.getResource("/a.txt")?.readText()
println(message)
but apparently no luck.
another thing to mention is that I can access the resource when I run test in jvmMain.
2 Views