apache/cordova-android · error · IllegalArgumentException

Relative URIs are not supported.

Error message

Relative URIs are not supported.

What it means

assertNonRelative rejects any Uri without a scheme (uri.isAbsolute() == false) with IllegalArgumentException. CordovaResourceApi methods (openForRead, openOutputStream, processUri) need an absolute URI because they dispatch on the scheme; relative references like 'img/foo.png' are meaningless until resolved against a base, and the API deliberately does not guess one.

Source

Thrown at framework/src/org/apache/cordova/CordovaResourceApi.java:477

        }
        String dataPartAsString = uriAsString.substring(commaPos + 1);
        byte[] data;
        if (base64) {
            data = Base64.decode(dataPartAsString, Base64.DEFAULT);
        } else {
            try {
                data = dataPartAsString.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                data = dataPartAsString.getBytes();
            }
        }
        InputStream inputStream = new ByteArrayInputStream(data);
        return new OpenForReadResult(uri, inputStream, contentType, data.length, null);
    }

    private static void assertNonRelative(Uri uri) {
        if (!uri.isAbsolute()) {
            throw new IllegalArgumentException("Relative URIs are not supported.");
        }
    }

    public static final class OpenForReadResult {
        public final Uri uri;
        public final InputStream inputStream;
        public final String mimeType;
        public final long length;
        public final AssetFileDescriptor assetFd;

        public OpenForReadResult(Uri uri, InputStream inputStream, String mimeType, long length, AssetFileDescriptor assetFd) {
            this.uri = uri;
            this.inputStream = inputStream;
            this.mimeType = mimeType;
            this.length = length;
            this.assetFd = assetFd;
        }
    }

View on GitHub (pinned to 7c1e190064)

Solutions

  1. Resolve against an explicit absolute base before the call: Uri.parse("file:///android_asset/www/").buildUpon().appendPath("img/logo.png").build(), or resolve within a remapUri() implementation using the WebView's base URL
  2. On the JS side always hand absolute URLs (or use toPluginUri from native) when crossing into native code

Example fix

// before
resourceApi.openForRead(Uri.parse("img/logo.png")); // no scheme -> IllegalArgumentException

// after
Uri base = Uri.parse("file:///android_asset/www/index.html");
Uri abs = base.resolve(Uri.parse("img/logo.png"));
resourceApi.openForRead(abs);
Defensive patterns

Strategy: type-guard

Validate before calling

// resolve relative references against a known absolute base before calling
Uri base = Uri.parse("file:///android_asset/www/");
Uri abs = uri.isAbsolute() ? uri : base.resolve(uri);
OpenForReadResult r = resourceApi.openForRead(abs);

Type guard

static boolean isAbsoluteUri(Uri uri) {
    return uri != null && uri.isAbsolute(); // scheme present, e.g. file:// https:// content:// data:
}

Try / catch

try {
    resourceApi.openForRead(uri);
} catch (IllegalArgumentException e) {
    if ("Relative URIs are not supported.".equals(e.getMessage())) {
        // resolve against a base URI and retry
    }
}

Prevention

When it happens

Trigger: Passing Uri.parse("www/file.txt"), "img/logo.png", or a protocol-relative "//example.com/x" to openForRead/openOutputStream; also Uris recovered from JS as plain paths without a base; processUri called on an unresolved link from shouldInterceptRequest.

Common situations: Plugin receives a path string from JavaScript (document-relative) and forwards it straight to the resource API; migrating code from File-relative operations; parsing a href out of HTML and forgetting it can be relative.

Related errors


AI-assisted analysis of apache/cordova-android@7c1e190064 (2026-08-22). Data as JSON: /api/errors/5d5dc876a5eb19fe. Report an issue: GitHub.