最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - Thread.currentThread().contextClassLoader.getResource() can't get folder in Release - Stack Overflow

programmeradmin7浏览0评论

Use Intellij Idea Compose Desktop project

Main.kt

@Composable
@Preview
fun App() {
    MaterialTheme {
        Button(
            onClick = {
                    val resource = Thread.currentThread()
                    .contextClassLoader.getResource("test")
                    if (resource != null) {
                        println("path: ${resource.path}")
                    } else {
                        println("no found")
                    }
            }
        ) {
            Text("check")
        }
    }
}


fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        App()
    }
}

build.gradle.kts

import .jetbrainspose.desktop.application.dsl.TargetFormat

plugins {
    kotlin("jvm")
    id(".jetbrainspose")
    id(".jetbrains.kotlin.pluginpose")
}

group = "com.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    maven("/public/p/compose/dev")
    google()
}

dependencies {
    // Note, if you develop a library, you should use compose.desktopmon.
    // compose.desktop.currentOs should be used in launcher-sourceSet
    // (in a separate module for demo project and in testMain).
    // With compose.desktopmon you will also lose @Preview functionality
    implementation(compose.desktop.currentOs)
    implementation(".jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation(".jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
}

compose.desktop {
    application {
        mainClass = "MainKt"
        buildTypes.release.proguard {
            isEnabled = true
            configurationFiles.from("compose-desktop.pro")
            version.set("7.4.0")
        }
        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "ResourceTest"
            packageVersion = "1.0.0"

            appResourcesRootDir.set(project.layout.projectDirectory.dir("src/main/resources"))
        }
    }
}

while runDistributable print path: file:/Users/shinxguangxing/Desktop/ResourceTest/ResourceTest/build/compose/binaries/main/app/ResourceTest.app/Contents/app/ResourceTest-1.0-SNAPSHOT-2ce49fe8d045f05311e2c12a2ba9c4cc.jar!/test

but while runReleaseDistributable

1.buildTypes.release isEnable = false print path: file:/Users/shinxguangxing/Desktop/ResourceTest/ResourceTest/build/compose/binaries/main-release/app/ResourceTest.app/Contents/app/ResourceTest-1.0-SNAPSHOT-2ce49fe8d045f05311e2c12a2ba9c4cc.jar!/test

2.buildTypes.release isEnable =true and add proguard-rules to keep

-keep public class java.lang.Thread {
    public static java.lang.Thread currentThread();
    public java.lang.ClassLoader getContextClassLoader();
}

-keep public class java.lang.ClassLoader {
    public java.URL getResource(java.lang.String);
}


-keep class **.resources.** { *; }

-keepnames class **.resources.** { *; }

print no found

Conclusion:

In release environment, even with proguard-rules added, Thread.currentThread().contextClassLoader.getResource("test") Still unable to retrieve the folder under the resource file

Test Demo: ResourceTest.zip

I hope to get the resource file path in release environment,what should i do?

发布评论

评论列表(0)

  1. 暂无评论