Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在运行时从Android应用程序读取jar文件中保存的第三方资源列表_Android_Classpath - Fatal编程技术网

如何在运行时从Android应用程序读取jar文件中保存的第三方资源列表

如何在运行时从Android应用程序读取jar文件中保存的第三方资源列表,android,classpath,Android,Classpath,我正在调查在我当前的Android应用程序中使用citeproc api 'de.undercouch:citeproc-java:2.0.0' implementation 'org.citationstyles:styles:20.11' implementation 'org.citationstyles:locales:20.11' 它使用起来很好 // https://repo1.maven.org/maven2/com/eclipsesource/j2v8/j2v8/6.2.

我正在调查在我当前的Android应用程序中使用
citeproc

api 'de.undercouch:citeproc-java:2.0.0'
implementation 'org.citationstyles:styles:20.11'
implementation 'org.citationstyles:locales:20.11'
它使用起来很好

   // https://repo1.maven.org/maven2/com/eclipsesource/j2v8/j2v8/6.2.0/
    implementation(name: 'j2v8-6.2.0', ext: 'aar')
然而,当我在安卓操作系统上运行时,CSL静态方法

CSL.getSupportedStyles()
返回一个空列表

此方法中的底层代码如下所示:-

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;
}
我所需要的只是Eclipse java项目中显示为驻留在
style-20.11.jar
文件中的“
.csl
”文件列表

当我提取应用程序APK文件时,“
.csl
”文件都单独列出

我哪里做错了


如何获取csl可用的所有“
.csl
”文件的列表?

您提供的详细信息指导我找到解决方案。这就足够在checker条件下定义apk了

class CLSHelper {

    public static Set<String> getSupportedStyles() throws IOException {
        return getAvailableFiles("", "ieee", "csl");
    }

    /**
     * Customizing this function to able run in Android environment
     */
    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 or apk file containing the file
            if (path.endsWith(".jar!/" + name) || path.endsWith(".apk!/" + name)) { // changing this line
                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;
    }
}
另外:要在AndroidStudio上运行,我必须在
build.gradle

android{
    ...
    packagingOptions {
        exclude 'META-INF/truffle/language'
    }
}

干得好,我很接近,但你赢得了赏金
CSL.getSupportedStyles()        // []
CLSHelper.getSupportedStyles()  // [dependent/annals-of-occupational-and-environmental-medicine, dependent/photoacoustics, dependent/statistical-science, twentieth-century-music, dependent/aims-medical-science, dependent/cell-systems, dependent/nursingplus-open, dependent/computer-science-review,...]
android{
    ...
    packagingOptions {
        exclude 'META-INF/truffle/language'
    }
}