I have a JAR library that retrieves random files from the JAR resources.
When added as a dependency and ran within an Android App, the library fails to retrieve its resources (the resources are to be used by the library within itself, not the Android App).
I’ve seen other answers here saying it’s not possible, but they are all at least 10 years old, and Android changed a lot since then. Is it possible?
This is how the library accesses its resources, if it changes anything:
public static List<String> getAllResourcesFromFolder(String path) throws IOException {
List<String> filenames = new ArrayList();
URL dirURL = ResourcesUtils.class.getClassLoader().getResource(path);
if (dirURL != null && dirURL.getProtocol().equals("jar")) {
JarURLConnection jarConn = (JarURLConnection)dirURL.openConnection();
JarFile jarFile = jarConn.getJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
while(entries.hasMoreElements()) {
String name = ((JarEntry)entries.nextElement()).getName();
if (name.startsWith(path) && !name.equals(path + "/")) {
String entry = name.substring(path.length() + 1);
int checkSubdir = entry.indexOf("/");
if (checkSubdir < 0) {
filenames.add(entry);
}
}
}
} else if (dirURL != null) {
try {
return Arrays.asList((new File(dirURL.toURI())).list());
} catch (NullPointerException | URISyntaxException var9) {
Exception e = var9;
throw new IOException("Unable to convert URL to URI", e);
}
}
return filenames;
}