Reputation: 213
We include an assets folder in our KMM shared/commonMain project. This asset folder basically contains a lot of JSON files, structured in different sub-folders.
In build.gradle, the assets folder is added as android resource, so its included in the root of the build AAR.
android {
compileSdkVersion(29)
sourceSets["main"].resources.srcDirs("src/commonMain/assets")
...
In the shared module, the files are reads on demand in commonAndroid with
val file = this::class.java.classLoader!!.getResource(folder1/file1.json)
This works perfectly fine.
Now to the issue, reading a folder:
val folder= this::class.java.classLoader!!.getResource(folder1)
This works when code is executed via unit tests, then it returns the folder. Once the AAR is build and included in an android app, this line always returns null.
Keep in mind, the files are properly added to the AAR, and they can be read when specifying the path to the file. Its just not working with the folder path only.
Is there any way to get all files from a directory in the AAR module, from within the KMM project?
The iOS solution is working, so its really just about android.
EDIT 1: To clarify, the goal is not to provide access to the folder/files to an external user, it is to use the files within the KMM common module itself.
I am looking for any way to work with files and folders that also works once the project is distributed as AAR and used as third party library.
EDIT 2: It seems like there currently is no solution to access folders in JARS that are within AARs.
So we switched to a workaround, where we generate an index file of all files within the folder at build time, and use this information at runtime to get all files from specific folders, using this::class.java.classLoader!!.getResource(path)
(as getResource works for files, just not for folders)
Upvotes: 3
Views: 2327
Reputation: 2752
I think this is a problem with proguard.
Move your json folders from assets folder to raw folder and changed the keep.xml file.
keep.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@drawable/*,@raw/*"/>
reference: https://developer.android.com/studio/build/shrink-code#keep-resources
Upvotes: 1