Wednesday 17 March 2021

Read list of 3rd party jar file resources from Android Application at runtime

Im investigating the use of citeproc in my current Android application

api 'de.undercouch:citeproc-java:2.0.0'
implementation 'org.citationstyles:styles:20.11'
implementation 'org.citationstyles:locales:20.11'

Its working fine using

   // https://repo1.maven.org/maven2/com/eclipsesource/j2v8/j2v8/6.2.0/
    implementation(name: 'j2v8-6.2.0', ext: 'aar')

However as Im running on Android OS the CSL static method

CSL.getSupportedStyles()

returns an empty list.

the underlying code in this method is as follows:-

private static Set<String> getAvailableFiles(String prefix,
        String knownName, String extension) throws IOException {
    Set<String> result = new LinkedHashSet<>();

    // first load a file that is known to exist
    String name = prefix + knownName + "." + extension;
    URL knownUrl = CSL.class.getResource("/" + name);
    if (knownUrl != null) {
        String path = knownUrl.getPath();
        // get the jar file containing the file
        if (path.endsWith(".jar!/" + name)) {
            String jarPath = path.substring(0, path.length() - name.length() - 2);
            URI jarUri;
            try {
                jarUri = new URI(jarPath);
            } catch (URISyntaxException e) {
                // ignore
                return result;
            }
            try (ZipFile zip = new ZipFile(new File(jarUri))) {
                Enumeration<? extends ZipEntry> entries = zip.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry e = entries.nextElement();
                    if (e.getName().endsWith("." + extension) &&
                            (prefix.isEmpty() || e.getName().startsWith(prefix))) {
                        result.add(e.getName().substring(
                                prefix.length(), e.getName().length() - 4));
                    }
                }
            }
        }
    }

    return result;
}

Is it possible to list get getAvailableFiles method to work on Android OS?

The following aproach does not work

   val name: String = "$prefix$knownName.$extension"
   val knownUrl: URL? = CSL::class.java.classLoader.getResource("$name")

All I require is the list of ".csl" files that in an Eclipse java project show as residing in styles-20.11.jar file

When I extract my Application APK file the ".csl" files are all listed seperately

Where am I going wrong?

How can I get a list of all ".csl" files available to CSL?



from Read list of 3rd party jar file resources from Android Application at runtime

No comments:

Post a Comment