Code example for XPath

Methods: evaluate, setNamespaceContext

0
    @Override 
    public String getPackage(File manifestFile) {
        XPath xpath = AndroidXPathFactory.newXPath();
 
        try { 
            return xpath.evaluate("/manifest/@package",
                    new InputSource(new FileInputStream(manifestFile)));
        } catch (XPathExpressionException e) {
            // won't happen. 
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } 
 
        return null; 
    } 
 
    @Override 
    public int getMinSdkVersion(@NonNull File manifestFile) {
        try { 
            Object value = AndroidManifest.getMinSdkVersion(new FileWrapper(manifestFile));
            if (value instanceof Integer) {
                return ((Integer)value).intValue();
            } else if (value instanceof String) {
                // TODO: support codename 
            } 
 
        } catch (XPathExpressionException e) {
            // won't happen. 
        } catch (StreamException e) {
            throw new RuntimeException(e);
        } 
 
        return 1; 
    } 
}