I have resources placed in "resources" directory o...
# multiplatform
m
I have resources placed in "resources" directory of the commonMain source set as mentioned here: https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-images-resources.html#configuration I can access them on Android using the following code for example as InputStream (or can use "resource" method with readBytes()):
applicationContext.javaClass.classLoader?.getResource("music.mp3")
This will get something like this as URL:
jar:file:/data/app/~~703PFoktDXUKZn5lPAVPA==/com.example.test-AtrtrkqGf82Y2vOReZF9Ew==/base.apk!/music.mp3
But I have problem with creating Android Uri which is needed for ExoPlayer and its MediaItem:
MediaItem.Builder().setUri(uri).build()
This will throw:
<http://sun.net|sun.net>.<http://www.protocol.jar.JarURLConnection|www.protocol.jar.JarURLConnection> cannot be cast to java.net.HttpURLConnection
Is there any solition other than copying the resource to the assets directory for Android? It will be then accessible using:
Uri.parse(("file:///android_asset/music.mp3"))
Thanks
not kotlin but kotlin colored 1
e
I have the same question but for iOS. Is there any general solution for copying files from common resources to the platform specific bundles at least for Android and iOS?
m
You can you this script in gradle for copying:
Copy code
tasks.register<Copy>("copyiOSTestResources") {
  from("src/commonTest/resources")
  into("build/bin/iosX64/debugTest/resources")
}

tasks.findByName("iosX64Test")!!.dependsOn("copyiOSTestResources")
It's taken from: https://developer.squareup.com/blog/kotlin-multiplatform-shared-test-resources/ Also worth mentioning that article listed above from Jetbrains website has also part about placing resources in platform specific folders if shared resource folder is not an option. Shared resource folder is working so far good for me (don't counting that Uri problem).
e
@Michal Havryluk thank you for the snippet? Did you try it already? My last tests showed that the file placed in any of resource folder (common or ios) is not present in the common bundle made.
m
I haven't tried exactly this one, but I'm using similar for example for coyping git hooks. So it should be working similary.
Copy code
tasks.register("PreCommitMessage", Copy::class) {
    from(file("${rootProject.rootDir}/commit-msg"))
    into(File(rootProject.rootDir, ".git/hooks"))
}
What are you using for accessing that shared resources folder on iOS? I'm using:
NSBundle.mainBundle.resourcePath + "/compose-resources/" + asset // asset is name of the resource
e
@Michal Havryluk I went to the emulator folder in Finder and opened the bundle itself and didn’t find anything there.
m
Perhaps you have missplaced resource folder - it should be inside commonMain/resources. Or try to follow the article mentioned in the first message here. It should be working this way.
e
@Michal Havryluk I will try one more time. But it was indeed commonMain/resources folder.
👍 1
m
Just a follow up - I have created issue in Media3/Exoplayer repository about this Uri/Resources issue - https://github.com/androidx/media/issues/1405
👍 1
399 Views